+ Start a Discussion
Karleen MendozaKarleen Mendoza 

Add additional Lead field to customized lead conversion page

Hi all, I am completely new to Apex and such. I want to create a customize lead conversion page and would like to add an additional custom field to the lead conversion page. 

I found some sample code with a visualforce page and components already made. But I don't know how to add the additional custom field to the page?

The field name I'd like to add is "Bypass New Business Tasks", API name Bypass_New_Business_Tasks__c. It is a field on the Lead object.

This is the sample code I got:
<!-- This component allows for the core lead converting functionality -->

<apex:component controller="leadConvertCoreComponentController">
    <!-- This allows for the lookup to Account but hides the text of the selected account name leaving the magnifying glass only -->
    <style type="text/css">
        input.hiddenInput {
            width:0;
            height:0;
            border:0;
            padding:0;
            margin:0;
        }
    </style>

    <!-- This is the attribute that holds the lead to convert -->
    <apex:attribute name="LeadToConvert" 
        description="The lead to convert"
        type="Lead" 
        assignTo="{!leadConvert}"
        required="true"/>

    <!-- This attribute is used for the leadConvertController to interact with this component's controller -->
    <apex:attribute name="thepageController"
      type="PageControllerBase"
      assignTo="{!pageController}"
      required="true"
      description="The controller for the page." />
    
    <!-- Lead Convert section -->    
    <apex:pageBlockSection id="pblockconvertLead" title="Convert Lead" collapsible="no" columns="1">
    
        
    
        <!-- Leads Record Owner -->
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Record Owner" for="recordOwner" />
            <apex:inputField value="{!contactId.ownerID}" id="recordOwner" />
        </apex:pageBlockSectionItem>
        
        <!-- Checkbox indicating whether to send an email to the owner -->    
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Send Email to the Owner" for="sendOwnerEmail" />
            <apex:inputCheckbox value="{!sendOwnerEmail}" id="sendOwnerEmail" />
        </apex:pageBlockSectionItem>
        
             
        <apex:pageBlockSectionItem id="convertLeadAccountBlockSectionItem" >
            
            <!-- Account Name picklist  -->
            <apex:outputLabel value="Account Name" for="accountList" />
            <apex:outputPanel styleClass="requiredInput" id="block">
                <div class="requiredBlock"></div>
                <apex:selectList value="{!selectedAccount}" size="1" 
                    multiselect="false" id="accountList">
                    
                    <apex:actionSupport event="onchange" 
                        action="{!accountChanged}" 
                        rerender="opportunityName, ViewLink"
                         />     
                    <apex:selectOptions value="{!accounts}"/>
                    
                </apex:selectList>
                
                <!--  Account lookup to add existing accounts to the picklist -->
                <apex:inputField styleClass="hiddenInput" 
                value="{!contactId.AccountId}" id="accountId" >
                    
                    <!-- When an account is looked up, it is added to the select list -->                            
                    <apex:actionSupport event="onchange" 
                        action="{!accountLookedUp}" 
                        rerender="accountList, opportunityName, ViewLink"
                         />
                        
                </apex:inputField>
                
                <!-- This is a link which shows a view of the selected account in a new pop up window -->                
                <apex:commandLink id="ViewLink"  value="View"
                    onclick="javascript:if ('{!selectedAccount}' != 'NEW' && '{!selectedAccount}' != 'NONE' ) {var newWindow = window.open('/{!selectedAccount}/p', 'accountview', 'top=40, left=40,scrollbars=yes, height=450, width=800');newwindow.focus();} else {alert('You can only view existing accounts.');}"
                />
                
            </apex:outputPanel>
        </apex:pageBlockSectionItem>
        
        <!-- Opportunity section -->
        <apex:pageBlockSectionItem id="oppSection" >
            
            <apex:outputPanel id="oppPanel" >
                <apex:outputLabel value="Opportunity Name" for="opportunityName" />
            </apex:outputPanel>
            
            
            <apex:outputPanel id="opportunityPanel" layout="block">
                <apex:outputPanel rendered="{!NOT(doNotCreateOppty)}" styleClass="requiredInput" id="block">
                <div class="requiredBlock"></div>
                
                <!-- Opportunity Name -->
                <apex:inputField required="false" 
                    value="{!opportunityID.Name}" id="opportunityName"/>
                </apex:outputPanel>
                
                <!--  This is only shown if the Do Not Create Opportunity is true -->    
                <apex:inputText disabled="true" rendered="{!doNotCreateOppty}"  value="{!opportunityID.Name}" id="opportunityNameDisabled"/>
                <apex:outputPanel layout="block" styleClass="requiredInput">
                <apex:outputPanel layout="block" />
                
                <!-- Checkbox indicating whether to create an Opportunity -->
                <apex:inputCheckbox value="{!doNotCreateOppty}" id="doNotCreateOppty">
                    <apex:actionSupport event="onchange" 
                        
                        rerender="opportunityPanel"
                         />
                </apex:inputCheckbox>
                Do not create a new opportunity upon conversion.
            </apex:outputPanel> 
            </apex:outputPanel>
        </apex:pageBlockSectionItem>
        
        <!-- Converted Status -->
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Converted Status" for="leadStatus" />
            <apex:outputPanel styleClass="requiredInput" layout="block">
                <div class="requiredBlock"></div>
                <apex:selectList id="LeadStatusList" size="1" 
                    value="{!LeadToConvert.Status}" multiselect="false" required="true">
                    <apex:selectOptions value="{!LeadStatusOption}"/>
                </apex:selectList> 
            </apex:outputPanel>
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
    
