locked
Case form. I am trying to populate a text field from the lookupfield that is looking up from the Products Entity. RRS feed

  • Question

  • Function Description()
    {
    
        var ExistingProduct = Xrm.Page.getAttribute('new_part1').getValue();
        if (ExistingProduct === null) return;
     
        var serverUrl = Xrm.Page.context.getClientUrl();
     
        var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/ProductSet(guid'" + ExistingProduct[0].id + "')?$select=Description";  
        var req = new XMLHttpRequest();
        req.open("GET", oDataSelect, false); //false = sync & true = async
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        req.onreadystatechange = function () {
            if (req.readyState === 4) {
                if (req.status === 200) {         
                    var retrieved = JSON.parse(req.responseText).d;
                    Xrm.Page.getAttribute('new_partdescription').setValue(retrieved.Description); 
                    Xrm.Page.getAttribute('new_partdescription').setSubmitMode('always');
                    //ForceSave();
                }
            }
        };
        req.send();
    
    }

    I tried to post an image, but I have to verify my account :(

    PART1 Field is names new_part1 and it is a lookup field looking up from the products Entity, PartDescription is named and is the text box that I want to populate with the Description from the Products Entity from the lookup field PART1(new_part1)

    Can someone please help me? I am new to this

    Regards,

    Johnathan901

    Sunday, June 1, 2014 4:18 PM

Answers

  • Hi,
    try with this code:

    function setDescription() {
    	var existingProduct = Xrm.Page.getAttribute('new_part1').getValue();
        if (existingProduct === null) return;
    	
        var serverUrl;
        if (Xrm.Page.context.getClientUrl !== undefined) {
            serverUrl = Xrm.Page.context.getClientUrl();
        } else {
            serverUrl = Xrm.Page.context.getServerUrl();
        }
    	
        var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; 
        var productRequest = new XMLHttpRequest(); 
        productRequest.open("GET", ODataPath + "/ProductSet(guid'" + existingProduct[0].id + "')", false); 
        productRequest.setRequestHeader("Accept", "application/json"); 
        productRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
        productRequest.send();
        if (productRequest.status === 200) {
            var retrievedProduct = JSON.parse(productRequest.responseText).d; 
            var description = retrievedProduct.Description;
    		Xrm.Page.getAttribute('new_partdescription').setValue(description); 
            Xrm.Page.getAttribute('new_partdescription').setSubmitMode('always');
        }
        else {
            alert("error");
        }
    }


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Johnathan901 Sunday, June 1, 2014 4:58 PM
    Sunday, June 1, 2014 4:52 PM

All replies

  • Hi,
    try with this code:

    function setDescription() {
    	var existingProduct = Xrm.Page.getAttribute('new_part1').getValue();
        if (existingProduct === null) return;
    	
        var serverUrl;
        if (Xrm.Page.context.getClientUrl !== undefined) {
            serverUrl = Xrm.Page.context.getClientUrl();
        } else {
            serverUrl = Xrm.Page.context.getServerUrl();
        }
    	
        var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; 
        var productRequest = new XMLHttpRequest(); 
        productRequest.open("GET", ODataPath + "/ProductSet(guid'" + existingProduct[0].id + "')", false); 
        productRequest.setRequestHeader("Accept", "application/json"); 
        productRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
        productRequest.send();
        if (productRequest.status === 200) {
            var retrievedProduct = JSON.parse(productRequest.responseText).d; 
            var description = retrievedProduct.Description;
    		Xrm.Page.getAttribute('new_partdescription').setValue(description); 
            Xrm.Page.getAttribute('new_partdescription').setSubmitMode('always');
        }
        else {
            alert("error");
        }
    }


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Johnathan901 Sunday, June 1, 2014 4:58 PM
    Sunday, June 1, 2014 4:52 PM
  • You are awesome!!!!!! You just showed me the way to everything I needed for the rest of this project :)

    Thanks a ton!!!!!!!!

    Sunday, June 1, 2014 4:58 PM
  • Well I hate to bug you again, but I promise this will be the last question on this topic :)

    I have one more field that I need to populate from the Products Entity, (PRICE). I Have it in the lookup * I didn't think it was necessary, but I have tried just about every combination of code that I can think of based on your corrections). 

    I am still new to the output that this function provides, 

    Option 1.

    Can you show me how to view the output of this function?

    Option 2.

    Can you show me how to write to Description and Price from the lookup?

    I know they are the same thing in essence, however, If I can see exactly what is presented to the function during the request, I can learn a lot more :)

    Regards and thanks!

    Johnathan901

    Monday, June 2, 2014 7:11 PM
  • just to be sure, can you post a screenshot with the exact field you want to retrieve?


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Monday, June 2, 2014 9:52 PM
  • It is the price field that I am looking to return from the Product Entity, the name of the field that it is writing to is new_price1

    For some reason, I still cannot post screenshots or links. :(

    Tuesday, June 3, 2014 5:48 AM
  • If you want to get a currency field, try with:

    var price = parseFloat(retrievedProduct.Price.Value);
    

    after you can do a setValue like

    Xrm.Page.getAttribute('new_price1').setValue(price); 


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Tuesday, June 3, 2014 7:55 AM
  • Spot on, and I really do appreciate your help. 

    I ran into an issue where if I deleted the record from the new_part1 field that it wouldn't update the other fields, but I figured that out with an if (existingProduct != null) { "Run through my function"} else

    set values null and .00

    You are a true rockstar and I appreciate it. Just the little tidbits you have helped me with have given me the tools I need to move forward on several other things :)


    Friday, June 6, 2014 4:55 PM