I am trying to obtain the GUID for a Workflow that I need to execute via JavaScript and am running into a problem, for which I hoping someone can provide some guidance and potentially correct my code so I know what to do to remediate the issue.
1. What GUID Do I need to use to execute the workflow?
- workflowid
- workflowidunique
- or some other field's Guid
2. Once I know the Guid, what JavaScript code must I use to execute the workflow in a V9.1 UCI Environment?
The "entire" batch of Code I am using to do this is shown below.
I believe the errant part is at the very bottom and is specifically the code shown here:
function getFieldData(retrieveReq) { if (retrieveReq.readyState == 4) { if (retrieveReq.status == 200) { var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d; var retrievedValue = retrieved.results[0].workflowidunique; var id = retrievedValue.Id; alert(id); return id; } } }
My use case is that when a particular drop down value is selected, the code below executes on Change and is suppose to obtain the GUID of the workflow, and then execute the workflow, on change when the selected value of the drop down box is Yes also known as 100000000.
My code works perfectly and even goes out and pulls back the correct workflow data with GUID inside it.
I also have some question about which unique id I need to use to execute the workflow, and for some strange reason the workflow appears two times with different GUID's which I do not entirely understand why this is the case, so I need to make sure I get the right one.
Assuming that the value of workflowidunique is the actual GUID I need, which I am probably incorrect on that, I can see that my code is retrieving some of the GUID's as shown below.
However, where things get messed up is obviously with the parse code that sets the retrieved and retrievedValue variables.
I am looking for help understanding which GUID Value I need to return so I can reference it in the MakeWorkflowRequest and ExecuteWorkflow portions of the code, along with exactly how I need to modify the code to properly access and assign that GUID value to the variable I need to return.
ENTIRE BATCH OF JAVASCRIPT CODE BELOW
function setOpportunityServiceAddress(executionContext) { var formContext = executionContext.getFormContext(); var CurrentRecordId = formContext.data.entity.getId(); var customer = formContext.getAttribute("ace_customer").getValue(); var customerType = customer[0].entityType; var OptVal = formContext.getAttribute("ace_serviceaddress").getValue(); var WkflowId = ""; if (OptVal == 100000000) { if (customerType == "contact") { WkflowId = GetWorkflowIDByURL(executionContext, "/api/data/v9.1/workflows?$select=workflowidunique&$filter=(name eq 'Opportunity - (On Demand) Copy Related Contact Address to Service Address' and statuscode eq 2)&$top=1"); MakeWorkflowRequest(CurrentRecordId, WkflowId); } else if (customerType == "account") { WkflowId = GetWorkflowIDByURL(executionContext, "/api/data/v9.1/workflows?$select=workflowidunique&$filter=(name eq 'Opportunity - (On Demand) Copy Related Account Address to Service Address' and statuscode eq 2)&$top=1"); MakeWorkflowRequest(CurrentRecordId, WkflowId); } } else if (OptVal == 100000001); { formContext.getAttribute("ace_serviceaddressline1").setValue(null); formContext.getAttribute("ace_serviceaddressline2").setValue(null); formContext.getAttribute("ace_serviceaddresscity").setValue(null); formContext.getAttribute("ace_serviceaddressstate").setValue(null); formContext.getAttribute("ace_serviceaddresspostalcode").setValue(null); } } function MakeWorkflowRequest(recId, workflowId) { this.EntityId = { "guid": recId }; this.entity = { id: workflowId, entityType: "workflow" }; this.getMetadata = function() { return { boundParameter: "entity", parameterTypes: { "entity": { "typeName": "Microsoft.Dynamics.CRM.workflow", "structuralProperty": 5 }, "EntityId": { "typeName": "Edm.Guid", "structuralProperty": 1 } }, operationType: 0, operationName: "ExecuteWorkflow", }; }; } function ExecuteWorkflow(requestObject, onSuccessFunction) { Xrm.WebApi.online.execute(requestObject).then( onSuccessFunction, function(error) { console.log(error.message); }); } function runWorkflow(recId, workflowId, onSuccessFunction) { var workflowObject = new MakeWorkflowRequest(recId, workflowId); ExecuteWorkflow(workflowObject, onSuccessFunction); } function GetWorkflowIDByURL(executionContext, WorkflowURL) { var formContext = executionContext.getFormContext(); var clientUrl = formContext.context.getClientUrl(); var odataSelect = clientUrl WorkflowURL; var retrieveReq = new XMLHttpRequest(); retrieveReq.open("GET", odataSelect, false); retrieveReq.setRequestHeader("Accept", "application/json"); retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); retrieveReq.onreadystatechange = function() { getFieldData(this); }; retrieveReq.send(); function getFieldData(retrieveReq) { if (retrieveReq.readyState == 4) { if (retrieveReq.status == 200) { var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d; var retrievedValue = retrieved.results[0].workflowidunique; var id = retrievedValue.Id; alert(id); return id; } } } }
Any help would be greatly appreciated.