locked
CRM 2011 - Javascript - calculated field based on lookups RRS feed

  • Question

  • Hi, I am very new to javascript and need some help getting a function to work please....

    I have 3 lookup fields of user type, for fields called Introducer, Account Manager (actually the owner field renamed), and Closer. I need to use these in a formula that calculates the total commission for a salesperson depending on what stage of the sale they are involved in.

    For example, Joe Bloggs is the Account manager and will earn the related commission for the account manager part of the sale. He is also the Introducer, so will also earn this commission. I therefore want it to add the Account manager Commission to the Introducer Commission and return it in the field called TotalCommForAM. If he was also the Closer, then I would want to add up all three values.

    Please forgive the crudeness of the code, I have pieced it together from various examples I've found on the web!

    function calctotalamcomm()   


    var amcomm = Xrm.Page.getAttribute("new_amcommamount").getValue();
    var introcomm = Xrm.Page.getAttribute("new_introcommamount").getValue();
    var closercomm = Xrm.Page.getAttribute("new_closercommamount").getValue();
    var totalcommforAM = Xrm.Page.getAttribute("new_totalcommforAM").getValue();

    //get accmgr lookup details

        var accmgr = new Array(); 
        accmgr = Xrm.Page.getAttribute("ownerid").getValue();

        if (accmgr !== null) 
        {

            var name = accmgr[0].name; 
            var guid = accmgr[0].id; 
        }

    //get closer details

        var closer = new Array(); 
        closer = Xrm.Page.getAttribute("new_closer").getValue();

        if (closer !== null) 
        {

            var closername = closer[0].name; 
            var closerguid = closer[0].id; 

        }

    //get intro details

        var intro = new Array(); 
        intro = Xrm.Page.getAttribute("new_introducer").getValue();

        if (intro !== null) 
        {

            var introname = intro[0].name; 
            var introguid = intro[0].id; 

        }




    if(closername.getvalue() = name.getvalue())
    {
    var totalclosercomm = closercomm;
    }
    else
    {
    var totalclosercomm = 0;
    }

    if(introname.getvalue() = name.getvalue())
    {
    var totalintrocomm = introcomm;

    else
    {
    var totalintrocomm = 0;
    }

    totalcommforAM = amcomm + totalclosercomm + totalintrocomm;
    Xrm.Page.getAttribute("new_totalcommforAM").setValue(totalcommforAM;


    }

    Monday, April 23, 2012 1:52 PM

Answers

  • What errors coming now when you enter the above code on form.
    Last thing please check your schema names.
    Is they right?

    if any of the lookup is null your code generates an error because closername is defined in != null condition and
    for the below code its undefined
    if (closername == name) {
            var totalclosercomm = closercomm;
        }

    before running my code make sure none of the lookup is null means (Empty);

    if your code does not work please give me a call on skype

    if you need more information please let me know.


    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!
    Skype Id: mubasher.sharif

    Monday, April 23, 2012 9:51 PM

All replies

  • Please tell me what are the attributes in your code i.e new_amcommamount = lookup;
    You have alot of syntax error in your code.
    Please use the below code and please tell me where is name.getvalue() is defined?
    First of all define name if name is simple variable define it like. var name = "test";
    you could not set Lookup in text box; As you are doing it in your code.

    function calctotalamcomm()   

    {

    var amcomm = Xrm.Page.getAttribute("new_amcommamount").getValue();
    var introcomm = Xrm.Page.getAttribute("new_introcommamount").getValue();
    var closercomm = Xrm.Page.getAttribute("new_closercommamount").getValue();
    var totalcommforAM = Xrm.Page.getAttribute("new_totalcommforAM").getValue();

    //get accmgr lookup details

        var accmgr = new Array();
        accmgr = Xrm.Page.getAttribute("ownerid").getValue();

        if (accmgr != null)
        {

            var name = accmgr[0].name;
            var guid = accmgr[0].id;
        }

    //get closer details

        var closer = new Array();
        closer = Xrm.Page.getAttribute("new_closer").getValue();

        if (closer != null)
        {

            var closername = closer[0].name;
            var closerguid = closer[0].id;

        }

    //get intro details

        var intro = new Array();
        intro = Xrm.Page.getAttribute("new_introducer").getValue();

        if (intro != null)
        {

            var introname = intro[0].name;
            var introguid = intro[0].id;

        }




    if(closername == name)
    {
    var totalclosercomm = closercomm;
    }
    else
    {
    var totalclosercomm = 0;
    }

    if(introname == name)
    {
    var totalintrocomm = introcomm;
    }
    else
    {
    var totalintrocomm = 0;
    }

    totalcommforAM = amcomm + totalclosercomm + totalintrocomm;
    Xrm.Page.getAttribute("new_totalcommforAM").setValue(totalcommforAM);


    }
    If you need more information please let me know

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!



    Monday, April 23, 2012 2:57 PM
  • Hi, thanks very much for the response...

    Have tried the revised code and get the following message on load

    

    In answer to your question:

    new_amcommamount, new_introcommamount, new_closercommamount, new_totalcommforAM are all currency type fields. ownerid, new_introducer, new_closer are all lookup fields of user type.

    What do you mean by "where is name.getvalue() is defined?" 

    I have copied and pasted sections of code here which will be why I've got a few syntax errors - please feel free to tear it to bits if you think it is wrong :-)

    I've used this guide for getting a lookup value http://mileyja.blogspot.co.uk/2011/05/how-to-get-and-set-lookup-value-in.html by the way - is there a better way?

    Monday, April 23, 2012 3:43 PM
  • Hi Businessworks,

    I suggest put following statement at the top of script

    debugger;

    and go to IE -> Tools -> Internet options -> Advanced tab -> un tick disable script debugging options..

    after this run your code and it will ask you debug, so you can debug your java script line by line and check which line got issue and fix that line..

    hope this helps..


    MayankP
    My Blog
    Follow Me on Twitter

    Monday, April 23, 2012 4:05 PM
  • function calctotalamcomm() {

        var amcomm = Xrm.Page.getAttribute("new_amcommamount").getValue();
        var introcomm = Xrm.Page.getAttribute("new_introcommamount").getValue();
        var closercomm = Xrm.Page.getAttribute("new_closercommamount").getValue();
        var totalcommforAM = Xrm.Page.getAttribute("new_totalcommforAM").getValue();
        //get accmgr lookup details
        var accmgr = new Array();
        accmgr = Xrm.Page.getAttribute("ownerid").getValue();
        if (accmgr != null) {
            var name = accmgr[0].name;
            var guid = accmgr[0].id;
        }
        //get closer details
        var closer = new Array();
        closer = Xrm.Page.getAttribute("new_closer").getValue();
        if (closer != null) {
            var closername = closer[0].name;
            var closerguid = closer[0].id;
        }
        //get intro details
        var intro = new Array();
        intro = Xrm.Page.getAttribute("new_introducer").getValue();
        if (intro != null) {
            var introname = intro[0].name;
            var introguid = intro[0].id;
        }
        if (closername == name) {
            var totalclosercomm = closercomm;
        }
        else {
            var totalclosercomm = 0;
        }
        if (introname == name) {
            var totalintrocomm = introcomm;
        }
        else {
            var totalintrocomm = 0;
        }
        totalcommforAM = amcomm + totalclosercomm + totalintrocomm;
        Xrm.Page.getAttribute("new_totalcommforAM").setValue(totalcommforAM);

    }
    Use this code and you are getting this error because you are not defining name any where.
    Please define name.
    Like
    var name = 'Mubasher Sharif';
    Jamie Miley has explained very well in his blog for lookup and you are using it in right manner.

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!



    Monday, April 23, 2012 6:39 PM
  •  I'm a little confused now... name is defined in the code, isnt it?

    if (accmgr != null) {

            var name = accmgr[0].name;
            var guid = accmgr[0].id;

    Monday, April 23, 2012 8:43 PM
  • What errors coming now when you enter the above code on form.
    Last thing please check your schema names.
    Is they right?

    if any of the lookup is null your code generates an error because closername is defined in != null condition and
    for the below code its undefined
    if (closername == name) {
            var totalclosercomm = closercomm;
        }

    before running my code make sure none of the lookup is null means (Empty);

    if your code does not work please give me a call on skype

    if you need more information please let me know.


    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!
    Skype Id: mubasher.sharif

    Monday, April 23, 2012 9:51 PM
  • Final code below - tested and working! Thanks very much for the help

    function calctotalamcomm() {

        var amcomm = Xrm.Page.getAttribute("new_amcommamount").getValue();
        var introcomm = Xrm.Page.getAttribute("new_introcommamount").getValue();
        var closercomm = Xrm.Page.getAttribute("new_closercommamount").getValue();
        var totalcommforam = Xrm.Page.getAttribute("new_totalcommforam").getValue();

      //get accmgr lookup details
        var accmgr = new Array();
        accmgr = Xrm.Page.getAttribute("ownerid").getValue();
        if (accmgr != null) {
            var accmgrname = accmgr[0].name;
            var accmgrguid = accmgr[0].id;
        }

        //get closer details
        var closer = new Array();
        closer = Xrm.Page.getAttribute("new_closer").getValue();
        if (closer != null) {
            var closername = closer[0].name;
            var closerguid = closer[0].id;
        }

        //get intro details
        var intro = new Array();
        intro = Xrm.Page.getAttribute("new_introducer").getValue();
        if (intro != null) {
            var introname = intro[0].name;
            var introguid = intro[0].id;
        }

    if (closername = accmgrname) {
            var totalclosercomm = closercomm;
        }
        else {
            var totalclosercomm = 0;
        }
        if (introname = accmgrname) {
            var totalintrocomm = introcomm;
        }
        else {
            var totalintrocomm = 0;
        }
        totalcommforam = amcomm + totalclosercomm + totalintrocomm;
        Xrm.Page.getAttribute("new_totalcommforam").setValue(totalcommforam);

    }

      
    Monday, April 23, 2012 10:25 PM