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 CRM (Archived)

Getting the value of a control on a form

(0) ShareShare
ReportReport
Posted on by

I'm trying to get the value of a control on a form.  I want the data that has been entered, not the value of the underlying data which hasn't been updated yet.  For example,

Xrm.Page.getAttribute(myfield).getValue() returns the value of the field prior to editing, or if blank returns Object.object

Xrm.Page.getControl(myfieldcontrol), gets me the control, but no value

What am I missing?

Don

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Royal King Profile Picture
    27,686 on at

    You can check whether field value is modified by user or not using below script

    Xrm.Page.getAttribute(arg).getIsDirty()

    If it returns true that means user modified the data in the form and you can retrieve
    actual data using Odata service for that field. If the return value is false then you can use whatever data it returns.

    Here is the same code to retrieve data using odata

    function getAccount() {
    try {
    var serverUrl;
    /* This is the heart and soul of the whole thing - the actual oData call. */
    serverUrl = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'" + Xrm.Page.data.entity.getId() + "')"; // Replace "AccountSet" with your ENtity Name schema.
    serverUrl +="$select=FullName,JobTitle"; // Replace field names with schema field names of yours
    var request = new XMLHttpRequest();
    request.open("GET", serverUrl, true);
    request.setRequestHeader("Accept", "application/json");
    request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    /* Next, we specify the function that we want called when the XMLHttpRequest state
    changes. This will get called on each state change. */
    request.onreadystatechange = function () {
    requestComplete(request);
    }
    request.send();
    } catch (e) {
    // You probably want to do something other than an alert here.
    alert(e.Description);
    }
    }

    function requestComplete(request) {
    /* A request.readyState of 4 means the request is complete. If
    it's complete and the status is 200 (OK), then we can assign the results
    of the call to our controls on the form.*/
    if (request.readyState == 4 && request.status == 200) {
    // debugger;
    var json = $.parseJSON(request.responseText);
    if ((json != undefined) && (json.d != undefined) && (json.d.results != undefined) && (json.d.results[0] != null)) {
    var xp1 = json.d.results[0];
    // Note that the way things are set up, it's possible to have more
    // than one "Support Contact" returned but I'm hard coding the results
    // to return the first contact only.
    Xrm.Page.getAttribute("FullName").setValue(xp1.JobTitle);
    Xrm.Page.getAttribute("new_mostrecentphone").setValue(xp1.FullName);

    }
    }
    }







  • Suggested answer
    Hosk Profile Picture
    on at

    If you put an on change event on a field then your JavaScript will be triggered when the value changes

    You say it gets the value, which hadn't been updated but I don't really understand the scenario you are saying this, please clarify

  • Jitendra Sahu Profile Picture
    2 on at

    What is the datatype of your field for which you want to retrieve data?

  • Community Member Profile Picture
    on at

    Sorry - I wasn't very clear initially.  Here is the scenario:

    A user brings up a form for to edit an existing record.  They change the value from Old1 to New1 in FieldControl1.  They then move on to FieldControl2, where they change that from Old2 to New2.  In the onChange event of FieldControl2 I have code that is attempting to read those two values, concatenate them, and create a value (Val3) that I put into FieldControl3.  It looks like this.

    var Val1 = Xrm.Page.getAttribute(FieldControl1).getValue();  //should be New1

    var Val2 = Xrm.Page.getAttribute(FieldControl2).getValue();  //should be New2

    var Val3 = Val1 + "-" + Val2;

    The problem is that Val1 and Val2 are giving me Old1 and Old2, respectively, not New1 and New2.  However, I have found that this is sporadic, dependent on whether the auto-save has completed or not.

  • Verified answer
    Royal King Profile Picture
    27,686 on at

    Add Onchange event on FieldControl1 and FieldControl2  so that as soon as value changes in any one of the filed, Val3 gets updated with new Value.

    Add Below script on your onload to attach on-change event to the controls..

    Xrm.Page.getAttribute(FieldControl1).addOnChange(setValue3);

    Xrm.Page.getAttribute(FieldControl2).addOnChange(setValue3);

    function setValue3()

    {

    var Val1 = Xrm.Page.getAttribute(FieldControl1).getValue();  //should be New1

    var Val2 = Xrm.Page.getAttribute(FieldControl2).getValue();  //should be New2

    var Val3 = Val1 + "-" + Val2;

    Xrm.Page.getAttribute(FieldControl3).setValue(Val3 );

    }

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans