SBX - Search With Button

SBX - Forum Post Title

change state and statusreason of salesorder ( order entity ) via JavaScript

Microsoft Dynamics CRM Forum

Albert_ asked a question on 14 Jul 2018 1:46 AM
My Badges

Question Status

Verified

Hi all experts.
We all know that Order states are :

0-Active
1-Submiited

2-Canceled

3-Fulfilled

4-Invoiced

I wanna go from Submitted( in progress statusreason ) to Fullfield ( Partial statusreson) with soap.
Here is the code and function :

function changeStatusCodeToPartial()  { 
   XrmServiceToolkit.Soap.SetState("salesorder", Xrm.Page.data.entity.getId(), 3, 100002, false);
  Xrm.Page.data.refresh(true).then(null, null);
 }

But does not work.
I can go from Active ( prepared statusreason) to Submitted ( in-progress via below code ) :
function changeStatusCodeToSubmitted() { 
   XrmServiceToolkit.Soap.SetState("salesorder", Xrm.Page.data.entity.getId(),1, 3, false);
   Xrm.Page.data.refresh(true).then(null, null);

}

How can it be possible?
Reply
Drew Poggemann responded on 14 Jul 2018 8:40 PM
My Badges
Suggested Answer

Hi Albert,

You will need to set the state transitions for the status reason code or this will fail.  See the following article for details on how to set the transitions.

www.preact.co.uk/.../how-to-set-dynamics-crm-status-reason-transitions

Hope this helps!

Reply
Albert_ responded on 14 Jul 2018 11:03 PM
My Badges

I use crm 2011 sir

Reply
Ravi Kashyap responded on 14 Jul 2018 11:24 PM
My Badges
Suggested Answer

Hi,

Insteadd of setting the state directly, try using the FulfillSalesOrderRequest message-

docs.microsoft.com/.../gg307979(v=crm.5)

mileyja.blogspot.com/.../full-sales-order-in-microsoft-dynamics.html

Hope this helps.

Reply
Temmy Raharjo responded on 15 Jul 2018 12:53 AM
My Badges
Suggested Answer

Hi,

Do you have the error log when you changed it? In my experience when I'm work at Incident Entity. I need to change the statecode to Active first because CRM operation denied me to change directly to another statecode.

Try to put debugger when you do SetState. From that it will be more clearer to do something.

Reply
Nithya Gopinath responded on 15 Jul 2018 11:20 PM
My Badges
Suggested Answer
Albert_ responded on 20 Jul 2018 1:52 AM
My Badges
Verified Answer

Hi there All, THank u for your support,

It suppose that for  Canceled and Fulfilled we should different service names !

So for Canceled I did this :

