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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

How to copy single line of text value to a lookup field using javascript

(0) ShareShare
ReportReport
Posted on by 25

Hi community,

Hope all are doing well during this pandemic.

I am having oob country field which is single line of text and want to save this value into custom lookup field using javascript.

Can anyone please provide the code.

I have the same question (0)
  • Kruthi2704 Profile Picture
    25 on at
    RE: How to copy single line of text value to a lookup field using javascript

    Hi Sam,

    I have two fields on the same form one is address1_country(single line of text) and country(lookup)

    Now the scenario is when the lookup is empty it has to retrieve single line of text to lookup

  • Inogic Profile Picture
    643 on at
    RE: How to copy single line of text value to a lookup field using javascript

    It seems like you are trying to get the value of the text field and set it to the lookup field on the CRM form. Well, it is not possible to set the single line of text field's value (i.e. string) to the lookup field in Dynamics 365 CRM.

    But assuming you are having a country entity and want to set the lookup of the country based on the value entered in the text field.

    So, in that case, you can first retrieve the country record using Xrm.WebApi.retrieveMultipleRecords() method based on the name entered in text field.
    You can use this retrieved record id, record name, and entity type to set the lookup field value.

    Note: You need to make sure that the value entered in the text field must be present in the country entity records. Else, it will not return any result.

    The below code shows how to set the lookup field value using JavaScript in Dynamics 365 CRM:
    var setLookupValue = [];
    setLookupValue[0] = new Object();
    setLookupValue[0].id = recordId;
    setLookupValue[0].name = recordName;
    setLookupValue[0].entityType = entityLogicalName ;
    Xrm.Page.getAttribute("LookupFiels_LogicalName").setValue(setLookupValue );

    You can see the following link to know more about how to retrieve records using Xrm.WebApi.retrieveMultipleRecords :

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/retrievemultiplerecords

    Hope this helps.

    Thanks!

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
DAnny3211 Profile Picture

DAnny3211 126

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 117

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 70 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans