web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Set lookup field value

(0) ShareShare
ReportReport
Posted on by

I'm trying to set lookup field value using javascript. My script gives me "An error has occurred. Please return to the home page and try again", but everything works well. This is my code:

var lookupVal = new Array();
    lookupVal[0] = new Object();
    lookupVal[0].id = lookup.savedEntityReference.id;
    lookupVal[0].name = lookup.savedEntityReference.name;
    lookupVal[0].entityType = "new_agreement";
    var existingValue = Xrm.Page.getAttribute("new_agreement").getValue();
    if (existingValue === null) {
        Xrm.Page.getAttribute("new_agreement").setValue(lookupVal); //error is in this row
    }

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    Everything in your script to build the lookup is correct, I don't think you're supposed to have three equals for the comparison but that's not the issue. The only thing that looks like it could be a problem is the "lookup.savedEntityReference" object. If that's not evaluating the way you expect then it would explain why the setValue is failing. Put in alerts to verify those values are what you'd expect, but I'd bet that's what the problem is.

    Hope this helps! I'd appreciate if you'd mark this as a Verified answer.

    Thanks,

     Aiden

  • Suggested answer
    Aileen Gusni Profile Picture
    44,524 on at

    Hi Taras

    It could be a problem here

    lookup.savedEntityReference.id;

       lookupVal[0].name = lookup.savedEntityReference.name;

    Do you have the lookup.savedEntityReference.name

    And you set the value to new_agreement with the lookup value to the entity new_agreement also?

    1. Are you sure the entity type name for the lookup field is new_agreement

    2. Are you sure that the field name is new_agreement not new_agreementid?

    THAnks

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Taras,

    Please use below function to Set LookUp field. Pass the required parameter in SetLookupValue function. Please make sure you pass correct parameters.

    function SetLookupValue(fieldName, id, name, entityType)

    {

      if (fieldName != null)

      {

           var lookupValue = new Array();

           lookupValue[0] = new Object();

           lookupValue[0].id = id;

           lookupValue[0].name = name;

           lookupValue[0].entityType = entityType;

           if (lookupValue[0].id != null)

    {

               Xrm.Page.getAttribute(fieldName).setValue(lookupValue);

           }

       }

    }

    Hope this Helps !! Let us know if you need any more help on this.

    I would appreciate if you can mark answer as verified if it helped.Thanks

    Regards,

    Kamran

  • Community Member Profile Picture
    on at

    Hi TarasTComTech,

    Are you able to get on with your requirement..

    Regards,

    Kamran

  • Suggested answer
    Saddamk206 Profile Picture
    777 on at

    Please try this. it will helpfull to you

    Xrm.Page.getAttribute("Field Name").setValue([{ id: recorid, name: recordname, entityType: entityname}]);

  • Community Member Profile Picture
    on at

    I am trying to apply an account value to a lookup field

    I am calling this javascript on page load event of the entity 'testEntity'

    my code is

    function setLookupField(){

    var lookUpData =new Array();

    var lookUpItem =new Object();

    lookUpItem.id ='7b624C940A-49FF-E711-80FC-XXXXXXXXXXXX';

    lookUpItem.name ='myaccountname';

    lookUpItem.entityType ='account';

    lookUpData[0]=lookUpItem;

    Xrm.Page.getAttribute('new_lookupfield').setValue(lookUpData);

    }

    It is setting the value on page load but when I save the record it is throwing error, am I doing something wrong?

  • Suggested answer
    Saddamk206 Profile Picture
    777 on at

    I hope it will helpfull for you

    function setLookupField(){

    var lookUpItem =new Object();

    lookUpItem.id ='7b624C940A-49FF-E711-80FC-XXXXXXXXXXXX';

    lookUpItem.name ='myaccountname';

    lookUpItem.entityType ='account';

    Xrm.Page.getAttribute("Field Name").setValue([{ id: lookUpItem.id, name: lookUpItem.name, entityType: lookUpItem.entityType}]);

    }

  • Suggested answer
    Community Member Profile Picture
    on at
  • Kamaldeep Singh Profile Picture
    80 on at

    Hi Kamran,

    Is it mandatory to pass the GUID of the Lookup value?

  • Saddamk206 Profile Picture
    777 on at

    Kamaldeep, it is mandatory to pass the GUID of the Lookup value.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans