How do you mark a Lead converted on record.js ?

Hello there !

What i want :

On Pro 7.5.2

For a client, when a user clicks on a custom button in a Leads' record, it calls an external API.
So, basically, i want that, if the api call is successful and we don't get any errors, i want to disable  editing on the Leads' record, and i want to mark it as "converted". (aka, get the convert button to turn green, and save the fact that the lead has been converted into the databases)


What i did :
I've managed to do this with the 
app.api.call('GET', app.api.buildURL('myCustomEndPoint/myCustomMethod/'+this.model.get('id')), null, { 
    success: function(date) {
        console.debug("Success!");
        var acls = app.user.getAcls();
        acls.Leads.edit = 'no'; 
        app.user.set("acls", acls);
        self.model.set('converted','1');
        /*$('[name="edit_button"]').hide(); */
    },
    error:function(error){
        console.debug(error);
    }
});


Where i am stuck :

For the "disable edit" part,  i tried to change the ACL on it -as you can see, but it does not seem to work. Do you have any idea on this?
But, well, this is not the most important question i have (really, because i can hide the "edit" button if i want to).

The most important question here is :

- How to mark the lead as Converted ? Aka : Being on record.js, how can i fill the database column "converted" with 1, and render the green button saying "Converted"

As you can see, i've tried with

this.model.set('converted','1');

but in the Firebug Console, it says that "this" is undefined, so i tried with declaring self = this before the api call. But it does not work either.


I would like it to change from 0 to 1 and to be saved in databases...

Thanks a lot for the ideas and the tips !
Parents
  • Hello!

    I created my own little test button in the record view for my local 7.5.2.3 Leads module. My function simply does this:

     
    this.model.set("converted",true);  
    this.model.save();

    That seems to set the converted field in the database and adds the green "Converted" label to the record view. If this does not work for you and you are still getting the "this" is undefined, then your issue is probably more scope than the setting of the converted field. If it helps any, I followed the examples here to setup the custom button and function call.

    I hope that helps!

    -Mark
  • After all this time, thanks a lot Mark Everidge ! This solution of " this.model.set('converted',true); "   worked like a charm !

    But i still have one question about converting a Lead :

    Setting "converted" to true does set the conversion, and if i refresh my page, i can see the little green label "Converted", just beside the name of my lead.

    BUT (yes, one but again), i would like the page to display the little green label immediately when i've set my lead to Converted, without any refresh.

    How could i do that? Any idea everyone ?

    Thanks a lot !

  • Hi Gaelle,

    After performing the save(); you can trigger the "change:badge" event to fire. This should refresh the "Converted" label. The following is now how the code should look:

    this.model.set("converted",true); 
    this.model.save();
    this.model.trigger("change:badge");
    

    -Mark

  • Hi Mark,

    Just one suggestion here!! Would it be better if we trigger the change:badge event in save success callback (as below) so that our logic won't go wrong if the save action fails because of any reason also?

    this.model.set("converted",true);   
    this.model.save({},{
         success:function(){
              self.model.trigger("change:badge"); 
         },
    });
    

    -Shijin Krishna

Reply Children
No Data