...........................................................................................................................................................................

            var requestMain = ""

              requestMain += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";

              requestMain += "  <s:Body>";

              requestMain += "    <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";

              requestMain += "      <request i:type=\"b:CancelSalesOrderRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">";

              requestMain += "        <a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">";

              requestMain += "          <a:KeyValuePairOfstringanyType>";

              requestMain += "            <c:key>OrderClose</c:key>";

              requestMain += "            <c:value i:type=\"a:Entity\">";

              requestMain += "              <a:Attributes>";

              requestMain += "                <a:KeyValuePairOfstringanyType>";

              requestMain += "                  <c:key>salesorderid</c:key>";

              requestMain += "                  <c:value i:type=\"a:EntityReference\">";

              requestMain += "                    <a:Id>" +recordGuid+ "</a:Id>";

              requestMain += "                    <a:LogicalName>salesorder</a:LogicalName>";

              requestMain += "                    <a:Name i:nil=\"true\" />";

              requestMain += "                  </c:value>";

              requestMain += "                </a:KeyValuePairOfstringanyType>";

              requestMain += "                <a:KeyValuePairOfstringanyType>";

              requestMain += "                  <c:key>subject</c:key>";

              requestMain += "                  <c:value i:type=\"d:string\" xmlns:d=\"www.w3.org/.../XMLSchema\">Sales Order Cancelled - No Money</c:value>";

              requestMain += "                </a:KeyValuePairOfstringanyType>";

              requestMain += "              </a:Attributes>";

              requestMain += "              <a:EntityState i:nil=\"true\" />";

              requestMain += "              <a:FormattedValues />";

              requestMain += "              <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";

              requestMain += "              <a:LogicalName>orderclose</a:LogicalName>";

              requestMain += "              <a:RelatedEntities />";

              requestMain += "            </c:value>";

              requestMain += "          </a:KeyValuePairOfstringanyType>";

              requestMain += "          <a:KeyValuePairOfstringanyType>";

              requestMain += "            <c:key>Status</c:key>";

              requestMain += "            <c:value i:type=\"a:OptionSetValue\">";

              requestMain += "              <a:Value>" +statusCode+"</a:Value>";

              requestMain += "            </c:value>";

              requestMain += "          </a:KeyValuePairOfstringanyType>";

              requestMain += "        </a:Parameters>";

              requestMain += "        <a:RequestId i:nil=\"true\" />";

              requestMain += "        <a:RequestName>CancelSalesOrder</a:RequestName>";

              requestMain += "      </request>";

              requestMain += "    </Execute>";

              requestMain += "  </s:Body>";

              requestMain += "</s:Envelope>";

              var req = new XMLHttpRequest();

              req.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", true);

              // Responses will return XML. It isn't possible to return JSON.

              req.setRequestHeader("Accept", "application/xml, text/xml, */*");

              req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

              req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot;);

              var successCallback = null;

              var errorCallback = null;

              req.send(requestMain);

...........................................................................................................................................................................

And for Fulfilled I do this :

...........................................................................................................................................................................

                 var requestMain = ""

              requestMain += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";

              requestMain += "  <s:Body>";

              requestMain += "    <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";

              requestMain += "      <request i:type=\"b:FulfillSalesOrderRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">";

              requestMain += "        <a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">";

              requestMain += "          <a:KeyValuePairOfstringanyType>";

              requestMain += "            <c:key>OrderClose</c:key>";

              requestMain += "            <c:value i:type=\"a:Entity\">";

              requestMain += "              <a:Attributes>";

              requestMain += "                <a:KeyValuePairOfstringanyType>";

              requestMain += "                  <c:key>salesorderid</c:key>";

              requestMain += "                  <c:value i:type=\"a:EntityReference\">";

              requestMain += "                    <a:Id>" +recordGuid+ "</a:Id>";

              requestMain += "                    <a:LogicalName>salesorder</a:LogicalName>";

              requestMain += "                    <a:Name i:nil=\"true\" />";

              requestMain += "                  </c:value>";

              requestMain += "                </a:KeyValuePairOfstringanyType>";

              requestMain += "                <a:KeyValuePairOfstringanyType>";

              requestMain += "                  <c:key>subject</c:key>";

              requestMain += "                  <c:value i:type=\"d:string\" xmlns:d=\"www.w3.org/.../XMLSchema\">Sales Order Closed</c:value>";

              requestMain += "                </a:KeyValuePairOfstringanyType>";

              requestMain += "              </a:Attributes>";

              requestMain += "              <a:EntityState i:nil=\"true\" />";

              requestMain += "              <a:FormattedValues />";

              requestMain += "              <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";

              requestMain += "              <a:LogicalName>orderclose</a:LogicalName>";

              requestMain += "              <a:RelatedEntities />";

              requestMain += "            </c:value>";

              requestMain += "          </a:KeyValuePairOfstringanyType>";

              requestMain += "          <a:KeyValuePairOfstringanyType>";

              requestMain += "            <c:key>Status</c:key>";

              requestMain += "            <c:value i:type=\"a:OptionSetValue\">";

              requestMain += "              <a:Value>"+statusCode+"</a:Value>";

              requestMain += "            </c:value>";

              requestMain += "          </a:KeyValuePairOfstringanyType>";

              requestMain += "        </a:Parameters>";

              requestMain += "        <a:RequestId i:nil=\"true\" />";

              requestMain += "        <a:RequestName>FulfillSalesOrder</a:RequestName>";

              requestMain += "      </request>";

              requestMain += "    </Execute>";

              requestMain += "  </s:Body>";

              requestMain += "</s:Envelope>";

              var req = new XMLHttpRequest();

              req.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", true);

              // Responses will return XML. It isn't possible to return JSON.

              req.setRequestHeader("Accept", "application/xml, text/xml, */*");

              req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

              req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot;);

              var successCallback = null;

              var errorCallback = null;

               req.send(requestMain);

...........................................................................................................................................................................

Just replace statusCode and recordGuid with your own.

Reply
Albert_ responded on 20 Jul 2018 1:52 AM
My Badges
Verified Answer

Hi there All, THank u for your support,

It suppose that for  Canceled and Fulfilled we should different service names !

So for Canceled I did this :

...........................................................................................................................................................................

            var requestMain = ""

              requestMain += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";

              requestMain += "  <s:Body>";

              requestMain += "    <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";

              requestMain += "      <request i:type=\"b:CancelSalesOrderRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">";

              requestMain += "        <a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">";

              requestMain += "          <a:KeyValuePairOfstringanyType>";

              requestMain += "            <c:key>OrderClose</c:key>";

              requestMain += "            <c:value i:type=\"a:Entity\">";

              requestMain += "              <a:Attributes>";

              requestMain += "                <a:KeyValuePairOfstringanyType>";

              requestMain += "                  <c:key>salesorderid</c:key>";

              requestMain += "                  <c:value i:type=\"a:EntityReference\">";

              requestMain += "                    <a:Id>" +recordGuid+ "</a:Id>";

              requestMain += "                    <a:LogicalName>salesorder</a:LogicalName>";

              requestMain += "                    <a:Name i:nil=\"true\" />";

              requestMain += "                  </c:value>";

              requestMain += "                </a:KeyValuePairOfstringanyType>";

              requestMain += "                <a:KeyValuePairOfstringanyType>";

              requestMain += "                  <c:key>subject</c:key>";

              requestMain += "                  <c:value i:type=\"d:string\" xmlns:d=\"www.w3.org/.../XMLSchema\">Sales Order Cancelled - No Money</c:value>";

              requestMain += "                </a:KeyValuePairOfstringanyType>";

              requestMain += "              </a:Attributes>";

              requestMain += "              <a:EntityState i:nil=\"true\" />";

              requestMain += "              <a:FormattedValues />";

              requestMain += "              <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";

              requestMain += "              <a:LogicalName>orderclose</a:LogicalName>";

              requestMain += "              <a:RelatedEntities />";

              requestMain += "            </c:value>";

              requestMain += "          </a:KeyValuePairOfstringanyType>";

              requestMain += "          <a:KeyValuePairOfstringanyType>";

              requestMain += "            <c:key>Status</c:key>";

              requestMain += "            <c:value i:type=\"a:OptionSetValue\">";

              requestMain += "              <a:Value>" +statusCode+"</a:Value>";

              requestMain += "            </c:value>";

              requestMain += "          </a:KeyValuePairOfstringanyType>";

              requestMain += "        </a:Parameters>";

              requestMain += "        <a:RequestId i:nil=\"true\" />";

              requestMain += "        <a:RequestName>CancelSalesOrder</a:RequestName>";

              requestMain += "      </request>";

              requestMain += "    </Execute>";

              requestMain += "  </s:Body>";

              requestMain += "</s:Envelope>";

              var req = new XMLHttpRequest();

              req.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", true);

              // Responses will return XML. It isn't possible to return JSON.

              req.setRequestHeader("Accept", "application/xml, text/xml, */*");

              req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

              req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot;);

              var successCallback = null;

              var errorCallback = null;

              req.send(requestMain);

...........................................................................................................................................................................

And for Fulfilled I do this :

...........................................................................................................................................................................

                 var requestMain = ""

              requestMain += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";

              requestMain += "  <s:Body>";

              requestMain += "    <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";

              requestMain += "      <request i:type=\"b:FulfillSalesOrderRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">";

              requestMain += "        <a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">";

              requestMain += "          <a:KeyValuePairOfstringanyType>";

              requestMain += "            <c:key>OrderClose</c:key>";

              requestMain += "            <c:value i:type=\"a:Entity\">";

              requestMain += "              <a:Attributes>";

              requestMain += "                <a:KeyValuePairOfstringanyType>";

              requestMain += "                  <c:key>salesorderid</c:key>";

              requestMain += "                  <c:value i:type=\"a:EntityReference\">";

              requestMain += "                    <a:Id>" +recordGuid+ "</a:Id>";

              requestMain += "                    <a:LogicalName>salesorder</a:LogicalName>";

              requestMain += "                    <a:Name i:nil=\"true\" />";

              requestMain += "                  </c:value>";

              requestMain += "                </a:KeyValuePairOfstringanyType>";

              requestMain += "                <a:KeyValuePairOfstringanyType>";

              requestMain += "                  <c:key>subject</c:key>";

              requestMain += "                  <c:value i:type=\"d:string\" xmlns:d=\"www.w3.org/.../XMLSchema\">Sales Order Closed</c:value>";

              requestMain += "                </a:KeyValuePairOfstringanyType>";

              requestMain += "              </a:Attributes>";

              requestMain += "              <a:EntityState i:nil=\"true\" />";

              requestMain += "              <a:FormattedValues />";

              requestMain += "              <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";

              requestMain += "              <a:LogicalName>orderclose</a:LogicalName>";

              requestMain += "              <a:RelatedEntities />";

              requestMain += "            </c:value>";

              requestMain += "          </a:KeyValuePairOfstringanyType>";

              requestMain += "          <a:KeyValuePairOfstringanyType>";

              requestMain += "            <c:key>Status</c:key>";

              requestMain += "            <c:value i:type=\"a:OptionSetValue\">";

              requestMain += "              <a:Value>"+statusCode+"</a:Value>";

              requestMain += "            </c:value>";

              requestMain += "          </a:KeyValuePairOfstringanyType>";

              requestMain += "        </a:Parameters>";

              requestMain += "        <a:RequestId i:nil=\"true\" />";

              requestMain += "        <a:RequestName>FulfillSalesOrder</a:RequestName>";

              requestMain += "      </request>";

              requestMain += "    </Execute>";

              requestMain += "  </s:Body>";

              requestMain += "</s:Envelope>";

              var req = new XMLHttpRequest();

              req.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", true);

              // Responses will return XML. It isn't possible to return JSON.

              req.setRequestHeader("Accept", "application/xml, text/xml, */*");

              req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

              req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot;);

              var successCallback = null;

              var errorCallback = null;

               req.send(requestMain);

...........................................................................................................................................................................

Just replace statusCode and recordGuid with your own.

Reply
Drew Poggemann responded on 14 Jul 2018 8:40 PM
My Badges
Suggested Answer

Hi Albert,

You will need to set the state transitions for the status reason code or this will fail.  See the following article for details on how to set the transitions.

www.preact.co.uk/.../how-to-set-dynamics-crm-status-reason-transitions

Hope this helps!

Reply
Ravi Kashyap responded on 14 Jul 2018 11:24 PM
My Badges
Suggested Answer

Hi,

Insteadd of setting the state directly, try using the FulfillSalesOrderRequest message-

docs.microsoft.com/.../gg307979(v=crm.5)

mileyja.blogspot.com/.../full-sales-order-in-microsoft-dynamics.html

Hope this helps.

Reply
Temmy Raharjo responded on 15 Jul 2018 12:53 AM
My Badges
Suggested Answer

Hi,

Do you have the error log when you changed it? In my experience when I'm work at Incident Entity. I need to change the statecode to Active first because CRM operation denied me to change directly to another statecode.

Try to put debugger when you do SetState. From that it will be more clearer to do something.

Reply
Nithya Gopinath responded on 15 Jul 2018 11:20 PM
My Badges
Suggested Answer

SBX - Two Col Forum

SBX - Migrated JS