Skip to main content

Notifications

Announcements

No record found.

Service | Customer Service, Contact Center, Fie...
Suggested answer

D365 - Get and Set lookup

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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
    Ram Prakash 2,285 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
    CRMJetty 3,508 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
    Community Member Microsoft Employee 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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans