web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Script doesn't update fields

(0) ShareShare
ReportReport
Posted on by 552

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) {
}
    }


I have the same question (0)
  • Mick_4711 Profile Picture
    552 on at

    I checked if the conditions and the commands  (from XMLHttpsRequest to req.send) are okay via the developer tools in chrome and they worked.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans