Search code examples
javascriptdynamics-crm

Set lookup value on opportunity not working when it's created after lead qualification


I've got the following piece of code on the onload event of an opportunity:

$.ajax({
    url: Xrm.Page.context.getClientUrl() + '/api/data/v8.0/pricelevels(75ec69fe-3118-e611-80b7-0050568a6c2d)?$select=name',
    headers: { 'Accept': 'Application/json' }
}).done(function (data) {
    if (!$.isEmptyObject(data)) {
        var lookup = [];
        lookup[0] = {};
        lookup[0].id = data.pricelevelid;
        lookup[0].name = data.name;
        lookup[0].entityType = 'pricelevel';
        Xrm.Page.getAttribute('pricelevelid').setValue(lookup);
    }
}).fail(function (data) {
    console.log('error getting default price list');
});

This code is working fine when I open an opportunity or when I create a new opportunity.

However when I qualify a lead CRM opens the newly created opportunity. I can debug the javascript and see that it's working. The value is retrieved and the setValue line is called. Only the ui isn't updated and the field value isn't set.

Things to note:

  • I'm using CRM 2016
  • The lead qualification is customized using a plugin.

Any ideas on what's causing the ui not to update?


Solution

  • Couple things I'd try: 1) turn off the plugin, same problem? 2) wrap the setValue call in a setTimeout: setTimeout(function() { /*setValue*/ }, 100) to see if that makes any difference.