JavaScript triggered twice in Form Onchage

心已入冬 提交于 2020-06-01 05:07:46

问题


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.

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

回答1:


can I add my 2 cent.

Why are you firing your flow via Javascript and now via CDS connector.

If you fire your flow with Javascript, it will trigger only when user interaction with button happens. 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 too when Record is updated and filter out your field to be only Status.

In this way your flow will get triggered automatically only when Status field is changed.



来源:https://stackoverflow.com/questions/62051247/javascript-triggered-twice-in-form-onchage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!