Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Suggested answer

D365 - Get and Set lookup

(0) ShareShare
ReportReport
Posted on by

Hi guys, I'm having some difficulty trying to get and set a lookup value. I've created a button on a Case form which opens up a new Email and I want to set the Regarding of that email to the case it just came from.


I don't know if there's a more clever way to do this - currently, this is what I have:

var formContext = executionContext.getFormContext(); //get formContext

function getLookupValue(executionContext) {
	var lookupObject = formContext.getAttribute(incidentid).getValue(); //get the Lookup Object
		if (lookupObject != null) { 
			var lookupObjectValues = lookupObject.getValue();//get the Lookup Value 
		if (lookupObjectValues != null) {
			var RecordId = lookupObjectValues[0].id; //get the Lookup id 
			var RecordName = lookupObjectValues[0].name; //get the Lookup Name 
			var EntitySchemaName = lookupObjectValues[0].entityType; //get the Lookup EntityName 
		}
	}
}
		
function setLookupValue(executionContext, regardingid, RecordId, RecordName, EntitySchemaName) {
	if(regardingid != null)	{
		var lookupValue = new Array();
			lookupValue[0] = new Object();
			lookupValue[0].id = RecordId;
			lookupValue[0].name = RecordName;
			lookupValue[0].entityType = EntitySchemaName;

		formContext.getAttribute(regardingid).setValue(lookupValue);//set lookupValue from array
	}
}

When the button is pressed and the event handler runs, I get a script error:

TypeError: Cannot read property 'getAttribute' of undefined


I feel like I must be missing something fundamental - I've gone through a few iterations with this (I was using the old Xrm.Page before - whoops!) but any advice would be very much appreciated!

  • Suggested answer
    Ram Prakash Profile Picture
    2,287 on at
    RE: D365 - Get and Set lookup

    Hi JVicente,

    Make Sure in the Ribbon workbench, while doing COMMAND, Make sure you are passing CRM Parameter as "PRIMARYCONTROL"

    then

    Try changing the code like below

    function getLookupValue(executionContext) {
    var formContext = executionContext;

    var lookupObject = formContext.getAttribute(incidentid).getValue(); //get the Lookup Object
    if (lookupObject != null) {
    var lookupObjectValues = lookupObject.getValue();//get the Lookup Value
    if (lookupObjectValues != null) {
    var RecordId = lookupObjectValues[0].id; //get the Lookup id
    var RecordName = lookupObjectValues[0].name; //get the Lookup Name
    var EntitySchemaName = lookupObjectValues[0].entityType; //get the Lookup EntityName

    setLookupValue(formContext , regardingid, RecordId, RecordName, EntitySchemaName)
    }
    }
    }

    function setLookupValue(formContext , regardingid, RecordId, RecordName, EntitySchemaName) {
    if(regardingid != null) {
    var lookupValue = new Array();
    lookupValue[0] = new Object();
    lookupValue[0].id = RecordId;
    lookupValue[0].name = RecordName;
    lookupValue[0].entityType = EntitySchemaName;

    formContext.getAttribute(regardingid).setValue(lookupValue);//set lookupValue from array
    }
    }

  • Suggested answer
    CRMJetty Profile Picture
    3,512 on at
    RE: D365 - Get and Set lookup

    Hello,

    Pls refer this article,

    https://blog.magnetismsolutions.com/blog/adammurchison/2017/09/20/how-to-get-and-set-a-lookup-field-using-javascript-in-dynamics-365

    I hope it helps,

    Thanks.

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: D365 - Get and Set lookup

    Hi,

    According to the error message, the value of  formContext is undefined.

    You can try the following code(I created a on load event on email form and it can get the lookup value)

    var formContext; 
    
    function getLookupValue(executionContext) {
        formContext = executionContext.getFormContext(); //get formContext
    	var lookupObject = formContext.getAttribute("incidentid"); //get the Lookup Object
    		if (lookupObject != null) { 
    			var lookupObjectValues = lookupObject.getValue();//get the Lookup Value 
    		if (lookupObjectValues != null) {
    			var RecordId = lookupObjectValues[0].id; //get the Lookup id 
    			var RecordName = lookupObjectValues[0].name; //get the Lookup Name 
    			var EntitySchemaName = lookupObjectValues[0].entityType; //get the Lookup EntityName 
    		}
    	}
    }
    		
    function setLookupValue(executionContext, regardingid, RecordId, RecordName, EntitySchemaName) {
    	if(regardingid != null)	{
    		var lookupValue = new Array();
    			lookupValue[0] = new Object();
    			lookupValue[0].id = RecordId;
    			lookupValue[0].name = RecordName;
    			lookupValue[0].entityType = EntitySchemaName;
    
    		formContext.getAttribute("regardingid").setValue(lookupValue);//set lookupValue from array
    	}
    }

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

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 51 Most Valuable Professional

#2
Ramesh Kumar Profile Picture

Ramesh Kumar 42

#3
David Shaw_UK Profile Picture

David Shaw_UK 27

Featured topics

Product updates

Dynamics 365 release plans