Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Not able to set lookup field value

(0) ShareShare
ReportReport
Posted on by 260

Hello,
I am not able to set lookup field value when writing the below code. I am getting the error that "Uncaught ReferenceError:Xrm is not defined". I have also added XrmServiceToolkit and ClientGlobalContext.js.aspx but still getting the same error. Please provide the solution if possible.


var recordname = $("#SelectEntityPrimaryName").val();
var recordnamevalue = $("#SelectEntityPrimaryName option:selected").text();
	var primaryrecord = new Array();
	primaryrecord[0] = new Object();
	primaryrecord[0].id = recordname;
	primaryrecord[0].name = recordnamevalue;
	primaryrecord[0].entityType = "new_event";
Xrm.Page.data.entity.attributes.get("regardingobjectid").setValue(primaryrecord);


*This post is locked for comments

  • Verified answer
    Drashti Mehta Profile Picture
    Drashti Mehta 260 on at
    RE: Not able to set lookup field value

    Thanks everyone,

    issue is resolved since i was using "regardingobjectid" i need to create object instead of array and pass logical name of entity.

  • Suggested answer
    RE: Not able to set lookup field value

    Hi Drashti,

    Check out these links, they might help..

    community.dynamics.com/.../142587

    social.microsoft.com/.../javascript-problems-with-my-first-web-resource-xrm-is-undefined

    Regards,

    Ezgi

    Please verify if it helps

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,076 on at
    RE: Not able to set lookup field value

    Hi Drashti,

    Please try the following code.

    var id = Xrm.Page.data.entity.attributes.get("primarycustomerid").getValue()[0].id;
    var name = Xrm.Page.data.entity.attributes.get("primarycustomerid").getValue()[0].name;
    var entityType = "Account";
    if (id != null) {
      var lookupVal = new Array();
      lookupVal[0] = new Object();
      lookupVal[0].id = guid;
      lookupVal[0].name = name;
      lookupVal[0].entityType = entityType;
     Xrm.Page.getAttribute("new_parentcustomerid2").setValue(lookupVal);
    }

    Thanks

  • Drashti Mehta Profile Picture
    Drashti Mehta 260 on at
    RE: Not able to set lookup field value

    Thanks Nithya Gopinath,

    But my field in not on the form. Actually i am saving the data in the entity from a web resource. So Xrm.Page.getAttribute() will not work. Henceforth, i used Xrm.Page.data.entity

  • Drashti Mehta Profile Picture
    Drashti Mehta 260 on at
    RE: Not able to set lookup field value

    Hello Saad Akhtar,

    Thanks for the reply i also tried this but its not working

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,076 on at
    RE: Not able to set lookup field value

    Hi Drashti,

    Try the following code.

    var recordname = $("#SelectEntityPrimaryName").val();
    var recordnamevalue = $("#SelectEntityPrimaryName option:selected").text();
    	var primaryrecord = new Array();
    	primaryrecord[0] = new Object();
    	primaryrecord[0].id = recordname;
    	primaryrecord[0].name = recordnamevalue;
    	primaryrecord[0].entityType = "new_event";
    Xrm.Page.getAttribute("regardingobjectid").setValue(primaryrecord);

    Also an alternate code is shown below.

    Xrm.Page.getAttribute("regardingobjectid").setValue([{ id:recordname, name:recordnamevalue, entityType:"new_event"}]);

    Hope it helps.

    Thanks

  • RE: Not able to set lookup field value

    try parent.Xrm.Page.data.entity.attributes.get("regardingobjectid").setValue(primaryrecord);

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,407 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans