简体   繁体   English

javascript Dynamics 365无法在刷新前保存我的字段

[英]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. 我正在使用Dynamics CRM 365,尝试在机会表单上使用javascript应用逻辑。

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. Xrm.WebApi方法始终是异步的,它返回浏览器Promise对象并以异步模式返回结果。 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. 以同样的方式.then可帮助您按顺序执行代码。 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);
              }
           );               

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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