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

Announcements

No record found.

News and Announcements icon
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!

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    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
    	}
    }

  • Suggested answer
    CRMJetty Profile Picture
    3,514 on at

    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
    Ram Prakash Duraisamy Profile Picture
    2,289 on at

    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
    }
    }

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
11manish Profile Picture

11manish 36

#2
Mallesh Deshapaga Profile Picture

Mallesh Deshapaga 32

#3
ManoVerse Profile Picture

ManoVerse 30 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans