My colleague and I have been tasked to update our Convert Lead workflow. The process is still the same of converting the leads but we now have new fields and values that the user can choose and we have more validation rules added to the pages that we need to take into account.

The following is the code that what we have:

// Converts a lead as a step in a Visual Workflow process.
global class SGIConvertLead implements Process.Plugin {
// This method runs when called by a flow's Apex plug-in element.
global Process.PluginResult invoke(
    Process.PluginRequest request) {

    // Set up variables to store input parameters from 
    // the flow.
    String leadID = (String) request.inputParameters.get('LeadID');
    String contactID = (String) request.inputParameters.get('ContactID');
    String accountID = (String) request.inputParameters.get('AccountID');
    String convertedStatus = (String) request.inputParameters.get('ConvertedStatus');
    Boolean overWriteLeadSource = (Boolean) request.inputParameters.get('OverwriteLeadSource');
    Boolean createOpportunity = (Boolean) request.inputParameters.get('CreateOpportunity');
    String opportunityName = (String) request.inputParameters.get('ContactID');
    Boolean sendEmailToOwner = (Boolean) request.inputParameters.get('SendEmailToOwner');
    String ownerID = (String) request.inputParameters.get('OwnerID');

    // Set the default handling for booleans. 
    if (overWriteLeadSource == null) overWriteLeadSource = false;
    if (createOpportunity == null) createOpportunity = true;
    if (sendEmailToOwner == null)  sendEmailToOwner = false;

    // Convert the lead by passing it to a helper method.
    Map<String,Object> result = new Map<String,Object>();
    result = convertLead(leadID, contactID, accountID, convertedStatus, overWriteLeadSource,createOpportunity, opportunityName, sendEmailToOwner, ownerID);
    return new Process.PluginResult(result); 

// This method describes the plug-in and its inputs from
// and outputs to the flow.
// Implementing this method adds the class to the 
// Cloud Flow Designer palette.
global Process.PluginDescribeResult describe() {
    // Set up plugin metadata
    Process.PluginDescribeResult result = new Process.PluginDescribeResult();
    result.description = 'The LeadConvert Flow Plug-in converts a lead into ' +  'an account, a contact, and ' + '(optionally)an opportunity.';
    result.tag = 'Lead Management';

    // Create a list that stores both mandatory and optional 
    // input parameters from the flow.
    // NOTE: Only primitive types (STRING, NUMBER, etc.) are 
    // supported at this time.
    // Collections are currently not supported.
    result.inputParameters = new 
        // Lead ID (mandatory)
        new Process.PluginDescribeResult.InputParameter('LeadID', Process.PluginDescribeResult.ParameterType.STRING, true),
        // Account Id (optional)
        new Process.PluginDescribeResult.InputParameter('AccountID', Process.PluginDescribeResult.ParameterType.STRING, false),
        // Contact ID (optional)
        new Process.PluginDescribeResult.InputParameter('ContactID', Process.PluginDescribeResult.ParameterType.STRING, false),            
        // Status to use once converted
        new Process.PluginDescribeResult.InputParameter('ConvertedStatus', Process.PluginDescribeResult.ParameterType.STRING, True),
        new Process.PluginDescribeResult.InputParameter('OpportunityName', Process.PluginDescribeResult.ParameterType.STRING, false),
        new Process.PluginDescribeResult.InputParameter('OverwriteLeadSource', Process.PluginDescribeResult.ParameterType.BOOLEAN, false),
        new Process.PluginDescribeResult.InputParameter('CreateOpportunity',  Process.PluginDescribeResult.ParameterType.BOOLEAN, false),
        new Process.PluginDescribeResult.InputParameter('SendEmailToOwner',  Process.PluginDescribeResult.ParameterType.BOOLEAN, false),
        new Process.PluginDescribeResult.InputParameter('OwnerID',  Process.PluginDescribeResult.ParameterType.String, false)  

    // Create a list that stores output parameters sent 
    // to the flow.
    result.outputParameters = new List<Process.PluginDescribeResult.OutputParameter>{
        // Account ID of the converted lead
        new Process.PluginDescribeResult.OutputParameter('AccountID', Process.PluginDescribeResult.ParameterType.STRING),
        // Contact ID of the converted lead
        new Process.PluginDescribeResult.OutputParameter('ContactID', Process.PluginDescribeResult.ParameterType.STRING),
        // Opportunity ID of the converted lead
        new Process.PluginDescribeResult.OutputParameter('OpportunityID', Process.PluginDescribeResult.ParameterType.STRING),
        //Owner ID of the converted lead
        new Process.PluginDescribeResult.OutputParameter('OwnerID', Process.PluginDescribeResult.ParameterType.STRING)                

    return result;

 * Implementation of the LeadConvert plug-in.
 * Converts a given lead with several options:
 * leadID - ID of the lead to convert
 * contactID - 
 * accountID - ID of the Account to attach the converted 
 *  Lead/Contact/Opportunity to.
 * convertedStatus - 
 * overWriteLeadSource - 
 * createOpportunity - true if you want to create a new 
 *  Opportunity upon conversion
 * opportunityName - Name of the new Opportunity.
 * sendEmailtoOwner - true if you are changing owners upon 
 *  conversion and want to notify the new Opportunity owner.
 * returns: a Map with the following output:
 * AccountID - ID of the Account created or attached 
 *  to upon conversion.
 * ContactID - ID of the Contact created or attached 
 *  to upon conversion.
 * OpportunityID - ID of the Opportunity created 
 *  upon conversion.
public Map<String,String> convertLead (
                           String leadID,
                           String contactID,
                           String accountID,
                           String convertedStatus,
                           Boolean overWriteLeadSource,
                           Boolean createOpportunity,
                           String opportunityName,
                           Boolean sendEmailToOwner,
                           String ownerID) 
    Map<String,String> result = new Map<String,String>();                        

    if (leadId == null) throw new ConvertLeadPluginException('Lead Id cannot be null');

    // check for multiple leads with the same ID
    Lead[] leads = [Select Id, Name, FirstName, LastName, Company, LeadSource, Email, Industry, City, Street, State, PostalCode,
                    Country_Code__c, Line_of_Business__c, Relationship_with_Intergraph_Status__c From Lead where Id = :leadID];

    if (leads.size() > 0) 
    {       Lead l = leads[0];

        // CheckAccount = true, checkContact = false
        if (accountID == null && l.Company != null) 
            Account[] accounts = [Select Id, Name, BillingStreet, BillingCity, BillingState, BillingPostalCode,
                            BillingCountry, Street_Address__c, City__c, State_Province__c, Zip_Postal_Code__c, 
                            Country_Code__c, Account_Intergraph_Line_of_Business__c, Industry, Type, 
                            Account_Category__c, RecordTypeId From Account where Name = :l.Company LIMIT 1];
            if (accounts.size() > 0) 
                accountId = accounts[0].id;

        // Perform the lead conversion.
        Database.LeadConvert lc = new Database.LeadConvert();
        if (ownerID != null) lc.setOwnerId(ownerID);
        if (sendEmailToOwner != null) lc.setSendNotificationEmail(sendEmailToOwner);

     //Lead Convert Account - Map Fields and Custom Fields- Set AccountID for leadconvert - Handle all exceptions for Account - Add new Account or attach to exsiting Account   
    // {
        Account leadAccount = new Account();
        if (accountId != null && accountId.length() > 0) leadAccount.ParentId = accountId;

        if (leadAccount.Account_Intergraph_Line_of_Business__c== 'PPM') leadAccount.RecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'PPM Standard Account Record Type'].Id;
            else leadAccount.RecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'SGI Standard Record Account Type'].Id;
        if (l.Line_of_Business__c!= null) leadAccount.Account_Intergraph_Line_of_Business__c = l.Line_of_Business__c;  
        if (l.Industry != null) leadAccount.Industry = l.Industry;               
        if (l.Relationship_with_Intergraph_Status__c != null) leadAccount.Type = l.Relationship_with_Intergraph_Status__c;   
        //if (leadAccount.Account_Category__c != null && leadAccount.Account_Intergraph_Line_of_Business__c == 'PPM') leadAccount.Account_Category__c = leadAccount.Account_Category__c;

            //only overwrite the physical address of any of the current fields are blank
            if (leadAccount.Street_Address__c == null || leadAccount.City__c == null || 
                leadAccount.State_Province__c == null || leadAccount.Zip_Postal_Code__c == null || 
                leadAccount.Country_Code__c == null) 

                if (l.Street != null)
                    leadAccount.Street_Address__c = l.Street;
                if (l.City != null)
                    leadAccount.City__c = l.City;
                if (l.State != null) 
                    leadAccount.State_Province__c = l.State;
                if (l.PostalCode != null) 
                    leadAccount.Zip_Postal_Code__c = l.PostalCode;
                if (l.Country_Code__c != null) 
                    leadAccount.Country_Code__c = l.Country_Code__c;
         insert leadAccount;

    /* Catch (DmlException ex)
         throw new ConvertLeadPluginException();

        //Contact ID
        Contact leadContact = New Contact();
     if (contactId != null && contactId.length() > 0) leadContact.AccountId = leadAccount.Id;
            if (l.FirstName != null) leadContact.FirstName = l.FirstName;
                leadContact.LastName = l.LastName;
            if (l.Department__c != null)leadContact.Department = l.Department__c;
                leadContact.Budget_for_Project__c = l.Budget_for_Project__c;

            insert leadContact;             
     /*else {
           if (contactId == null) leadContact.AccountId = leadAccount.Id;
          leadContact.Name = [Select Id  Name From Lead Where Id = : l.Id Limit 1];
        insert leadContact;

     //Opportunity ID
        if (createOpportunity) 

        Database.LeadConvertResult lcr = Database.convertLead(lc, true);

     if (lcr.isSuccess()) 
            result.put('AccountID', lcr.getAccountId());
            result.put('ContactID', lcr.getContactId());
            if (createOpportunity) 
                result.put('OpportunityID', lcr.getOpportunityId());
            String error = lcr.getErrors()[0].getMessage();
            throw new ConvertLeadPluginException(error);
        throw new ConvertLeadPluginException('No leads found with Id : "' + leadId + '"');
    return result;

// Utility exception class
class ConvertLeadPluginException extends Exception {}


The old code for the ConvertLead process is too long to post but I'm willing to post, if need be. We've hit a road block and we're wondering if we're going in the right direction with our code. I do realize that you all can't see our validation rules to see what is required and what's not, but we're wondering if we're mapping the fields correctly?



You must log in to answer this question.

Browse other questions tagged .