To Clone a Contact or any other object in Salesforce we need a VisualForce Page and a Controller Class
here we have a VisualForce Page and controller extension for the VisualForce Page
After creating VisualForce Page we call our VisualForce Page according to our need either on button click or tab click.
Here Is Code To Clone a Contact
<apex:page standardController="Contact" extensions="ContactCloneController" id="pageId"> <apex:form id="formId"> <apex:pagemessages ></apex:pagemessages> <apex:pageBlock title="Contact Edit"> <apex:pageBlockButtons > <apex:commandButton action="{!customSave}" value="Save"/> <button class="btn" style="padding:4px;" onclick="window.open('/{!$currentpage.parameters.id}', target='_self');return false;">Cancel</button> </apex:pageBlockButtons> <apex:pageBlockSection title="Contact Information" collapsible="false" > <apex:pageBlockSectionItem > <apex:outputText value="First Name"/> <apex:outputPanel > <apex:inputfield value="{!C1.Salutation}" /> <apex:inputField value="{!C1.FirstName}" style="margin-left:5px;"/> </apex:outputPanel> </apex:pageBlockSectionItem> <apex:inputField value="{!C1.phone}"/> <apex:inputField value="{!C1.LastName}"/> <apex:inputField value="{!C1.Contact_Direct_Phone__c}"/> <apex:inputField value="{!C1.AccountId}"/> <apex:inputField value="{!c1.Company_Main_Phone__c}"/> <apex:inputField value="{!c1.Title}"/> <apex:inputField value="{!c1.Extension__c}"/> <apex:inputField value="{!c1.Email}"/> <apex:inputField value="{!c1.MobilePhone}"/> <apex:inputField value="{!c1.Company_Website__c}"/> <apex:inputField value="{!c1.Decision_Maker_Status__c}"/> <apex:inputtext value="{!c1.Client__c}"/> <apex:inputField value="{!c1.Lead_Status__c}"/> <apex:inputField value="{!c1.Next_Call_Time__c}"/> <apex:inputField value="{!c1.OwnerId}"/> <apex:inputField value="{!c1.Data_Source__c}"/> <apex:inputField value="{!c1.Disable_Auto_Email__c}"/> <apex:inputField value="{!c1.Title_Group__c}"/> <apex:inputField value="{!c1.LID__LinkedIn_Company_Id__c}" style="width:250px;"/> <apex:inputField value="{!c1.Management_Level__c}"/> <apex:inputField value="{!c1.LID__LinkedIn_Member_Token__c}" style="width:250px;"/> </apex:pageBlockSection> <apex:pageBlockSection title="Referral Groups" collapsible="false" > <apex:inputtext value="{!c1.RG_Lead_Status__c}"/> <apex:inputField value="{!c1.Attended_a_Meeting__c}"/> <apex:inputField value="{!c1.Clear_Next_Step__c}"/> <apex:inputField value="{!c1.Commission_Paid__c}"/> <apex:inputField value="{!c1.Lead_Source__c}"/> <apex:inputField value="{!c1.Evaluation_Call_Made__c}"/> <apex:inputField value="{!c1.Group_City__c}"/> <apex:inputField value="{!c1.Linkedin_URL__c}"/> <apex:inputField value="{!c1.Group_State__c}"/> <apex:inputField value="{!c1.Member_Hierarchy__c}"/> <apex:inputField value="{!c1.Automation__c}"/> <apex:outputField value="{!c1.Referred_by__c}"/> <apex:inputField value="{!c1.Next_Data_Search__c}"/> </apex:pageBlockSection> <apex:pageBlockSection columns="1"> <apex:inputField value="{!c1.Market_Segment__c}"/> </apex:pageBlockSection> <apex:pageBlockSection title="Swagelok" collapsible="false" > <apex:inputtext value="{!c1.First_Call__c}"/> <apex:inputField value="{!c1.Swagelok_Lead_Status__c}"/> <apex:inputField value="{!c1.X2nd_Call__c}"/> <apex:inputField value="{!c1.Swagelok_Salesperson__c}"/> <apex:inputField value="{!c1.X3rd_Call__c}"/> <apex:inputField value="{!c1.Swagelok_Order_Date__c}"/> <apex:inputField value="{!c1.Swagelok_Buyer_Phase__c}"/> </apex:pageBlockSection> <apex:pageBlockSection columns="1" title="System Information" collapsible="false" > <apex:inputField value="{!c1.Trigger_Edit__c}"/> </apex:pageBlockSection> <apex:pageBlockSection title="Address Information" columns="1" collapsible="false" > <apex:inputField value="{!c1.MailingStreet}" /> <apex:inputField value="{!c1.MailingCity}"/> <apex:inputField value="{!c1.MailingState}"/> <apex:inputField value="{!c1.MailingPostalCode}"/> <apex:inputField value="{!c1.MailingCountry}"/> </apex:pageBlockSection> </apex:pageblock> </apex:form> </apex:page>
here is our apex controller for this visualforce page
public class ContactCloneController { public contact con; public contact C1 {get;set;} public ContactCloneController (ApexPages.StandardController stdController) { Id conId = ApexPages.currentPage().getParameters().get('id'); C1 = new Contact(); con = [Select phone,FirstName,Salutation,LastName,Contact_Direct_Phone__c,AccountId,Company_Main_Phone__c, Title,Extension__c,Email,MobilePhone,Company_Website__c,Decision_Maker_Status__c,Client__c, Lead_Status__c,Next_Call_Time__c,OwnerId,Data_Source__c,Disable_Auto_Email__c,Total_Activities__c, Title_Group__c,LID__LinkedIn_Company_Id__c,Management_Level__c,LID__LinkedIn_Member_Token__c, RG_Lead_Status__c,Attended_a_Meeting__c,Clear_Next_Step__c,Commission_Paid__c,Lead_Source__c, Evaluation_Call_Made__c,Group_City__c,Linkedin_URL__c,Group_State__c,Member_Hierarchy__c, Market_Segment__c,Automation__c,Referred_by__c,Next_Data_Search__c,First_Call__c,Swagelok_Lead_Status__c, X2nd_Call__c,Swagelok_Salesperson__c,X3rd_Call__c,Swagelok_Order_Date__c,Swagelok_Buyer_Phase__c,Trigger_Edit__c, MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry From contact Where Id =:conId ]; //Cloning the above Contact Record con C1= con.clone(false, false, false, false); C1.Lead_Status__c = 'Pipeline'; C1.Lead_Source__c = 'Referral'; C1.Total_Activities__c = 0; con.Lead_Status__c = 'Referred'; C1.Referred_by__c = con.Id; C1.Referred_by_Text__c = Con.FirstName+' '+con.Lastname; c1.X2nd_Call__c=False; c1.X3rd_Call__c=False; } public PageReference customSave() { PageReference pr; try{ database.upsert(new list<Contact>{C1,con}); pr = new PageReference('/'+C1.Id ); }catch( Exception e ){ ApexPages.addMessages(e); } return pr; } }
Hits: 1268