Showing posts with label Duplicate Records. Show all posts
Showing posts with label Duplicate Records. Show all posts

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.......

Monday, 18 March 2013

Display Duplicate Records count

Here We have a scenario.

In Account object we have lot of records. If we need to know , how many  records with the same name present in your object.

Record Name and that record count..

Please look at this code.

Vf Page:-
----------

<apex:page controller="test4">
 <apex:pageblock title="Map Usage On VF">
     <apex:pageBlockTable value="{!data}" var="d">
         <apex:column headerValue="Account Name">
             {!d}
         </apex:column>
         <apex:column headerValue="Duplicate Count">
             {!data[d]}
         </apex:column>
     </apex:pageBlockTable>
 </apex:pageblock>
</apex:page>


Controller
------------

public class test4{
 
    public map<string,integer> data {get;set;}
    public test4(){
        data = new map<string,integer>();
        for(Account acc: [Select Id, Name, (Select Id, Name, Email from Contacts), Phone from Account]){
            integer count = data.get(acc.name);
            if(count != null)
                count++;
            else
                count = 1;
            data.put(acc.name, count);
        }
    }
}