Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Script doesn't update fields

Posted on by 550

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


  • Mick_4711 Profile Picture
    Mick_4711 550 on at
    RE: Script doesn't update fields

    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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans