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
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 );
}
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.
What is the datatype of your field for which you want to retrieve data?
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
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);
}
}
}
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,113 Super User 2024 Season 2
Martin Dráb 229,918 Most Valuable Professional
nmaenpaa 101,156