Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Getting the value of a control on a form

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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

  • Verified answer
    Royal King Profile Picture
    Royal King 27,686 on at
    RE: Getting the value of a control on a form

    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 );

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting the value of a control on a form

    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.

  • Jitendra Sahu Profile Picture
    Jitendra Sahu 2 on at
    RE: Getting the value of a control on a form

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

  • Suggested answer
    Hosk Profile Picture
    Hosk on at
    RE: Getting the value of a control on a form

    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

  • Suggested answer
    Royal King Profile Picture
    Royal King 27,686 on at
    RE: Getting the value of a control on a form

    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);

    }
    }
    }







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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans