Hi together,
I wrote a javascript that should change some fields to "required fields" if a statuscode value or the stepname value in the business process flow are changing to special values.
But it doesn't really work.
Furthermore the script should also work if a user made a false entry and the script should react directly when the user is correcting the field.
But this also doesn't seem to happen.
The javascript is placed in the in the "onchange"-trigger of the opportunity field "statuscode".
What can I do to make this work fluently?
Can anybody help me with this? This would be awesome!
Best regards
Mick
function p_changeReqLevel_MatSurfSys() { try{ var req = new XMLHttpRequest(); var currentOppID = Xrm.Page.data.entity.getId(); currentOppID = currentOppID.replace(/[{}]/g, ""); req.open("GET", Xrm.Page.context.getClientUrl() "/api/data/v8.2/opportunities?$select=name,opportunityid,stageid,statecode,statuscode,stepid,stepname&$filter=opportunityid eq " currentOppID, true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); req.onreadystatechange = function() { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var results = JSON.parse(this.response); for (var i = 0; i < results.value.length; i ) { var name = results.value[i]["name"]; var opportunityid = results.value[i]["opportunityid"]; var stageid = results.value[i]["stageid"]; var statecode = results.value[i]["statecode"]; var statuscode = results.value[i]["statuscode"]; var statecode_formatted = results.value[i]["statecode@OData.Community.Display.V1.FormattedValue"]; var stepid = results.value[i]["stepid"]; var stepname = results.value[i]["stepname"]; if(stepname != "1-Qualify" || statuscode == 771630002 || statuscode == 771630003 ||statuscode == 771630005 || statuscode == 771630006) { Xrm.Page.getAttribute("p_system").setRequiredLevel("required"); Xrm.Page.getAttribute("p_material").setRequiredLevel("required"); Xrm.Page.getAttribute("p_surface").setRequiredLevel("required"); } else { Xrm.Page.getAttribute("p_system").setRequiredLevel("none"); Xrm.Page.getAttribute("p_material").setRequiredLevel("none"); Xrm.Page.getAttribute("p_surface").setRequiredLevel("none"); } } }} else { // Xrm.Utility.alertDialog(this.statusText); return; } } req.send(); } catch (error) { } }