この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

リードの変換用の Process.Plugin の実装のサンプル

この例では、Apex クラスで Process.Plugin インターフェースを実装し、リードを取引先、取引先責任者、および必要に応じて商談に変換します。プラグインのテストメソッドも含まれています。この実装は、Apex プラグイン要素を使用してフローからコールできます。

Process.Plugin インターフェースではなく @InvocableMethod アノテーションを使用することをお勧めします。

  • インターフェースは、Blob、Collection、sObject、および Time データ型と一括操作をサポートしていません。インターフェースをクラスに実装すると、クラスはフローからしか参照できません。
  • アノテーションは、すべてのデータ型と一括操作をサポートしています。アノテーションをクラスに実装すると、クラスはフロー、プロセス、および Custom Invocable Actions REST API エンドポイントから参照できます。

ヒント

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