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?
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.
How exactly would I insert that filter condition into the code below?
function setOpportunityServiceAddress(executionContext) { var formContext = executionContext.getFormContext(); var CurrentRecordId = formContext.data.entity.getId(); CurrentRecordId = CurrentRecordId.replace("{", "").replace("}", ""); var customer = formContext.getAttribute("ace_customer").getValue(); var customerType = customer[0].entityType; var OptVal = formContext.getAttribute("ace_serviceaddress").getValue(); var WkflowId = ""; var clientUrl = ""; var requestUri = ""; var xhr = ""; var IsDirty = ""; 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"); WkflowId = "fc67a661-9e41-49b5-b1ca-79eaaf9eadd4"; clientUrl = formContext.context.getClientUrl(); requestUri = clientUrl "/api/data/v9.0/workflows(" WkflowId ")/Microsoft.Dynamics.CRM.ExecuteWorkflow"; xhr = new XMLHttpRequest(); xhr.open("POST", requestUri, true); xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8"); xhr.setRequestHeader("OData-MaxVersion", "4.0"); xhr.setRequestHeader("OData-Version", "4.0"); xhr.onreadystatechange = function () { if (this.readyState == 4) { xhr.onreadystatechange = null; if (this.status == 200) { var result = JSON.parse(this.response); } else { var error = JSON.parse(this.response).error; } } }; xhr.send("{\"EntityId\":\"" CurrentRecordId "\"}"); IsDirty = formContext.data.entity.getIsDirty(); if (IsDirty) { formContext.data.refresh().then(function () { formContext.data.refresh(true); }, null); formContext.data.refresh(true); } else { //formContext.data.refresh(true); formContext.data.refresh().then(function () { formContext.data.refresh(true); }, null); formContext.data.refresh(true); } } 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"); WkflowId = "9e5b668c-2feb-4d92-abfa-5af7194a02df"; clientUrl = formContext.context.getClientUrl(); requestUri = clientUrl "/api/data/v9.0/workflows(" WkflowId ")/Microsoft.Dynamics.CRM.ExecuteWorkflow"; xhr = new XMLHttpRequest(); xhr.open("POST", requestUri, true); xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8"); xhr.setRequestHeader("OData-MaxVersion", "4.0"); xhr.setRequestHeader("OData-Version", "4.0"); xhr.onreadystatechange = function () { if (this.readyState == 4) { xhr.onreadystatechange = null; if (this.status == 200) { var result = JSON.parse(this.response); } else { var error = JSON.parse(this.response).error; } } }; xhr.send("{\"EntityId\":\"" CurrentRecordId "\"}"); IsDirty = formContext.data.entity.getIsDirty(); if (IsDirty) { formContext.data.refresh().then(function () { formContext.data.refresh(true); }, null); formContext.data.refresh(true); } else { //formContext.data.refresh(true); formContext.data.refresh().then(function () { formContext.data.refresh(true); }, null); formContext.data.refresh(true); } } } 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); formContext.getAttribute("ace_serviceaddresscountry").setValue(null); formContext.getAttribute("ace_serviceaddresslatitude").setValue(null); formContext.getAttribute("ace_serviceaddresslongitude").setValue(null); } }
Hi,
To get correct workflow guid you will need to add below filter condition while fetching workflow guid.
<filter type="and">
<condition attribute="type" operator="eq" value="1" />
<condition attribute="category" operator="eq" value="0" />
</filter>
Category - 0 -->Workflow
type - 1 --> Definition
then use workflowid which will give you workflow GUID
Make sure you convert fetchxml filter condition to Odata which I think is easy to do. Status condition is also required which you have already added in your odata query.
I seem to have gotten something working with hard-coded workflow Guid values.
The code below works. However, I still need to figure out how to obtain the Guid from the workflow name.
function setOpportunityServiceAddress(executionContext) { var formContext = executionContext.getFormContext(); var CurrentRecordId = formContext.data.entity.getId(); CurrentRecordId = CurrentRecordId.replace("{", "").replace("}", ""); var customer = formContext.getAttribute("ace_customer").getValue(); var customerType = customer[0].entityType; var OptVal = formContext.getAttribute("ace_serviceaddress").getValue(); var WkflowId = ""; var clientUrl = ""; var requestUri = ""; var xhr = ""; var IsDirty = ""; 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"); WkflowId = "fc67a661-9e41-49b5-b1ca-79eaaf9eadd4"; clientUrl = formContext.context.getClientUrl(); requestUri = clientUrl "/api/data/v9.0/workflows(" WkflowId ")/Microsoft.Dynamics.CRM.ExecuteWorkflow"; xhr = new XMLHttpRequest(); xhr.open("POST", requestUri, true); xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8"); xhr.setRequestHeader("OData-MaxVersion", "4.0"); xhr.setRequestHeader("OData-Version", "4.0"); xhr.onreadystatechange = function () { if (this.readyState == 4) { xhr.onreadystatechange = null; if (this.status == 200) { var result = JSON.parse(this.response); } else { var error = JSON.parse(this.response).error; } } }; xhr.send("{\"EntityId\":\"" CurrentRecordId "\"}"); IsDirty = formContext.data.entity.getIsDirty(); if (IsDirty) { formContext.data.refresh().then(function () { formContext.data.refresh(true); }, null); formContext.data.refresh(true); } else { //formContext.data.refresh(true); formContext.data.refresh().then(function () { formContext.data.refresh(true); }, null); formContext.data.refresh(true); } } 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"); WkflowId = "9e5b668c-2feb-4d92-abfa-5af7194a02df"; clientUrl = formContext.context.getClientUrl(); requestUri = clientUrl "/api/data/v9.0/workflows(" WkflowId ")/Microsoft.Dynamics.CRM.ExecuteWorkflow"; xhr = new XMLHttpRequest(); xhr.open("POST", requestUri, true); xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8"); xhr.setRequestHeader("OData-MaxVersion", "4.0"); xhr.setRequestHeader("OData-Version", "4.0"); xhr.onreadystatechange = function () { if (this.readyState == 4) { xhr.onreadystatechange = null; if (this.status == 200) { var result = JSON.parse(this.response); } else { var error = JSON.parse(this.response).error; } } }; xhr.send("{\"EntityId\":\"" CurrentRecordId "\"}"); IsDirty = formContext.data.entity.getIsDirty(); if (IsDirty) { formContext.data.refresh().then(function () { formContext.data.refresh(true); }, null); formContext.data.refresh(true); } else { //formContext.data.refresh(true); formContext.data.refresh().then(function () { formContext.data.refresh(true); }, null); formContext.data.refresh(true); } } } 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); formContext.getAttribute("ace_serviceaddresscountry").setValue(null); formContext.getAttribute("ace_serviceaddresslatitude").setValue(null); formContext.getAttribute("ace_serviceaddresslongitude").setValue(null); } }
In an attempt to at least get the workflow executing, I verified that the workflow is an Asynchronous On-Demand Workflow
Then, I tried to execute it directly via GUID as shown below in the LauchWorkFlow(executionContext, RecordGuid, WorkflowGuid) function.
Unfortunately, the code runs and even completes without error, but the workflow does not actually appear to execute because the data it is supposed to insert did not insert.
If I manually run the workflow, it does insert the data, but it doesn't seem to work via the JavaScript 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"); WkflowId = "{4e8df995-ba31-eb11-a813-000d3a5a7ad8}" LauchWorkFlow(executionContext, 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"); WkflowId = "fd4c0304-4231-eb11-a813-000d3a5a7103" 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 LauchWorkFlow(executionContext, RecordGuid, WorkflowGuid){ var formContext = executionContext.getFormContext(); var clientUrl = formContext.context.getClientUrl(); var workflowId = WorkflowGuid; var entityId = RecordGuid; var requestUri = clientUrl "/api/data/v9.0/workflows(" workflowId ")/Microsoft.Dynamics.CRM.ExecuteWorkflow"; var xhr = new XMLHttpRequest(); xhr.open("POST", requestUri, true); xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8"); xhr.setRequestHeader("OData-MaxVersion", "4.0"); xhr.setRequestHeader("OData-Version", "4.0"); xhr.onreadystatechange = function () { if (this.readyState == 4) { xhr.onreadystatechange = null; if (this.status == 200) { var result = JSON.parse(this.response); } else { var error = JSON.parse(this.response).error; } } }; xhr.send("{\"EntityId\":\"" entityId "\"}"); }
Am I running the workflow by the correct GUID?
Where am I going wrong?
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156