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 !
  • 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

  • Hello everyone !

    I'm coming back to these little lines of code...

    It works REALLY great.

    But, to make it complete, i still have a question for you !

    How to change the ACLs on this particular converted lead to Edit = 'no' ?

    I know i can do

      var acls = app.user.getAcls();

      acls.Leads.edit = 'no';

      app.user.set("acls", acls);

    But this will unable Edit on ALL Leads. How can i specify the record i want to force to be uneditable ?

    Thanks a lot !

  • Hi Gaelle,

    You can just disable the edit button for the Leads of it is converted. If you update the access in acl object for the user, it will long in the entire session of the user. Once you set, that will be applicable for list view,record view etc for all the records.

    Thanks

    -Shijin Krishna

  • Hello Shijin Krishna !

    Thanks a lot for your reply and your time.

    I've already set the edit button to disable like this :

    $('[name = "edit_button"]').addClass("disabled");

    But unfortunately, the user can edit every field separately. Do you have any idea on how to disable the inline edit ?

    Thanks a lot : )

    have a nice day

  • Hi Gaelle

    I am just updating my previous code here. I checked from my end and I could achieve this just by overrideing the method instead of completing rewriting in the custom record view controller. Please add the below code to your custom record.js file

    _buildGridsFromPanelsMetadata:function(panels){
      this._super('_buildGridsFromPanelsMetadata',[panels]);
      _.each(panels, function(panel) {
       _.each(panel.fields, function(field, index) {
    //if the lead is converted we will push all the fields to noEditFields array.
      if(this.model.get('converted')){
           this.noEditFields.push(field.name);
      }
      },this);
      }, this);
      },
    
  • The above method is taken from the standard record.js file. If you push any fields to the noEditFields array, it will become non-editable.

    So along with the conditions based on acls we are just adding a new condition which will check the lead is converted or not. If it is converted each fields will be added to the noEditFields array.

    1. else if(this.model.get('converted') == true){ 
    2.   this.noEditFields.push(field.name); 

                             }