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

  • joman Profile Picture
    joman 617 on at
    RE: Pass CRM Rest Builder Results to another function

    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;
    }
  • rw_ga Profile Picture
    rw_ga 52 on at
    RE: Pass CRM Rest Builder Results to another function

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

  • Verified answer
    joman Profile Picture
    joman 617 on at
    RE: Pass CRM Rest Builder Results to another function

    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.

  • joman Profile Picture
    joman 617 on at
    RE: Pass CRM Rest Builder Results to another function

    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
    rw_ga Profile Picture
    rw_ga 52 on at
    RE: Pass CRM Rest Builder Results to another function

    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

  • rw_ga Profile Picture
    rw_ga 52 on at
    RE: Pass CRM Rest Builder Results to another function

    @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

  • Mohd Tahir Profile Picture
    Mohd Tahir 676 on at
    RE: Pass CRM Rest Builder Results to another function

    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.

  • Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: Pass CRM Rest Builder Results to another function

    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

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 231,401 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans