简体   繁体   English

JavaScript 在 Onchage 表单中触发两次

[英]JavaScript triggered twice in Form Onchage

I use Javascript to trigger a Flow when I want to change the state. Everything worked fine but the JavaScript function triggered two times when I change the state my record.当我想更改 state 时,我使用 Javascript 触发流程。一切正常,但当我更改 state 我的记录时,JavaScript function 触发了两次。

i think the Problem is something with the save.我认为问题在于保存。 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

 storno=function (executionContext) { var functionName = "storno"; var formContext = executionContext.getFormContext(); if(formContext.getAttribute("statecode").getValue() == 3) { //try{ var data= {"id": ""}; data.id = formContext.data.entity.getId(); var requestUrl = "https://prod-78.westeurope.logic.azure.com:443/workflows/6bff2c7051424e00b8519160db83c1bf/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=sfSBGx1gP3WzU1x7XMY64WVFc_RJ6EBMadIBnNudKR4"; var req = new XMLHttpRequest(); req.open("POST",requestUrl,true); req.setRequestHeader("Accept","application/json"); req.setRequestHeader("Content-Type","application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion","4.0"); req.setRequestHeader("OData-Version","4.0"); req.onreadystatechange = function(){ if(this.readyState == 4 || this.readyState == 2){ req.onreadystatechange = null; if (this.status == 200 || this.status == 204 || this.status == 202){ formContext.data.refresh(true); Xrm.Utility.openEntityForm(formContext.data.entity.getEntityName(),formContext.getAttribute("description").getValue()); } //else{ // var error = JSON.parse(this.response).error; //} } }; req.send(JSON.stringify(data)); //} //catch(ex){ //Obj_RunFlow.throwError(functionName,ex.massage); //} } };

Have you resolved this issue?你解决这个问题了吗? I was experiencing the same problem where an OnChange event function was being triggered twice, I did manage to resolve it so hopefully this helps我遇到了同样的问题,即 OnChange 事件 function 被触发了两次,我确实设法解决了它,希望这会有所帮助

I noticed this happens only right after the record gets created, and the reloads in UPDATE type.我注意到这种情况仅在记录创建后立即发生,并且以 UPDATE 类型重新加载。

Seems like the problem happens because the onChange event handlers are not being cleared properly (maybe product defect) hence added twice and triggered twice.似乎问题的发生是因为 onChange 事件处理程序没有被正确清除(可能是产品缺陷)因此添加了两次并触发了两次。

I have been able to avoid this by adding a function that adds the OnChange function at the Form OnLoad, this allows you to handle when you want to attach the function to the OnChange event.我已经能够通过添加一个 function 来避免这种情况,该 function 在 Form OnLoad 中添加了 OnChange function,这允许您处理何时要将 function 附加到 OnChange 事件。 Something similar to this:类似于此:

FNS = {
OnLoadMain: function (executionContext) {
    try {
        
        FNS.OnChangeEvents(executionContext);
    }
    catch (ex) {
        var alertStrings = {
            text: "Function : OnLoadMain" + ex.message.toString()
        };
        Xrm.Navigation.openAlertDialog(alertStrings);
    }
},
OnChangeEvents: function (executionContext) {
    try {
        var formContext = executionContext.getFormContext();
        if (formContext.ui.getFormType() !== 1) {
        
            formContext.getAttribute("statuscode").addOnChange(function (executionContext) { //Status on change only upon update to prevent event from being added twice (product bug)

                //do you stuff here
            });
        }

    } catch (ex) {
        var alertStrings = {
            text: "Function : OnChangeEvent" + ex.message.toString()
        };
        Xrm.Navigation.openAlertDialog(alertStrings);
    }
},
}

The only thing left is to place the event handler under the Form唯一剩下的就是将事件处理程序放在 Form 下

在此处输入图像描述

can I add my 2 cent.我可以加我的 2 美分吗?

Why are you firing your flow via Javascript and now via CDS connector.你为什么通过 Javascript 和现在通过 CDS 连接器触发你的流程。

If you fire your flow with Javascript, it will trigger only when user interaction with button happens.如果您使用 Javascript 触发流,它只会在用户与按钮交互时触发。 Your flow will not trigger if Record (status) is changed by some background logic or some other plugin.如果某些后台逻辑或某些其他插件更改了记录(状态),您的流程将不会触发。

To make your flow run on Status change under all circumstances let it be via user interaction of background logic,为了让您的流程在所有情况下都在状态变化上运行,让它通过后台逻辑的用户交互,

I would prefer running your flow via CDS connector and that toowhen Record is updated and filter out your field to be only Status.我更喜欢通过 CDS 连接器运行您的流程,并且在记录更新时也如此,并过滤掉您的字段,使其仅为状态。

In this way your flow will get triggered automatically only when Status field is changed.这样,只有当 Status 字段更改时,您的流程才会自动触发。

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

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