Hi
I've written the following to update fields on the opportunity with data from a related entity. This should happen on OnChange, but the field are not populated before the form has been saved
Can you see what is wrong?
function retrieveServiceAvailabilityInfo(executionContext) { var city; var postalCode; var province; var serviceArea; //var policeGuard; formContext = executionContext.getFormContext(); var serviceAvailabilityEntity = formContext.getAttribute('cgi_serviceavailability'); var sAEID = 'F0A708D3-9700-EA11-A811-000D3AF3A041'; //serviceAvailabilityEntity.getValue()[0].id; Xrm.WebApi.retrieveRecord('cgi_serviceavailability', sAEID,'?$select=cgi_city,cgi_postalcode,cgi_province,cgi_servicearea').then( function success(result) { city = result.cgi_city; postalCode = result.cgi_postalcode; province = result.cgi_province; serviceArea = result.cgi_servicearea; //policeGuard = result.cgi_policeguard; var data = { 'cgi_city': city, 'cgi_postalcode': postalCode, 'cgi_province': province, 'cgi_servicearea': serviceArea } Xrm.WebApi.updateRecord('opportunity',formContext.data.entity.getId(), data).then(successCallback, errorCallback); formContext.data.entity.save(); }, function (error) { alert('Error'); } ); }
Brilliant, thank you for the answer. I just guessed that setValue wasnt possible any longer, but of course it is :)
As Opportunity also have that field city(cgi_city),postalcode(cgi_postalcode),province('cgi_province' ),serviceArea('cgi_servicearea') with same schema name.
I don't Understand then you use that Xrm.WebApi.Update Function . NO need for that Just use setValue like
formContext.getAttribute(
"cgi_city"
).setValue(
city
); if city have datatype text.
For Other Field dataType Check this link:
On Change or OnLoad in both case just use setValue() to set all field
function retrieveServiceAvailabilityInfo(executionContext)
{
var city;
var postalCode;
var province;
var serviceArea;
//var policeGuard;
formContext = executionContext.getFormContext();
var serviceAvailabilityEntity = formContext.getAttribute('cgi_serviceavailability');
var sAEID = 'F0A708D3-9700-EA11-A811-000D3AF3A041'; //serviceAvailabilityEntity.getValue()[0].id;
Xrm.WebApi.retrieveRecord('cgi_serviceavailability', sAEID,'?$select=cgi_city,cgi_postalcode,cgi_province,cgi_servicearea').then(
function success(result)
{
city = result.cgi_city;
postalCode = result.cgi_postalcode;
province = result.cgi_province;
serviceArea = result.cgi_servicearea;
//policeGuard = result.cgi_policeguard;
/*var data =
{
'cgi_city': city,
'cgi_postalcode': postalCode,
'cgi_province': province,
'cgi_servicearea': serviceArea
}*/
// if all four fields are text Field
formContext.getAttribute(
"cgi_city"
).setValue(
city
);
formContext.getAttribute(
"cgi_postalcode"
).setValue(postalCode
);
formContext.getAttribute(
"cgi_province"
).setValue(province
);
formContext.getAttribute(
"cgi_servicearea"
).setValue(serviceArea
);
//Xrm.WebApi.updateRecord('opportunity',formContext.data.entity.getId(), data).then(successCallback, errorCallback);
if(formContext.ui.getFormType() == 2){ // save will be call on Update Form not in create
formContext.data.entity.save();
}
},
function (error)
{
alert('Error');
}
);
}
Hope it helps
Hi Ajyendra,
The function is being called from Opportunity both on OnLoad and on OnChange of the Service Availability field.
The field is present on the form.
I understand the issue on OnLoad (Create), because as you say I dont have any ID yet for the opportunity, so in that case the Update function wont work.
Any Idea on how to make this work on Create.
Requirement;
When I pick new opportunity and fill out the Service Availability lookup field, the code needs to retrieve values from the service availability record and insert them on the opportunity.
HI Casper,
As per my Understanding Your requirement is like In Opportunity Form when User Change cgi_serviceavailability Field (which are present in Opportunity Form?) then This function is Call?
But If you try to work this code on Creating the new Opportunity Form It doesn't work Because on formContext.data.entity.getId() is Blank or null So how you update that field to opportunity.
Can you Explain more about From Which entity you call that function?
Hi Ajyendra,
Thank you for answering my issue. Please see answer above.
Hi Thank you for answering my isse!
Actually this can happen both on create and on update.
Even if I insert the "formContext.data.refresh();", nothing happens. It does not update my form. I can see that it has Unsaved fields afterwards, but it does not save or refresh the page after I update the fields in the succesCallback function
Agree with Gautam but few point then no need to use formContext.data.entity.save()
And that formContext.data.entity.refresh() is called in successCallback function of update record
Like function successCallback(){
formContext.data.entity.refresh();
formContext.data.entity.save(); // Note :- only used this line if record is in unsaved mode else no use
}
OR
Xrm.WebApi.updateRecord('opportunity',formContext.data.entity.getId(),data).then(
function success(){
formContext.data.entity.refresh();
formContext.data.entity.save(); // Note :- only used this line if record is in unsaved mode else no use
}, function error(){}
)
Hi ,
If I am not wrong you are calling the function in update form (Opportunity already created) and not in create form. If yes then you dont have to save , try to remove the line " formContext.data.entity.save(); " , instead add refresh the page data using " formContext.data.refresh(); "
Daivat Vartak (v-9d...
225
Super User 2025 Season 1
Eugen Podkorytov
106
Muhammad Shahzad Sh...
106
Most Valuable Professional