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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Set Lookup field with text field value using JS

(0) ShareShare
ReportReport
Posted on by 160

Hello,

I'm attempting to set a lookup field on a form with a text field value on the same form using JS. My scenario is that I'm writing a related record's GUID to a text field on my main form, and the trying to set a lookup field using the GUID value from the text field. I've attempted to build out the JS using a lookup array, but I receive an onload alert stating 'Entity Type not defined'.  Reason being is my formContext.getAttribute is a text field and not a lookup field. If I change out the getAttribute to a lookup it works fine b/c I'm defining the id/name/entity type. Is there any way to do this?? I keep spinning in circles.  Also, I'm currently using on-premise so I can't use Flow, this has to be done using .JS

Any advice would be greatly appreciated!! Thanks

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

    Hi kcpacker85,

    Please try the following code:

    function onload(execuationContext){
        var formContext = execuationContext.getFormContext();
    	var accountGUID = formContext.getAttribute("cr1bc_accountguid").getValue();
        console.log(accountGUID);
    	if(accountGUID){
    		var accountNameLookUpArray = new Array();
    		var accountNameLookUp = new Object();
    		accountNameLookUp.id = accountGUID;
            accountNameLookUp.name = "Steve"
    		accountNameLookUp.entityType = "account";
    		accountNameLookUpArray[0] = accountNameLookUp;
    		formContext.getAttribute("parentcustomerid").setValue(accountNameLookUpArray);
    	}
    }

    It can works for me:

    pastedimage1657693567193v1.png

    If you don't know the name property, you could send a request to get it and then set the value of lookup field in the call back function:

    docs.microsoft.com/.../retrieverecord

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    You would need to store/know related entity name to set the Lookup field.

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
#ManoVerse Profile Picture

#ManoVerse 93

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 62 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans