Skip to main content

Notifications

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

}

  • Casper Schau Profile Picture
    35 on at
    RE: Client API JS call does not trigger on OnChange

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

  • Verified answer
    ajyendra Profile Picture
    1,738 on at
    RE: Client API JS call does not trigger on OnChange

    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
    RE: Client API JS call does not trigger on OnChange

    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.

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at
    RE: Client API JS call does not trigger on OnChange

    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
    RE: Client API JS call does not trigger on OnChange

    Hi Ajyendra,

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

  • Casper Schau Profile Picture
    35 on at
    RE: Client API JS call does not trigger on OnChange

    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

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at
    RE: Client API JS call does not trigger on OnChange

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

    )

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Client API JS call does not trigger on OnChange

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

     

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Eugen Podkorytov Profile Picture

Eugen Podkorytov 106

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106 Most Valuable Professional

Overall leaderboard

Product updates

Dynamics 365 release plans