Newer Version Available

This content describes an older version of this product. View Latest

Sample Process.Plugin Implementation for Lead Conversion

In this example, an Apex class implements the Process.Plugin interface and converts a lead into an account, contact, and optionally, an opportunity. Test methods for the plug-in are also included. This implementation can be called from a flow via an Apex plug-in element.

We recommend using the @InvocableMethod annotation instead of the Process.Plugin interface.

  • The interface doesn’t support Blob, Collection, sObject, and Time data types, and it doesn’t support bulk operations. Once you implement the interface on a class, the class can be referenced only from flows.
  • The annotation supports all data types and bulk operations. Once you implement the annotation on a class, the class can be referenced from flows, processes, and the Custom Invocable Actions REST API endpoint.

Tip

1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// Converts a lead as a step in a Visual Workflow process.
18global class VWFConvertLead implements Process.Plugin {
19    // This method runs when called by a flow's Apex plug-in element.
20    global Process.PluginResult invoke(
21        Process.PluginRequest request) {
22            
23        // Set up variables to store input parameters from 
24        // the flow.
25        String leadID = (String) request.inputParameters.get(
26            'LeadID');
27        String contactID = (String) 
28            request.inputParameters.get('ContactID');
29        String accountID = (String) 
30            request.inputParameters.get('AccountID');
31        String convertedStatus = (String) 
32            request.inputParameters.get('ConvertedStatus');
33        Boolean overWriteLeadSource = (Boolean) 
34            request.inputParameters.get('OverwriteLeadSource');
35        Boolean createOpportunity = (Boolean) 
36            request.inputParameters.get('CreateOpportunity');
37        String opportunityName = (String) 
38            request.inputParameters.get('ContactID');
39        Boolean sendEmailToOwner = (Boolean) 
40            request.inputParameters.get('SendEmailToOwner');   
41        
42        // Set the default handling for booleans. 
43        if (overWriteLeadSource == null) 
44            overWriteLeadSource = false;
45        if (createOpportunity == null) 
46            createOpportunity = true;
47        if (sendEmailToOwner == null) 
48            sendEmailToOwner = false;
49        
50        // Convert the lead by passing it to a helper method.
51        Map<String,Object> result = new Map<String,Object>();
52        result = convertLead(leadID, contactID, accountID, 
53            convertedStatus, overWriteLeadSource, 
54            createOpportunity, opportunityName, 
55            sendEmailToOwner);
56 
57        return new Process.PluginResult(result); 
58    }
59    
60    // This method describes the plug-in and its inputs from
61    // and outputs to the flow.
62    // Implementing this method adds the class to the 
63    // Cloud Flow Designer palette.
64    global Process.PluginDescribeResult describe() {
65        // Set up plugin metadata
66        Process.PluginDescribeResult result = new 
67            Process.PluginDescribeResult();
68        result.description = 
69            'The LeadConvert Flow Plug-in converts a lead into ' + 
70            'an account, a contact, and ' + 
71            '(optionally)an opportunity.';
72        result.tag = 'Lead Management';
73        
74        // Create a list that stores both mandatory and optional 
75        // input parameters from the flow.
76        // NOTE: Only primitive types (STRING, NUMBER, etc.) are 
77        // supported at this time.
78        // Collections are currently not supported.
79        result.inputParameters = new 
80            List<Process.PluginDescribeResult.InputParameter>{
81            // Lead ID (mandatory)
82            new Process.PluginDescribeResult.InputParameter(
83                'LeadID', 
84                Process.PluginDescribeResult.ParameterType.STRING, 
85                true),
86            // Account Id (optional)
87            new Process.PluginDescribeResult.InputParameter(
88                'AccountID', 
89                Process.PluginDescribeResult.ParameterType.STRING, 
90                false),
91            // Contact ID (optional)
92            new Process.PluginDescribeResult.InputParameter(
93                'ContactID', 
94                Process.PluginDescribeResult.ParameterType.STRING, 
95                false),            
96            // Status to use once converted
97            new Process.PluginDescribeResult.InputParameter(
98                'ConvertedStatus', 
99                Process.PluginDescribeResult.ParameterType.STRING, 
100                true),
101            new Process.PluginDescribeResult.InputParameter(
102                'OpportunityName', 
103                Process.PluginDescribeResult.ParameterType.STRING, 
104                false),
105            new Process.PluginDescribeResult.InputParameter(
106                'OverwriteLeadSource', 
107                Process.PluginDescribeResult.ParameterType.BOOLEAN, 
108                false),
109            new Process.PluginDescribeResult.InputParameter(
110                'CreateOpportunity', 
111                Process.PluginDescribeResult.ParameterType.BOOLEAN, 
112                false),
113            new Process.PluginDescribeResult.InputParameter(
114                'SendEmailToOwner', 
115                Process.PluginDescribeResult.ParameterType.BOOLEAN, 
116                false)                                                   
117        };
118
119        // Create a list that stores output parameters sent 
120        // to the flow.
121        result.outputParameters = new List<
122            Process.PluginDescribeResult.OutputParameter>{
123            // Account ID of the converted lead
124            new Process.PluginDescribeResult.OutputParameter(
125                'AccountID', 
126                Process.PluginDescribeResult.ParameterType.STRING),
127            // Contact ID of the converted lead
128            new Process.PluginDescribeResult.OutputParameter(
129                'ContactID', 
130                Process.PluginDescribeResult.ParameterType.STRING),
131            // Opportunity ID of the converted lead
132            new Process.PluginDescribeResult.OutputParameter(
133                'OpportunityID', 
134                Process.PluginDescribeResult.ParameterType.STRING)                
135        };
136
137        return result;
138    }
139        
140    /**
141     * Implementation of the LeadConvert plug-in.
142     * Converts a given lead with several options:
143     * leadID - ID of the lead to convert
144     * contactID - 
145     * accountID - ID of the Account to attach the converted 
146     *  Lead/Contact/Opportunity to.
147     * convertedStatus - 
148     * overWriteLeadSource - 
149     * createOpportunity - true if you want to create a new 
150     *  Opportunity upon conversion
151     * opportunityName - Name of the new Opportunity.
152     * sendEmailtoOwner - true if you are changing owners upon 
153     *  conversion and want to notify the new Opportunity owner.
154     *
155     * returns: a Map with the following output:
156     * AccountID - ID of the Account created or attached 
157     *  to upon conversion.
158     * ContactID - ID of the Contact created or attached 
159     *  to upon conversion.
160     * OpportunityID - ID of the Opportunity created 
161     *  upon conversion.
162     */
163    public Map<String,String> convertLead (
164                               String leadID,
165                               String contactID,
166                               String accountID,
167                               String convertedStatus,
168                               Boolean overWriteLeadSource,
169                               Boolean createOpportunity,
170                               String opportunityName,
171                               Boolean sendEmailToOwner
172        ) {
173        Map<String,String> result = new Map<String,String>();
174                                
175        if (leadId == null) throw new ConvertLeadPluginException(
176            'Lead Id cannot be null');
177        
178        // check for multiple leads with the same ID
179        Lead[] leads = [Select Id, FirstName, LastName, Company 
180            From Lead where Id = :leadID];
181        if (leads.size() > 0) {
182            Lead l = leads[0];
183            // CheckAccount = true, checkContact = false
184            if (accountID == null && l.Company != null) {
185                Account[] accounts = [Select Id, Name FROM Account 
186                    where Name = :l.Company LIMIT 1];
187                if (accounts.size() > 0) {
188                    accountId = accounts[0].id;
189                }
190            }
191            
192            // Perform the lead conversion.
193            Database.LeadConvert lc = new Database.LeadConvert();
194            lc.setLeadId(leadID);
195            lc.setOverwriteLeadSource(overWriteLeadSource);
196            lc.setDoNotCreateOpportunity(!createOpportunity);
197            lc.setConvertedStatus(convertedStatus);
198            if (sendEmailToOwner != null) lc.setSendNotificationEmail(
199                sendEmailToOwner);
200            if (accountId != null && accountId.length() > 0) 
201                lc.setAccountId(accountId);
202            if (contactId != null && contactId.length() > 0) 
203                lc.setContactId(contactId);
204            if (createOpportunity) {
205                lc.setOpportunityName(opportunityName);
206            }
207            
208            Database.LeadConvertResult lcr = Database.convertLead(
209                lc, true);
210            if (lcr.isSuccess()) {
211                result.put('AccountID', lcr.getAccountId());
212                result.put('ContactID', lcr.getContactId());
213                if (createOpportunity) {
214                    result.put('OpportunityID', 
215                        lcr.getOpportunityId());
216                }
217            } else {
218                String error = lcr.getErrors()[0].getMessage();
219                throw new ConvertLeadPluginException(error);
220            }
221        } else { 
222            throw new ConvertLeadPluginException(
223                'No leads found with Id : "' + leadId + '"');
224        }
225        return result;
226    }
227        
228    // Utility exception class
229    class ConvertLeadPluginException extends Exception {}
230}
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// Test class for the lead convert Apex plug-in.
18@isTest
19private class VWFConvertLeadTest {
20    static testMethod void basicTest() {
21        // Create test lead
22        Lead testLead = new Lead(
23           Company='Test Lead',FirstName='John',LastName='Doe');
24        insert testLead;
25    
26        LeadStatus convertStatus = 
27           [Select Id, MasterLabel from LeadStatus 
28           where IsConverted=true limit 1]; 
29        
30        // Create test conversion
31        VWFConvertLead aLeadPlugin = new VWFConvertLead();
32        Map<String,Object> inputParams = new Map<String,Object>();
33        Map<String,Object> outputParams = new Map<String,Object>();
34
35        inputParams.put('LeadID',testLead.ID);
36        inputParams.put('ConvertedStatus', 
37           convertStatus.MasterLabel);
38
39        Process.PluginRequest request = new 
40           Process.PluginRequest(inputParams);
41        Process.PluginResult result;
42        result = aLeadPlugin.invoke(request);
43        
44        Lead aLead = [select name, id, isConverted 
45                       from Lead where id = :testLead.ID];
46        System.Assert(aLead.isConverted);
47        
48    }
49
50     /*
51      * This tests lead conversion with 
52      * the Account ID specified.
53      */
54    static testMethod void basicTestwithAccount() {
55
56        // Create test lead
57        Lead testLead = new Lead(
58            Company='Test Lead',FirstName='John',LastName='Doe');
59        insert testLead;
60        
61        Account testAccount = new Account(name='Test Account');
62        insert testAccount;
63    
64           // System.debug('ACCOUNT BEFORE' + testAccount.ID);
65
66        LeadStatus convertStatus = [Select Id, MasterLabel 
67                    from LeadStatus where IsConverted=true limit 1]; 
68        
69        // Create test conversion
70        VWFConvertLead aLeadPlugin = new VWFConvertLead();
71        Map<String,Object> inputParams = new Map<String,Object>();
72        Map<String,Object> outputParams = new Map<String,Object>();
73
74        inputParams.put('LeadID',testLead.ID);
75        inputParams.put('AccountID',testAccount.ID);
76        inputParams.put('ConvertedStatus',
77            convertStatus.MasterLabel);
78
79        Process.PluginRequest request = new 
80            Process.PluginRequest(inputParams);
81        Process.PluginResult result;
82        result = aLeadPlugin.invoke(request);
83        
84        Lead aLead = 
85            [select name, id, isConverted, convertedAccountID 
86             from Lead where id = :testLead.ID];
87        System.Assert(aLead.isConverted);
88        //System.debug('ACCOUNT AFTER' + aLead.convertedAccountID);
89        System.AssertEquals(testAccount.ID, aLead.convertedAccountID);
90    }
91
92    /*
93     * This tests lead conversion with the Account ID specified.
94    */
95    static testMethod void basicTestwithAccounts() {
96
97        // Create test lead
98        Lead testLead = new Lead(
99            Company='Test Lead',FirstName='John',LastName='Doe');
100        insert testLead;
101        
102        Account testAccount1 = new Account(name='Test Lead');
103        insert testAccount1;
104        Account testAccount2 = new Account(name='Test Lead');
105        insert testAccount2;
106
107           // System.debug('ACCOUNT BEFORE' + testAccount.ID);
108
109        LeadStatus convertStatus = [Select Id, MasterLabel 
110            from LeadStatus where IsConverted=true limit 1]; 
111        
112        // Create test conversion
113        VWFConvertLead aLeadPlugin = new VWFConvertLead();
114        Map<String,Object> inputParams = new Map<String,Object>();
115        Map<String,Object> outputParams = new Map<String,Object>();
116
117        inputParams.put('LeadID',testLead.ID);
118        inputParams.put('ConvertedStatus',
119            convertStatus.MasterLabel);
120
121        Process.PluginRequest request = new 
122            Process.PluginRequest(inputParams);
123        Process.PluginResult result;
124        result = aLeadPlugin.invoke(request);
125        
126        Lead aLead = 
127            [select name, id, isConverted, convertedAccountID 
128            from Lead where id = :testLead.ID];
129        System.Assert(aLead.isConverted);
130    }
131
132
133     /*
134      * -ve Test
135      */    
136    static testMethod void errorTest() {
137
138        // Create test lead
139        // Lead testLead = new Lead(Company='Test Lead',
140        //   FirstName='John',LastName='Doe');
141        LeadStatus convertStatus = [Select Id, MasterLabel 
142            from LeadStatus where IsConverted=true limit 1]; 
143        
144        // Create test conversion
145        VWFConvertLead aLeadPlugin = new VWFConvertLead();
146        Map<String,Object> inputParams = new Map<String,Object>();
147        Map<String,Object> outputParams = new Map<String,Object>();
148        inputParams.put('LeadID','00Q7XXXXxxxxxxx');
149        inputParams.put('ConvertedStatus',convertStatus.MasterLabel);
150
151        Process.PluginRequest request = new 
152            Process.PluginRequest(inputParams);
153        Process.PluginResult result;
154        try {
155            result = aLeadPlugin.invoke(request);    
156        }
157        catch (Exception e) {
158          System.debug('EXCEPTION' + e);
159          System.AssertEquals(1,1);
160        }
161        
162    }
163    
164    
165     /*
166      * This tests the describe() method
167      */ 
168    static testMethod void describeTest() {
169
170        VWFConvertLead aLeadPlugin = 
171            new VWFConvertLead();
172        Process.PluginDescribeResult result = 
173            aLeadPlugin.describe();
174        
175        System.AssertEquals(
176            result.inputParameters.size(), 8);
177        System.AssertEquals(
178            result.OutputParameters.size(), 3);
179        
180     }
181
182}