Workaround for double update on statecode change via WebAPI

The new WebApi of Dynamics CRM 2016 has some strange behavior when you only update the state code. You would expect that there would be one update to the record, but when you inspect the audit logs, there are two updates to the record. First there’s an update to set the state of the record the the state it already has. The second update to the record changes it to the actual state. This is both at on-premise and Online versions of Dynamics CRM.

A detailed post on this issue can be found at this blog post of my colleague Martijn Eikelenboom. As this issue is a real pain with one of my own implementations, I created a ticket with Microsoft to find if this is a bug or expected behavior. Because my customer couldn’t wait for a bugfix, I have created a workaround.

The code that activates the double update

At first, I wrote code to update the statecode and statuscode on a record. See the code below.

<br />
function setStatus(statusReasonValue, stateValue) {<br />
    var entitdyId = Xrm.Page.data.entity.getId();<br />
    var entity = {};<br />
    entity.statuscode = statusReasonValue;<br />
    entity.statecode = stateValue;</p>
<p>    $.ajax({<br />
        type: &quot;PATCH&quot;,<br />
        contentType: &quot;application/json; charset=utf-8&quot;,<br />
        datatype: &quot;json&quot;,<br />
        url: Xrm.Page.context.getClientUrl() + &quot;/api/data/v8.0/mg_orders(&quot; + entitdyId.substring(1,37) + &quot;)&quot;,<br />
        data: JSON.stringify(entity),<br />
        beforeSend: function (XMLHttpRequest) {<br />
            XMLHttpRequest.setRequestHeader(&quot;OData-MaxVersion&quot;, &quot;4.0&quot;);<br />
            XMLHttpRequest.setRequestHeader(&quot;OData-Version&quot;, &quot;4.0&quot;);<br />
            XMLHttpRequest.setRequestHeader(&quot;Accept&quot;, &quot;application/json&quot;);<br />
        },<br />
        async: false,<br />
        success: function (data, textStatus, xhr) {<br />
            //Success - No Return Data - Do Something<br />
            Xrm.Page.ui.close();<br />
        },<br />
        error: function (xhr, textStatus, errorThrown) {<br />
            alert(textStatus + &quot; &quot; + errorThrown);<br />
        }<br />
    });<br />
}<br />

The workaround

I needed an other way to change the status of the record and thought of workflow. When changing the status by using workflow, there is only one update instead of two. Therefor, at first my workaround was to create an Action process with the statuscode as an incoming variable. Second I have changed the javascript to call the Action with the proper statuscode and third: the Action does the trick! Also I used Process 2.0 as JavaScript library. You’ll find what you need down here. Good luck updating the statuscode!

Screenshot of the Action

action-process-update-statecode

The new JavaScript

<br />
function setStatus(statusReasonValue, stateValue) {<br />
    debugger;<br />
    Process.callAction(<br />
    &quot;mg_SetOrderState&quot;,<br />
    [{<br />
        key: &quot;Target&quot;,<br />
        type: Process.Type.EntityReference,<br />
        value: new Process.EntityReference(&quot;mg_order&quot;, Xrm.Page.data.entity.getId())<br />
    },<br />
    {<br />
        key: &quot;StatusCode&quot;,<br />
        type: Process.Type.Int,<br />
        value: statusReasonValue<br />
    }],<br />
    function (params) {<br />
        // Success<br />
        Xrm.Page.ui.close();<br />
    },<br />
    function (e, t) {<br />
        // Error<br />
        alert(e);</p>
<p>        // Write the trace log to the dev console<br />
        if (window.console &amp;&amp; console.error) {<br />
            console.error(e + &quot;\n&quot; + t);<br />
        }<br />
    });<br />
}<br />

2 thoughts on “Workaround for double update on statecode change via WebAPI

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.