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 :
Microsoft Dynamics CRM (Archived)

Pass CRM Rest Builder Results to another function

(0) ShareShare
ReportReport
Posted on by 52

I'm new to Dynamics and JavaScript.  I've created a custom entity, used CRM REST Builder to generate my request, and added an event to a lookup.  I'd like to run two different queries, combine the results, and update the new_name field.  Below is my code for the first query.  How do I get and use the results outside of the if statement?  Also, how would I pass the results to another function?

function updateName(){
var productLookupObject = Xrm.Page.getAttribute("new_productid");
	if (productLookupObject != null) {
		var productLookupObjectValue = productLookupObject.getValue();
		if (productLookupObjectValue != null) {
			var entityID = productLookupObjectValue[0].id;
			entityID = entityID.replace(/[{}]/gi, '');

			var req = new XMLHttpRequest();
			req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_customproducts(" + entityID + ")?$select=new_productid", 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 result = JSON.parse(this.response);
						var new_productid = result["new_productid"];

						Xrm.Page.getAttribute("new_name").setValue(new_productid);
					} else {
						var error = JSON.parse(this.response).error; 
						alert(error.message);
					}
				}
			};
			req.send();

		}
	}
}

Thanks,

Ryan

*This post is locked for comments

I have the same question (0)
  • Thomas David Dayman Profile Picture
    11,323 on at

    I tried doing this as well and I didn't have a luck with it.

    The only way I was able to share the result outside of the if statement was just to populate a field with that result which then enables the rest of the JavaScript to use that value.

    Someone might find a solution to this but its the only way i've been able to do it

  • Mohd Tahir Profile Picture
    676 on at

    To my understanding of your thread, you could first make sure it is a synchronous call to the API. That is done by changing the parameter 'true' in the req.open statement with 'false'. This makes sure that your next function will be called only once we have retrieved results from the API. Check the edited code below for your reference:

    function updateName(){
    var productLookupObject = Xrm.Page.getAttribute("new_productid");
    	if (productLookupObject != null) {
    		var productId = null;
    		var productLookupObjectValue = productLookupObject.getValue();
    		if (productLookupObjectValue != null) {
    			var entityID = productLookupObjectValue[0].id;
    			entityID = entityID.replace(/[{}]/gi, '');
                var req = new XMLHttpRequest();
    			req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_customproducts(" + entityID + ")?$select=new_productid", 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    			req.onreadystatechange = function() {
    				if (this.readyState === 4) {
    					req.onreadystatechange = null;
    					if (this.status === 200) {
    						var result = JSON.parse(this.response);
    						productId = result["new_productid"];
    					} else {
    						var error = JSON.parse(this.response).error; 
    						alert(error.message);
    					}
    				}
    			};
    			req.send();
    			calledFunction(productId);
    			
    		}
    	}
    	
    	calledFunction(productId)
    			{
    		       Xrm.Page.getAttribute("new_name").setValue(productId);
                }
    }


    Please mark the answer as verified if it helped you.

  • rw_ga Profile Picture
    52 on at

    @Thomas, that is what I started with, but I want to eliminate the need for the 2 extra fields.

    @Mohd,

    I don't follow the updated information.  I may not have given you enough of my code to see where I was going with it.  Below is what I'm trying to do, but I get an error saying "Cannot read property 'new_productid' of undefined at updateName.  Can you help me modify this code?

    function updateName(){

    	if (Xrm.Page.getAttribute("new_manufacturingfacility").getValue() != null && Xrm.Page.getAttribute("new_productid").getValue() != null){
    		var productInfo = getProductInfo();
    		var new_productIDValue = productInfo["new_productid"];
    
    	alert (new_productIDValue);
    
    		var manufacturingFacilityInfo = getManufacturingFacilityInfo();
    		var new_nameValue = manufacturingFacilityInfo["new_name"];
    
    	alert (new_nameValue);
    
    		Xrm.Page.getAttribute("new_name").setValue(productInfo + " " + new_name);
    	}
    }
    
    function getProductInfo(){
    var productLookupObject = Xrm.Page.getAttribute("new_productid");
    	if (productLookupObject != null) {
    		var productLookupObjectValue = productLookupObject.getValue();
    		if (productLookupObjectValue != null) {
    			var entityID = productLookupObjectValue[0].id;
    			entityID = entityID.replace(/[{}]/gi, '');
    
    			var req = new XMLHttpRequest();
    			req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_products(" + entityID + ")?$select=new_productid", 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    			req.onreadystatechange = function() {
    				if (this.readyState === 4) {
    					req.onreadystatechange = null;
    					if (this.status === 200) {
    						var result = JSON.parse(this.response);
    						
    						return result;
    
    						} else {
    						var error = JSON.parse(this.response).error; 
    						alert(error.message);					}
    				}
    			};
    			req.send();
    		}
    	}
    }
    
    function getManufacturingFacilityInfo(){	
    var manufacturingFacilityLookupObject = Xrm.Page.getAttribute("new_manufacturingfacility");
    	if (manufacturingFacilityLookupObject != null) {
    		var manufacturingFacilityLookupObjectValue = manufacturingFacilityLookupObject.getValue();
    		if (manufacturingFacilityLookupObjectValue != null) {
    			var entityID = manufacturingFacilityLookupObjectValue[0].id;
    			entityID = entityID.replace(/[{}]/gi, '');
    
    			var req = new XMLHttpRequest();
    			req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_manufacturingfacilities()?$select=new_name", 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    			req.onreadystatechange = function() {
    				if (this.readyState === 4) {
    					req.onreadystatechange = null;
    					if (this.status === 200) {
    						var result = JSON.parse(this.response);
    						
    						return result;
    
    					} else {
    						var error = JSON.parse(this.response).error; 
    						alert(error.message);					}
    				}
    			};
    			req.send();
    		}
    	}
    }

    Thanks,

    Ryan

  • Verified answer
    rw_ga Profile Picture
    52 on at

    OK I think I've figured it out!  Again, I'm new to all of this so there may be a better/safer way to do this.  Maybe this will help someone else.  See bold code below.  I've updated the code to include Joman's suggestion to check the variables before trying to use them.

    function updateName(){
    	if (Xrm.Page.getAttribute("new_manufacturingfacility").getValue() != null && Xrm.Page.getAttribute("new_productid").getValue() != null){
    		var productInfo = getProductInfo();
    		if(productInfo && productInfo["new_productid"]){
    			var productIDValue = productInfo["new_productid"];
    		}
    		
    		var manufacturingFacilityInfo = getManufacturingFacilityInfo();
    		if(manufacturingFacilityInfo && manufacturingFacilityInfo["new_name"]){
    			var manufacturingFacilityValue = manufacturingFacilityInfo["new_name"];
    		}
    
    		if(productIDValue && manufacturingFacilityValue){
    			Xrm.Page.getAttribute("new_name").setValue(productIDValue + " / " + manufacturingFacilityValue);
    		}
    		
    	}
    }
    
    function getProductInfo(){
    var productLookupObject = Xrm.Page.getAttribute("new_productid");
    	if (productLookupObject != null) {
    		var productLookupObjectValue = productLookupObject.getValue();
    		if (productLookupObjectValue != null) {
    			var entityID = productLookupObjectValue[0].id;
    			entityID = entityID.replace(/[{}]/gi, '');
    
    			var req = new XMLHttpRequest();
    			req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_products(" + entityID + ")?$select=new_productid", 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    			req.onreadystatechange = function() {
    				if (this.readyState == 4) {
    					req.onreadystatechange = null;
    					if (this.status == 200) {
    						//Don't need these right now because we are jsut passing the information back to another function.
    						//var result = JSON.parse(this.response);
    						//var new_productid = result["new_productid"];
    					}
    					else {
    						var error = JSON.parse(this.response).error; 
    						alert(error.message);
    					}
    				}
    			};
    			req.send();
    			var response = JSON.parse(req.responseText);
    			return response;
    		}
    	}
    }
    
    function getManufacturingFacilityInfo(){	
    var manufacturingFacilityLookupObject = Xrm.Page.getAttribute("new_manufacturingfacility");
    	if (manufacturingFacilityLookupObject != null) {
    		var manufacturingFacilityLookupObjectValue = manufacturingFacilityLookupObject.getValue();
    		if (manufacturingFacilityLookupObjectValue != null) {
    			var entityID = manufacturingFacilityLookupObjectValue[0].id;
    			entityID = entityID.replace(/[{}]/gi, '');
    
    			var req = new XMLHttpRequest();
    			req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_manufacturingfacilities(" + entityID + ")?$select=new_name", 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    			req.onreadystatechange = function() {
    				if (this.readyState == 4) {
    					req.onreadystatechange = null;
    					if (this.status == 200) {
    						//Don't need these right now because we are jsut passing the information back to another function.
    						//var result = JSON.parse(this.response);
    						//var new_name = result["new_name"];
    					}
    					else {
    						var error = JSON.parse(this.response).error; 
    						alert(error.message);
    					}
    				}
    			};
    			req.send();
    			var response = JSON.parse(req.responseText);
    			return response;
    		}
    	}
    }

    Thanks,

    Ryan

  • joman Profile Picture
    617 on at

    You should make global variable and store your results there:

    var Results = undefined;
    function F1(){
    ....
    Results = "someresult";
    }
    function F2(){
    ....
    if(Results){
      do_something(Results);
      Results = undefined;
    }
    }


  • Verified answer
    joman Profile Picture
    617 on at

    you  need to modify script:

    var manufacturingFacilityInfo = getManufacturingFacilityInfo();
    if(manufacturingFacilityInfo && manufacturingFacilityInfo["new_name"]){
       var new_nameValue = manufacturingFacilityInfo["new_name"];
    }

    You need always check variables before use it in JS.

  • rw_ga Profile Picture
    52 on at

    I'll update my code.  Is the "req.responseText" the best way to pass the values back to the calling function?

  • joman Profile Picture
    617 on at

    It is not the best way to run requests synchronously.
    If you need to return only "new_productid" then better to return only this value.

    var response = JSON.parse(req.responseText);
    if(response && response["new_productid"]){ return response["new_productid"];
    }
    else{
    return null;
    }

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans