Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Copy the text feild to lookup feild

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi

Is there any possibility to copy the text feild value to lookup feild using javascript or workflow ?

*This post is locked for comments

  • Lee-Martin Profile Picture
    Lee-Martin 511 on at
    RE: Copy the text feild to lookup feild

    Will this work with CRM 2011?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Copy the text feild to lookup feild

    And how would you do this as a workflow?

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Copy the text feild to lookup feild

    OK. But where does the link between them reside.

    Basically for this to work, your lookup entity (class code), would need to have a text field that contains the data for Class Id, so that you can retrieve the data.

    The above sample shows you how to do that.

    If you need to create the class/class code record, you will need to add additional logic to handle create.

    My answer above still applies. You will have to modify this to handles your own logic, but it should work.

    Don't forget to add a SDK.REST.js to your solution and to the form where you are calling this from.

    Good luck.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Copy the text feild to lookup feild

    Hi Aric ,

    I have entity called new_student

    In that entity i have two feilds called lookup feild (class code )and single line of text (class id). If i enter the value in text field it should autopopulate the lookup field using javascript

    Thanks

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Copy the text feild to lookup feild

    So, do you have an entity called class codes?

    How do you know what to put in the class code field based on class id?

    Are you planning to create a new record in the class code entity, or retrieve an existing value?

    As you are using CRM 2013, you can use SDK.REST API functions to retrieve or create values from the Class Code entity.

    My first suggestion is that you download the CRM Rest Builder by Jason Lattimer:

    github.com/.../CRMRESTBuilder

    Create a Request to Retrieve Multiple using the 2011 Endpoint (as you are using CRM 2013).

    Select your Primary Entity (new_ClassCode), select the Id and the name from the Fields to Select (this is in order to set the value in the lookup), in the Filter select new_ClassCode from the Entity, and select the attribute, eq and the value from your text box.

    Click on Create Request to Generate the Code:

    The following is the Generated Code, which I added to a function (assume new_ClassCode is the lookup entity name), the function to set the Lookup and the OnChange event done on the text box control:

    function text_classid_onchange()

    {

      var textValue = Xrm.Page.getAttribute("new_classid").getValue();

      GetLookupValue(textValue);

    }

    function GetLookupValue(classId)

    {

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/new_ClassCodeSet?$select=new_ClassCodeId,new_Name&$filter=new_ClassId eq '" + classId + "'", true);

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           this.onreadystatechange = null;

           if (this.status === 200) {

               var returned = JSON.parse(this.responseText).d;

               var results = returned.results;

               for (var i = 0; i < results.length; i++) {

                   var classCodeId = results[i].new_ClassCodeId;

                   var name = results[i].new_Name;

                   SetLookupValue("new_classcodeid", classCodeId, name, "new_classcode");

               }

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

    }

    function setLookupValue(fieldName, lookupId, lookupName, entityName)

    {

      var lookupData = new Array();

      var lookupItem = new Object();

      lookupItem.id = lookupId;

      lookupItem.name = lookupName;

      lookupItem.entityType = entityName;

      lookupData[0] = lookupItem;

      Xrm.Page.getAttribute(fieldName).setValue(lookupData);

    }

    If you make the required changes to this, and use CRM Rest Builder, you should be able to achieve your results.

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Copy the text feild to lookup feild

    Hi syed , Thanks for the reply

    I have an entity called student. In that entity i have 2 fields called class id and class code where class id is the single line text and class code is the lookup field . If i enter the value in class id (single line text )it should auto populate the lookup field which is class code . I need javascript for this scenerio. I tried to do with workflow but its not work for me , is there any idea please share it.

    Thanks,

  • Suggested answer
    Syed Ibrahim Profile Picture
    Syed Ibrahim 6,257 on at
    RE: Copy the text feild to lookup feild

    Yes we can copy text field value and create lookup records using both js and workflow .Can you pls provide more details what you are looking for.

    Try the below sample

    // define the data to create new account

    Var lookuptext =Xrm.page.getAttribute("new_customTextField").getValue();

    var data =

       {

           "name": lookuptext

       }

    // create account record

    Xrm.WebApi.createRecord("account", data).then(

       function success(result) {

           console.log("Account created with ID: " + result.id);

           // perform operations on record creation

       },

       function (error) {

           console.log(error.message);

           // handle error conditions

       }

    );

    Hope this helps

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,492 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans