Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Getting Text Value w/ JavaScript

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Is there a supported way to get the text value of a field within CRM?  For instance, I want to know if the user has entered any text in a field before I execute a function, so I would normally check via:

var toUserText = document.getElementById('to_ledit_multi').value;

However, I understand that this is not supported in CRM.  Is there a supported way to get the text value?  I know I can get the actual saved value, but that is not what I am looking for here.  Thanks.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Text Value w/ JavaScript

    Thank you for the proposed solutions.  Unfortunately these are all dependent on either a valid value being saved and posted back to the database, or unsupported JavaScript.  I guess we can throw all of our web standards out of the window when working with CRM, IE, SharePoint, etc....

  • Shh Profile Picture
    Shh on at
    RE: Getting Text Value w/ JavaScript

    To get the value of the lookup you can do by this way

    var LookupID = Xrm.Page.data.entity.attributes.get("lookupfieldname").getValue()[0].id;

    var LookupText = Xrm.Page.data.entity.attributes.get("lookupfieldname").getValue()[0].name;

  • Pramod M Profile Picture
    Pramod M 1,445 on at
    RE: Getting Text Value w/ JavaScript

    The below code gives you selected valid values from a multi lookup ('To' lookup).

    var ToFielValue = Xrm.Page.getAttribute('to');

    // var Textvalue =ToFielValue.controls.$5_0[0].$2_0[0].innerText

    var ToDetails = ToFielValue.getValue();

    for (var indxTo = 0; indxTo < ToDetails.length; indxTo++) {

       alert('selected value - ' + ToDetails[indxTo].id + ' Name -' + ToDetails[indxTo].name);

    }

    Since there are multiple values you need to loop through the object to get all the data.

    This will work if you change data values also if the lookup is able to resolve the values, if there is an invalid value you need to remove the same from the lookup before you get the updated value( this invalid values are shown in red color).

    There are ways to get text value of the lookup. The commented line in the above code gives you the control value but you need to manipulate the text received to get the data (simple to do). But I think these will be unsupported way of getting data.

    So the best way is to get data using above supported code.

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Text Value w/ JavaScript

    I am trying to get the text value of the "To" lookup on the "Phone Call" activity form.  Below is the code that I am using to do so, which does not provide the text value.  I have also tried to get the text value of the actual HTML element 'to_ledit_multi' which is the name of the input element associated with the 'To" lookup on the form and which is apparently an ephemeral element:

    function addLookupFilter() {

       var lookupTextValue;

       var toUser = Xrm.Page.getAttribute('to');

       if (toUser != null) {

           var lookup = toUser.getValue();

           if (lookup != null) {

               lookupTextValue = lookup[0].name;

           }

       }

  • Pramod M Profile Picture
    Pramod M 1,445 on at
    RE: Getting Text Value w/ JavaScript

    what filed you are trying to get the value? are you using crmtext box or a lookup?

    And can you please post your code here so that I can help you.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Text Value w/ JavaScript

    I have tried this, and it works great the first time I enter a value.  However, if I remove that value and type a new value, the name of the text box control changes from 'to_ledit_multi' to "to_i_ledit_multi" and 'to_ledit_multi' is no longer accessible on the page, which causes an error to be thrown.  I understand that I can defensively code around this error, but that seem rather hack-ish, and not knowing when or what MS will change the control id to next is unsettling.  I was under the assumption that this is the main reason for Microsoft not supporting coding to actual DOM elements in CRM, but I don't see an obvious means of extracting the actual text value of a page element with the Xrm tools.

  • Pramod M Profile Picture
    Pramod M 1,445 on at
    RE: Getting Text Value w/ JavaScript

    Hi,

    Xrm.Page.getAttribute('your textbox name').getValue() should give you the current value which is entered in text box, if your current text box value is different from saved value.

    Please let me know if this helps you.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Text Value w/ JavaScript

    Is there a supported way to get the text value of a field within CRM?  For instance, I want to know if the user has entered any text in a field before I execute a function, so I would normally check via:

    var toUserText = document.getElementById('to_ledit_multi').value;

    However, I understand that this is not supported in CRM.  Is there a supported way to get the text value?  I know I can get the actual saved value, but that is not what I am looking for here.  Thanks.

  • Suggested answer
    Pramod M Profile Picture
    Pramod M 1,445 on at
    RE: Getting Text Value w/ JavaScript

    For Lookup

    var lookupObject = Xrm.Page.getAttribute("to_ledit_multi");

       if (lookupObject != null)

     {

           var lookup = lookupObject.getValue();

           if ((lookup != null))

           {

            var lookuptextvalue = lookup[0].name;

            var lookupid =lookup[0].id;

           var type = lookup[i].entityType;

           }

     }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Text Value w/ JavaScript

    Does this not work for lookup fields? Unless I have saved the record, the getDetails method only returns null.

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…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,971 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,846 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans