I'm not a code ninja.. I've been using chatGPT to help me write some code for a model driven power app.
The code saves a form once all the required fields are filled in and should progress it to the next stage which is an approval cloud flow.
I attached this to a button using the ribbon workbench and the code saves the form but doesn't progress to the next stage and subsequently doesn't fire the cloud flow..
any help would be greatly appreciated!
function moveToNextStageAndRunWorkflow() {
// Get the ID of the record that you want to move to the next stage
var recordId = Xrm.Page.data.entity.getId();
// Get the entity name of the record that you want to move to the next stage
var entityName = Xrm.Page.data.entity.getEntityName();
// Save the form
Xrm.Page.data.entity.save().then(function () {
// Query for the next stage ID
Xrm.WebApi.retrieveRecord("processstage", "cea0b454-a3fe-4b10-881c-ede1f5d44ef6", "?$select=processid").then(function (result) {
var processId = result.processid;
// Move the record to the next stage
Xrm.WebApi.update({
entityName: entityName,
id: recordId,
data: {
stageid: "cea0b454-a3fe-4b10-881c-ede1f5d44ef6",
processid: "{D59CD510-F6A6-ED11-AAD1-6045BD0F9D4C}"
}
}).then(function (result) {
// Run the cloud workflow
Xrm.WebApi.online.execute({
entityName: "msdyn_workflow",
name: "ExecuteWorkflow",
parameter: {
"WorkflowId": "5de21d43-cb31-4fe0-a98b-371264369b80",
"EntityId": recordId
}
}).then(function (result) {
console.log("Workflow executed successfully.");
}).catch(function (error) {
console.error("Error executing workflow: " + error.message);
});
}).catch(function (error) {
console.error("Error moving record to the next stage: " + error.message);
});
}).catch(function (error) {
console.error("Error retrieving next stage ID: " + error.message);
});
}).catch(function (error) {
console.error("Error saving form: " + error.message);
});
}
I've been through lots of iterations and at first one was that I wasn't including the process id as well as the stage id but that still didn't fix the issue.
also I've since learnt that the code isn't to trigger a cloud flow, that needs to be done through a trigger in power automate, which it already is. but I'm trying to wrap it all into 1 swift motion. move it to the next stage and run the flow. all in the name of being user friendly.
Still stuck on this.. i've now tried using the out of the box functions that are already applied to buttons already within the ribbon, such as a simple save action by referencing the library and using XrmCore.Commands.Save.saveForm but to no avail!