locked
Multiple lookup fields with Onchange events using the exact same function. RRS feed

  • Question

  • I have 10 fields on a form that use the exact same function to do its work.

    Problem:

    I am a noob and admit it :). I was able to get all 10 fields to do what I wanted, But I had to copy and paste the function 10 times and change the names of the functions and call each different function for each different lookup field. I would like to streamline this code, but I have no clue where to start and I have been banging my head against this for 2 days now lol. I am sure one of you guys can point me in the right direction and I would greatly appreciate any help 

    Brief description of its purpose.

    There is a look up field called new_part1 that looks up from the Products Entity, The on change event populates 2 other fields from the products entity called Description, and Price (new_partdescription)(new_price). It then calculates all 10 fields (new_price*) and then populates (new_repairtotal) with the total of all 10 (new_price*) fields.

    What I am looking for :):

    I am looking to create a singular function for the population of (new_partdescription*) and (new_part*) that I can call based on the current fields on change event. 

    I remember back in my old visual basic class days that you can put "me." in front of whatever you were doing and it new that you were saying "This field" and you could reuse that function over an over again based on that. I've done my fair share of googling and I guess I am not asking the right search string. 

    The other thing I would like to do is have the total of (new_price*) in a singular function and call it from within my (new_partdesciption*), (new_price) function so that I don't have a lot of code for a simple process. (Trying to be a good programmer :))

    Please find below the first fields code, currently I have the 10 functions for this simple process, all the same except that I  rename the function to setdescription2, setdesciption3 and so on. I also change the new_partdescription to new_partdescription2, new_partdescription3, and the new_price1 to new_price2, new_price3, etc etc. new_repairtotal is a static 1 time field in the header. I hope I was descriptive enough for help and really do appreciate it. 

    function setDescription() {
            var existingProduct = Xrm.Page.getAttribute('new_part1').getValue();
            if (existingProduct != null) {
            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;
                var price = parseFloat(retrievedProduct.Price.Value);
                var price1 = Xrm.Page.getAttribute('new_price1').getValue();
                var price2 = Xrm.Page.getAttribute('new_price2').getValue();
                var price3 = Xrm.Page.getAttribute('new_price3').getValue();
                var price4 = Xrm.Page.getAttribute('new_price4').getValue();
                var price5 = Xrm.Page.getAttribute('new_price5').getValue();
                var price6 = Xrm.Page.getAttribute('new_price6').getValue();
                var price7 = Xrm.Page.getAttribute('new_price7').getValue();
                var price8 = Xrm.Page.getAttribute('new_price8').getValue();
                var price9 = Xrm.Page.getAttribute('new_price9').getValue();
                var price10 = Xrm.Page.getAttribute('new_price10').getValue();
                var totalprice = price1 + price2 + price3 + price4 + price5 + price6 + price7 + price8 + price9 + price10;
                Xrm.Page.getAttribute('new_partdescription').setValue(description);
                Xrm.Page.getAttribute('new_partdescription').setSubmitMode('always');
                Xrm.Page.getAttribute('new_price1').setValue(price);
                Xrm.Page.getAttribute('new_price1').setSubmitMode('always');
                setTimeout(setDescription, 1000);
                Xrm.Page.getAttribute('new_repairtotal').setValue(totalprice);
            }
        }
            else {
                var price1 = Xrm.Page.getAttribute('new_price1').getValue();
                var price2 = Xrm.Page.getAttribute('new_price2').getValue();
                var price3 = Xrm.Page.getAttribute('new_price3').getValue();
                var price4 = Xrm.Page.getAttribute('new_price4').getValue();
                var price5 = Xrm.Page.getAttribute('new_price5').getValue();
                var price6 = Xrm.Page.getAttribute('new_price6').getValue();
                var price7 = Xrm.Page.getAttribute('new_price7').getValue();
                var price8 = Xrm.Page.getAttribute('new_price8').getValue();
                var price9 = Xrm.Page.getAttribute('new_price9').getValue();
                var price10 = Xrm.Page.getAttribute('new_price10').getValue();
                var totalprice = price1 + price2 + price3 + price4 + price5 + price6 + price7 + price8 + price9 + price10;
                Xrm.Page.getAttribute('new_partdescription').setValue(null);
            Xrm.Page.getAttribute('new_price1').setValue(0.00);
            Xrm.Page.getAttribute('new_repairtotal').setValue(totalprice);
    
        } return;
    }
    

    Friday, June 6, 2014 5:14 PM

Answers

  • You can use the "Pass execution context as first parameter" option on your onChange event.

    Then you can do something like this in your function

    function setDescription(context) {

       var attrName = context.getEventSource().getName();

    }

    Or you can pass parameters to your functions:

    For onChange for product1: 'new_partdesription1','new_price1'
    For onChange for product2: 'new_partdesription2','new_price2'

    function setDescription(attrPart, attrPrice) {
      var existingProduct = Xrm.Page.getAttribute(attrPart).getValue();

    }

    • Proposed as answer by Nathan Eccles Saturday, June 7, 2014 1:00 PM
    • Marked as answer by Johnathan901 Sunday, June 8, 2014 9:00 PM
    Friday, June 6, 2014 5:57 PM

All replies

  • As you can see I am calling the total function twice, because when the else statement occurs the var totalprice clears. I hope I am saying this right :)
    Friday, June 6, 2014 5:17 PM
  • You can use the "Pass execution context as first parameter" option on your onChange event.

    Then you can do something like this in your function

    function setDescription(context) {

       var attrName = context.getEventSource().getName();

    }

    Or you can pass parameters to your functions:

    For onChange for product1: 'new_partdesription1','new_price1'
    For onChange for product2: 'new_partdesription2','new_price2'

    function setDescription(attrPart, attrPrice) {
      var existingProduct = Xrm.Page.getAttribute(attrPart).getValue();

    }

    • Proposed as answer by Nathan Eccles Saturday, June 7, 2014 1:00 PM
    • Marked as answer by Johnathan901 Sunday, June 8, 2014 9:00 PM
    Friday, June 6, 2014 5:57 PM
  • That did it. 

    In the onchange event I added the parameters 'new_part*','new_partdesciption*','new_price*' and changed my function to accept those parameters as you suggested. :)

    function setDesciption(attrPart,attrDesc,attrPrice){ blah blah}

    Much appreciated!

    Sunday, June 8, 2014 9:00 PM