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 :
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,512 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,287 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

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 > Service | Customer Service, Contact Center, Field Service, Guides

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 55 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 27 Most Valuable Professional

#3
Soundari Profile Picture

Soundari 15

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans