简体   繁体   中英

javascript Dynamics 365 unable to save my field before refreshing

I'm working on Dynamics CRM 365, trying to apply a logic using javascript on the opportunity form.

The need is to change a field's value and save the form before another treatment could refresh it.

var opportunityID= formContext.data.entity.getId();
           var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
                          "  <entity name='saft_bpf_isd_opportunities'>"+
                          "    <attribute name='businessprocessflowinstanceid' />"+
                          "    <attribute name='activestageid' />"+
                          "    <filter type='and'>"+
                          "      <condition attribute='bpf_opportunityid' operator='eq' uitype='opportunity' value='"+opportunityID+"' />"+
                          "    </filter>"+
                          "  </entity>"+
                          "</fetch>";

           Xrm.WebApi.retrieveMultipleRecords("saft_bpf_isd_opportunities","fetchXml= " + fetchXml).then(
           function success(result) { 
              debugger;
              alert("existed value ==> "+formContext.getAttribute("saft_activestage").getValue());
              alert("new value ==> "+result.entities[0]._activestageid_value);
              formContext.getAttribute("saft_activestage").setValue(result.entities[0]._activestageid_value);
              formContext.data.save(70).then(function (result) {});

           },function(error) {
                console.log(error.message);
              }
           );               
        // the reatement refreshing the form  
        formContext.data.process.setActiveProcess(idProcess_ISD, function (result) {});

But when I apply this code, I can simlply display the value but not assign it to the field desired.

Xrm.WebApi methods are always Asynchronous, it returns a browser Promise object & return the result in Async mode. Therefore when you want something to execute based on success callback result move it inside.

Same way .then helps you to put the code execution in sequence. Not sure how the cache clearing works randomly but made couple of changes. You can put breakpoints/debug to see it in action or just alert.

Xrm.WebApi.retrieveMultipleRecords("saft_bpf_isd_opportunities","fetchXml= " + fetchXml).then(
           function success(result) { 
              debugger;
              alert("existed value ==> "+formContext.getAttribute("saft_activestage").getValue());
              alert("new value ==> "+result.entities[0]._activestageid_value);
              formContext.getAttribute("saft_activestage").setValue(result.entities[0]._activestageid_value);
              formContext.data.save(70).then(function (result) {
            alert("after save"); 
            formContext.data.process.setActiveProcess(idProcess_ISD, function (result) { alert("setActiveProcess completed"); });
         });

           },function(error) {
                console.log(error.message);
              }
           );               

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM