Wednesday, 16 July 2014

Clone Button in Related List :- 


Task:-
------
The Clone button on a Account Related List of Contact, quickly creates a new contact with the same information as the existing contact, for example, when you need to add multiple contacts for the same account.

Here put a button on the related list of a Account object for the Contact to clone the selected records.

Step 1:- Create VF page and Class

<apex:page standardController="Contact" recordSetVar="record" extensions="clone_contacts" action="{!cloneprodetails}">

</apex:page>

             
 public class clone_contacts {
    Public List<Contact> Con{get;set;}
    Public Set<Id> accids=new Set<Id>();
    ApexPages.StandardSetController controller;
    public clone_contacts(ApexPages.StandardSetController controller) {
        try{           this.controller= controller;
         
           con= new List<Contact>();
           for (Contact c: (List<Contact>)Controller.getSelected()){
               accids.add(c.Id);
           }
           String dquery='Select ';
           Set<String> con_fields=Contact.SObjectType.getDescribe().fields.getMap().keySet();
           Integer temp = 1;
           for(String str:con_fields){
               dquery+=str;
               if(temp <con_fields.size()){
                   dquery+=',';
                   temp++;          
               }
           }
         
           dquery+=' from Contact where id in:accids';
           if(!accids.isempty())
               con=Database.query(dquery);
       }
       catch(Exception e){}
    }
    public PageReference cloneprodetails() {
       PageReference pageref;
       try{
           List<Contact> newRecords=new List<Contact>();
           for(Contact record:con) {
               newRecords.add(record.clone(false,true,false,false));
           }
           insert newRecords;
         
           pageref=new PageReference('/'+ApexPages.CurrentPage().getParameters().get('returl'));
       }
       Catch(Exception e){}
       return pageref;
   }

}



Step 2:- Create a List Button on Contact object , See the image






Step 3:- Add the Button into Account Related List of Contact

 Go to Account Detail Page --> Edit Layout --> Related List --> Click on Contact Properties Symbol
   

Then You get the below page , Add that Button into Selected List




Click on Ok and Save that Layout..

Then You can See the Custom Clone Button in Related List

Step 4:- Select some Records And Click on that button then you can see the cloned records




Hope the above info may helpful to you.......

No comments:

Post a Comment