Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer Service forum

Difficulty getting values using REST in JavaScript

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hello!

I'm trying to get some information from the contact entity to feed into a script on the Case entity.

I've built the below with the help of the CRM REST Builder, but I'm struggling to get any data pulled through into the variables.

Help, please!

function warningUserMessages() {
	var reportedByGuid = Xrm.Page.getAttribute("optevia_reportedbycontactid").getValue()[0].id;
	var cleanGuid = reportedByGuid.substr(1, reportedByGuid.length-2);
	
	//var dso_addresscheckstatus;
	//var dso_emailcheckstatus;
	//var dso_homephonecheckstatus;
	//var dso_mobilecheckstatus;
	//var dso_namecheckstatus;
	//var dso_workphonecheckstatus;
	
	var req = new XMLHttpRequest();
	req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts(" + cleanGuid + ")?$select=dso_addresscheckstatus,dso_emailcheckstatus,dso_homephonecheckstatus,dso_mobilecheckstatus,dso_namecheckstatus,dso_workphonecheckstatus", false);
	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.onreadystatechange = function() {
		if (this.readyState === 4) {
			req.onreadystatechange = null;
			if (this.status === 200) {
				var result = JSON.parse(this.response);
				var dso_addresscheckstatus = result["dso_addresscheckstatus"];
				var dso_emailcheckstatus = result["dso_emailcheckstatus"];
				var dso_homephonecheckstatus = result["dso_homephonecheckstatus"];
				var dso_mobilecheckstatus = result["dso_mobilecheckstatus"];
				var dso_namecheckstatus = result["dso_namecheckstatus"];
				var dso_workphonecheckstatus = result["dso_workphonecheckstatus"];
			} else {
				Xrm.Utility.alertDialog(this.statusText);
			}
		}
	};
	req.send();
	

//Functional code here, which I think works fine (though I can't be very sure until the above works
	

}


Categories:
  • Suggested answer
    Emre GULCAN Profile Picture
    Emre GULCAN 2,379 on at
    RE: Difficulty getting values using REST in JavaScript

    Hi,

    XmlHttpRequest and Jquery Ajax request's default process option is ASYNC, so you can not return value directly from your method. Instead of return data, you need to use callback functions that receive your result data as parameter and do your business logic or other process inside this callback method.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,492 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans