web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Client API JS call does not trigger on OnChange

(0) ShareShare
ReportReport
Posted on by 35

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

}

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

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

     

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at

    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(){}

    )

  • Casper Schau Profile Picture
    35 on at

    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

  • Casper Schau Profile Picture
    35 on at

    Hi Ajyendra,

    Thank you for answering my issue. Please see answer above.

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at

    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?

  • Casper Schau Profile Picture
    35 on at

    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.

  • Verified answer
    ajyendra Profile Picture
    1,738 on at

    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:

    https://crm365blog.wordpress.com/2018/05/29/get-set-field-values-in-c-javascript-and-utility-functions/

    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 

  • Casper Schau Profile Picture
    35 on at

    Brilliant, thank you for the answer. I just guessed that setValue wasnt possible any longer, but of course it is :)

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 52

#2
Manoj - ManoVerse Profile Picture

Manoj - ManoVerse 43 Super User 2026 Season 1

#3
Ayesha Wajahat Profile Picture

Ayesha Wajahat 14

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans