locked
Javascript to populate lookup field crashing RRS feed

  • Question

  • Hi everyone,

    What I have is a script for Account create/update that when the zipcode is entered it then auto-populates the sales Territory lookup.  Everything works fine and dandy; I enter the zip code, the lookup is filled, looks the same as if I were to manually select it from the list.

    The problem is when I try to save the Account with the Territory that was selected and populated with Javascript I get a generic error and it doesn't let me save; if I was to manually select the Territory it does save.

    Are there any known issues with populating a lookup with an entity through script?

    Below is the small code section that actually adds it to the field

                var lookup = new Object();
                var lookupValue = new Array();
                //set values that will go into the lookup in the form
                lookup.id = territory;
                lookup.entityType = "territory";
                lookup.name = territoryName;
                lookupValue[0] = lookup;

                //set city and territory on the form
                Xrm.Page.getAttribute("territoryid").setValue(lookup);

    Thanks

    Friday, April 19, 2013 1:44 PM

Answers

  • Sorry Everyone,

    I solved it after some extensive research, I was setting lookup.id to the Territory number and not its GUID, when it should be the GUID.

    • Marked as answer by Bmckie Friday, April 19, 2013 2:24 PM
    Friday, April 19, 2013 2:24 PM

All replies

  • Sorry Everyone,

    I solved it after some extensive research, I was setting lookup.id to the Territory number and not its GUID, when it should be the GUID.

    • Marked as answer by Bmckie Friday, April 19, 2013 2:24 PM
    Friday, April 19, 2013 2:24 PM
  • Hi, actually your code has some typo (need to be setValue(lookupValue) instead of setValue(lookup)) however the best guess is that the id you put inside the lookup doesn't exist.
    When you set a lookup in this way CRM doesn't check the values, you can try to change the name to Goofy and a fake id, you will see the lookup looks fine until you get the same error when you save.
    Check the ID you are passing to the code, another thing, rewrite the code in this form, is more readable:

    var value = new Array();
    value[0] = new Object();
    value[0].id = territory;
    value[0].name = territoryName;
    value[0].entityType = "territory";
    Xrm.Page.getAttribute("territoryid").setValue(value);
    EDIT: sorry, I saw your solution after I submit my reply.


    My blog: www.crmanswers.net



    Friday, April 19, 2013 2:30 PM
  • I also did forget to mention, when I was debugging before I submitted the question I changed the value to lookup instead of lookupValue and forgot to put that clarification in my reply. Thank you for the your help though.
    Friday, April 19, 2013 5:17 PM