1

The problem that I am having is that I am using addError method in before insert trigger which actually rollback every DML I make including @future method calls, sending email, queueing batch jobs, or performing any DML. What I would like to know is how to stop the process of converting Lead to Account and at the same time dispaly message in the convert page as well as to set and update a custom Lead status field to 'Existing Deal'.

To be more precise what I need to acheve: 1. Stop the process of converting Lead to Account. 2. Display message in the convert Lead to Account page that the respective Lead is not converted. 3. Set and Update the Lead.Status__c field to 'Existing Deal'.

Apparently this can't be achieved by using the addError method and I will strongly appreciate if you can advise how to proceed and find a solution to this issue?

Regards,

Dilyan

1 Answer 1

3

You can't have it both ways. Using addError removes all side effects of the transaction, including sending emails, kicking off batch/scheduled/future calls, and committing data that was otherwise created during the trigger's life cycle. If you want to prevent conversion in some circumstances, you can override the lead convert button with a custom Visualforce page, and enforce the logic there. Simply perform your logic of checking before conversion, display error if appropriate and update lead, or convert lead and redirect to opportunity page. The override is located in Setup > Customize > Leads > Buttons, Links, and Actions.

Example Controller (conversion only)

public class LeadConvert {
    Lead record;
    ApexPages.StandardController c;
    public LeadConvert(ApexPages.StandardController controller) {
        record = (Lead)controller.getRecord();
        c = controller;
    }
    public PageReference convertLeadRecord() {
        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(record.Id);
        lc.setOpportunityName('Default Name');
        try {
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            return new ApexPages.StandardController(new Opportunity(Id=lcr.getOpportunityId())).view();
        } catch(Exception e) {
            record.Status__c = 'Existing Deal';
            return c.save();
        }
    }
}

Example Page (conversion only)

<apex:page standardController="Lead" extensions="LeadConvert" action="{!convertLeadRecord}">

</apex:page>

Obviously, if you want something more complicated, like the actual UI presented on conversion out of the box, you'll have to build the entire thing yourself. Such an effort is outside the scope of this question, as it would require several hundred lines of code for a full implementation.

Useful Documentation:

6
  • Hi, because I'm new to VF could you please give an example how to create custom VF page and to integrate it in Salesforce in a way that the page to appear in the list of VF pages in the screen for overriding the Convert button?
    – Dylan D
    Nov 20, 2015 at 20:54
  • @DylanD You have to start your page off like this: <apex:page standardController="Lead" ....
    – sfdcfox
    Nov 20, 2015 at 21:00
  • ok, but I still do not understand if I create a VF file or file with VF code how to integrate such file in Salesforce? Could you please provide more detail example?
    – Dylan D
    Nov 20, 2015 at 21:07
  • @DylanD I provided an example that includes a bare-bones conversion. Writing up whatever you UI you want is up to you. This example always creates a new contact, account, and opportunity. I've also included an example of how to react to failures.
    – sfdcfox
    Nov 20, 2015 at 21:20
  • I have created a file with name customLeadConvert.vfp and added the content you suggested in the file. I would like to know how to integrate the customLeadConvertl.vfp file in Salesforce?
    – Dylan D
    Nov 20, 2015 at 21:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .