Warm tip: This article is reproduced from serverfault.com, please click

dynamics crm-JavaScript在Form Onchage中触发了两次

(dynamics crm - JavaScript triggered twice in Form Onchage)

发布于 2020-05-27 19:23:38

当我想更改状态时,我使用Javascript来触发流。一切正常,但是当我更改记录状态时,JavaScript函数触发了两次。

我认为问题出在保存方面。 在此处输入图片说明 在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

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);
		//}
	}
		
};
Questioner
bnb
Viewed
0
Antonio González 2020-12-01 18:55:27

你解决了这个问题吗?我遇到了相同的问题,两次触发OnChange事件函数,但确实设法解决了这个问题,因此希望这对你有所帮助

我注意到这种情况仅在创建记录后立即发生,并且以UPDATE类型重新加载。

似乎发生问题是因为onChange事件处理程序未正确清除(可能是产品缺陷),因此被添加了两次并触发了两次。

我已经能够通过在Form OnLoad上添加一个添加OnChange函数的函数来避免这种情况,这使你可以在要将函数附加到OnChange事件时进行处理。类似于以下内容:

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);
    }
},
}

剩下的唯一事情是将事件处理程序放置在Form下

在此处输入图片说明