</apex:component>

Component:
<!-- This is our page component tag -->
<!-- We are using the Standard Controller for Lead and a Controller Extension called 'leadConvertController' -->

<apex:page standardController="Lead" extensions="leadConvertController">

<!-- Page section header -->

<apex:sectionHeader title="Convert Lead" subtitle="{!Lead.name}"/>

<!-- Visualforce Component with custom page header text -->

<c:leadConvertPageHeaderTextComponent />

<!-- This form tag must include any fields or buttons used in the Lead Convert operation -->
 
<apex:form id="theForm" >

    <!-- This pageBlock component is a container that includes the visual elements of the Lead Convert page -->
    <!-- All custom Components should be included within this pageBlock unless you are using more advanced custom styling -->

    <apex:pageBlock mode="edit" id="pageBlock">
   
        <!-- This component is used to display error messages on the page -->
           
        <apex:pageMessages />
           
        <!-- This pageBlockButtons component is where you can add / remove custom buttons from the Lead Convert page -->
   
        <apex:pageBlockButtons >
       
            <apex:commandButton action="{!convertLead}" value="Convert"/>
            <apex:commandButton action="{!cancel}" value="Cancel"/>
               
        </apex:pageBlockButtons>           
       
        <!-- This custom component contains the section & fields specific to converting a Lead to an Account, Contact, and Opportunity -->
          
        <c:leadConvertCoreComponent id="leadComp" thepageController="{!this}" LeadToConvert="{!leadToConvert}" rendered="True" />

        <!-- This custom component contains the section & fields specific to creating a new Task on Lead Convert -->
        <!-- If you would like to hide this section, set the rendered attribute to 'False' -->
           
        <c:leadConvertTaskInfoComponent thepageController="{!this}" rendered="true" />

        <!-- This custom component contains the section & Description field specific to the new Task on Lead Convert -->
        <!-- If you would like to hide this section, set the rendered attribute to 'False' -->
                
        <c:leadConvertTaskDescriptionComponent thepageController="{!this}" rendered="true" />

    <!-- This is the closing tag for the pageBlock component -->
    
    </apex:pageBlock>
       
<!-- This is the closing tag for the form -->

</apex:form>
   
<!-- This is the closing tag for the page -->
    
</apex:page>

There are some classes and such that make the custom lead conversion page work. I just need to add the one field onto the page but I don't know where??!
Santosh Bompally 8Santosh Bompally 8
Post the apex class as well, And to which object/field you want to map Bypass_New_Business_Tasks__c to ?
Karleen MendozaKarleen Mendoza
There are quite a few Apex Classes, I'm not sure which one to post here. I downloaded these sample codes from an unmanaged package on the appexchange: https://appexchange.salesforce.com/listingDetail?listingId=a0N3000000B4sHpEAJ

Here is one that is called "lead convert controller"
/*
  This is the controller for the Visual Force page leadConvertPage.
*/

public with sharing class leadConvertController extends PageControllerBase {
    
    // This is the lead that is to be converted
    public  Lead leadToConvert {get; set;}
    
    // Constructor for this controller
    public leadConvertController(ApexPages.StandardController stdController) {
        
        //get the ID to query for the Lead fields
        Id leadId = stdController.getId();
        
        leadToConvert = [SELECT Id, Status, OwnerId, Name, Company FROM Lead WHERE Id = :leadId];
    }

  /*
  These are instances of the components' controllers which this class will access.
  
  If you add new custom components, add an instance of the class here
  */
    public leadConvertCoreComponentController myComponentController { get; set; }
    public leadConvertTaskInfoComponentController myTaskComponentController { get; set; }
    public leadConvertTaskDescComponentController myDescriptionComponentController { get; set; }
    
    /*
      These are the set methods which override the methods in PageControllerBase. 
      These methods will be called by the ComponentControllerBase class.
      
      If you add new custom components, a new overridden set method must be added here.
    */
    public override void setComponentController(ComponentControllerBase compController) {
        
        myComponentController = (leadConvertCoreComponentController)compController;
    
    }
   
    public override void setTaskComponentController(ComponentControllerBase compController) {
    
        myTaskComponentController = (leadConvertTaskInfoComponentController)compController;
    
    }
  
    public override void setDescriptionComponentController(ComponentControllerBase compController) {
    
        myDescriptionComponentController = (leadConvertTaskDescComponentController)compController;
    
    } 

  /*
      These are the get methods which override the methods in PageControllerBase.
      
      If you add new custom components, a new overridden get method must be added here.
    */
    public override ComponentControllerBase getMyComponentController() {

        return myComponentController;

    }

    public override ComponentControllerBase getmyTaskComponentController() {

        return myTaskComponentController;

    }   
  
    public override ComponentControllerBase getmyDescriptionComponentController() {

        return myDescriptionComponentController;

    }
    
    
    // This method is called when the user clicks the Convert button on the VF Page
    public PageReference convertLead() {
    
    // This is the lead convert object that will convert the lead 
        Database.LeadConvert leadConvert = new database.LeadConvert();
        
        // if a due date is set but the subject is not, then show an error 
        if (myTaskComponentController != null && myTaskComponentController.taskID.ActivityDate != null && string.isBlank(myTaskComponentController.taskID.Subject)){
            
            PrintError('You must enter a Subject if a Due Date is set..');
            return null;
            
        } 
        
        // if Lead Status is not entered show an error  
        if (myComponentController != null && myComponentController.leadConvert.Status == 'NONE'){
            
            PrintError('Please select a Lead Status.');
            return null;
            
        } 
        
        //set lead ID
        leadConvert.setLeadId(leadToConvert.Id);    
        
        //if the main lead convert component is not set then return
        if (myComponentController == NULL) return null;
        
        //if the Account is not set, then show an error
        if (myComponentController.selectedAccount == 'NONE')
        {
            PrintError('Please select an Account.');
            return null;
            
        }
        
        // otherwise set the account id
        else if (myComponentController != NULL && myComponentController.selectedAccount != 'NEW') {
            leadConvert.setAccountId(myComponentController.selectedAccount);
        }
        
        //set the lead convert status
        leadConvert.setConvertedStatus(myComponentController.leadConvert.Status);
        
        //set the variable to create or not create an opportunity
        leadConvert.setDoNotCreateOpportunity(myComponentController.doNotCreateOppty);
        
        //set the Opportunity name
        leadConvert.setOpportunityName(((myComponentController.doNotCreateOppty) 
            ? null : myComponentController.opportunityID.Name));
        
        //set the owner id
        leadConvert.setOwnerId(myComponentController.contactId.ownerID);
        
        //set whether to have a notification email
        leadConvert.setSendNotificationEmail(myComponentController.sendOwnerEmail);
        
        system.debug('leadConvert --> ' + leadConvert);
        
        //convert the lead
        Database.LeadConvertResult leadConvertResult = Database.convertLead(leadConvert);
        
        // if the lead converting was a success then create a task
        if (leadConvertResult.success)
        {
          // make sure that the task information component is being used and check to see if the user has filled out the Subject field 
            if(myTaskComponentController != NULL 
                && myDescriptionComponentController != NULL 
                && myTaskComponentController.taskID.subject != null)
            {
              //create a new task
                Task taskToCreate = new Task();
                
                //set whether there is a reminder
                taskToCreate.IsReminderSet = myTaskComponentController.remCon.taskID.IsReminderSet;
                
                //if the reminder is set, and the reminder's date is set
                if (taskToCreate.IsReminderSet 
                    && myTaskComponentController.remCon.taskID.ActivityDate != null) {
          
          //set the reminder time based on the reminder class's ActivityDate
          //The date and time in the reminder class is converted into a datetime by the convertToDatetime() method
                    taskToCreate.ReminderDateTime = 
                        convertToDatetime(
                            myTaskComponentController.remCon.taskID.ActivityDate,
                            myTaskComponentController.remCon.reminderTime
                        );
                    system.debug('taskToCreate.ReminderDateTime --> ' + taskToCreate.ReminderDateTime);
                    
                }   
        
        //set the whatId to the Opportunity Id            
                taskToCreate.WhatId = leadConvertResult.getOpportunityId();
                
                //set the whoId to the contact Id
                taskToCreate.WhoId = leadConvertResult.getContactId();
                
                //set the subject
                taskToCreate.Subject = myTaskComponentController.taskID.Subject;
                
                //set the status
                taskToCreate.Status = myTaskComponentController.taskID.Status;
                
                //set the activity date 
                taskToCreate.ActivityDate = myTaskComponentController.taskID.ActivityDate;
                
                //set the Priority 
                taskToCreate.Priority = myTaskComponentController.taskID.Priority;
                
                //set the custom field Primary Resource (this is a custom field on the Task showing an example of adding custom fields to the page)
                taskToCreate.Primary_Resource__c = myTaskComponentController.taskID.Primary_Resource__c;
                
                //set the Description field which comes from the leadConvertTaskDescComponent
                taskToCreate.Description =  myDescriptionComponentController.taskID.Description;

        //if the sendNotificationEmail variable in the leadConvertTaskDescComponent class is set then send an email
                if (myDescriptionComponentController.sendNotificationEmail)
                {
                  //create a new DMLOptions class instance
                    Database.DMLOptions dmlo = new Database.DMLOptions();
                    
                    //set the trigger user email flag to true
                    dmlo.EmailHeader.triggerUserEmail = true;
                    
                    //insert the task
                    database.insert(taskToCreate, dmlo);
                }
                else
                {
                  //if the sendNotificationEmail field was not checked by the user then simply insert the task
                    insert taskToCreate;
                }
            }
            
            // redirect the user to the newly created Account
            PageReference pageRef = new PageReference('/' + leadConvertResult.getAccountId());
            
            pageRef.setRedirect(true);
            
            return pageRef; 
        }
        else
        {

            //if converting was unsucessful, print the errors to the pageMessages and return null
            System.Debug(leadConvertResult.errors);

            PrintErrors(leadConvertResult.errors);
            
            return null;
        }
        
        return null;

    }
    
    //this method will take database errors and print them to teh PageMessages 
    public void PrintErrors(Database.Error[] errors)
    {
        for(Database.Error error : errors)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, error.message);
            ApexPages.addMessage(msg);
        }
    }
    
    //This method will put an error into the PageMessages on the page
    public void PrintError(string error) {
        ApexPages.Message msg = new 
            ApexPages.Message(ApexPages.Severity.ERROR, error);
        ApexPages.addMessage(msg);
    } 
    
    // given a date and time, where time is a string this method will return a DateTime
    private DateTime convertToDatetime(Date d, string t) {
        String timeFormat = DateTimeUtility.LocaleToTimeFormatMap().get(UserInfo.getLocale());
        
        //if the local of the user uses AM/PM 
        if (timeFormat != null && timeFormat.endsWith('a')) {
          
          //split the time into 2 strings 1 time and 1 am r pm
            string [] reminderTime = t.split(' ');
            
            //split the time into hour and minute
            string hour = reminderTime[0].split(':')[0];
            string min = reminderTime[0].split(':')[1];
            
            //get the am or pm
            string AM_PM = reminderTime[1];
            
            //turn the hour into an integer
            integer hr = Integer.valueOf(hour);
            
            //if the am/pm part of the string is PM then add 12 hours
            if (AM_PM.equalsIgnoreCase('PM')) hr += 12;
            
            //return a new DateTime based on the above information
            return (
                DateTime.newInstance(
                    d, 
                    Time.newInstance(
                        hr, 
                        Integer.valueOf(min), 
                        0,
                        0
                    )
                )
            ); 
        }
        //If the user's local does not use AM/PM and uses 24 hour time
        else {
            
            //split the time by a : to get hour and minute
            string hour = t.split(':')[0];
            string min = t.split(':')[1];
            
            //turn the hour into an integer
            integer hr = Integer.valueOf(hour);
            
            //return a new DateTime based on the above information
            return (
                DateTime.newInstance(
                    d, 
                    Time.newInstance(
                        hr, 
                        Integer.valueOf(min), 
                        0,
                        0
                    )
                )
            ); 
        }
    }
}

The "Bypass New Business Tasks" field is a field on the Lead object. I'd like to map it to there.