Thursday, January 23, 2014

Merge Accounts

Hello Salesforce guys.
Now time to post something new. Today i am going to post Apex code for Merge Account. First of all the exact mean of Merge Account. As we all know that Account have related list of Contact , Opportunity , Attachment, Activity, Events and Notes(May me related list of custom object). Now we have to merge merge Contact , Opportunity , Attachment, Activity, Events and Notes(May be related list of custom object) of any two Accounts in a single Account. For example we have two Accounts A and B. We have to merge Contact , Opportunity , Attachment, Activity, Events and Notes(May be related list of custom object) of both A and B into B.
To implement this we need following Apex Controller and VF page.

VF page :- 

<apex:page controller="MergeAccount" tabStyle="Account">
 <apex:sectionHeader title="Account" subtitle="Merge"/>
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockButtons >
              <apex:commandButton value="Merge" action="{!add}"/>
         </apex:pageBlockButtons>
          FROM-:<apex:selectList value="{!selectList}" size="1">
            <apex:selectOptions value="{!options}"/> 
          </apex:selectList>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          TO-:<apex:selectList value="{!selectListTo}" size="1">
            <apex:selectOptions value="{!optionsTo}"/> 
          </apex:selectList>
      </apex:pageBlock>
  </apex:form>

</apex:page>

Controller :- 
public class MergeAccount{
    public List<Account> accListFrom {get; set;}
    public List<Account> accListTo {get; set;}
    public string selectList {get; set;}
    public string selectListTo {get; set;}
    public List<selectOption> options {get; set;}
    public List<selectOption> optionsTo {get; set;}
    Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
    public List<String> SelectedFields {get; set;}
    public Account acc {get; set;}
    public Account accTo {get; set;}
    public Test__c tec {get; set;}
    public MergeAccount(){
        accListFrom = new List<Account>();
        accListTo = new List<Account>();
        options = new List<selectOption>();
        optionsTo = new List<selectOption>();
        SelectedFields = new List<String>() ;
        accListFrom = [select id, name from account];
        accListTo = [select id, name from account];
        
        for(Account ac : accListFrom){
            options.add(new selectOption(ac.id, ac.name));
        }
        for(Account ac : accListTo){
            optionsTo.add(new selectOption(ac.id, ac.name));
        }
        
    }
    public Pagereference add(){
        List<Contact> conFrom = new List<Contact>();
        conFrom = [select id, name, AccountId from contact where AccountId =: selectList];
        if(conFrom.size() > 0){
            for(Contact con : conFrom){
                con.AccountId = selectListTo;
            }
        }
        update conFrom;
        List<Opportunity> oppList = new List<Opportunity>();
        oppList = [select id, name , AccountId from Opportunity where AccountId =: selectList];
        if(oppList.size() > 0){
            for(Opportunity opp : oppList){
                opp.AccountId = selectListTo;
            }
        }
        update oppList;
        List<Case> ca = new List<Case>();
        ca = [select id,  AccountId from case where AccountId =: selectList];
        if(ca.size() > 0){
            for(Case cs : ca){
                cs.AccountId = selectListTo;
            }
        }
        update ca;
        List<Task> tsk = new List<Task>();
        tsk = [select id, AccountId, WhatId from task where AccountId =: selectList];
        if(tsk.size() > 0){
            for(task tk : tsk){
                tk.WhatId = selectListTo;
            }
        }
        update tsk;
        List<Event> evnt = new List<Event>();
        evnt = [select id, AccountId, whatId from event where AccountId =: selectList];
        if(evnt.size() > 0){
            for(Event ev : evnt){
                ev.whatId = selectListTo;
            }
        }
        update evnt;
        List<Attachment> attach = new List<Attachment>();
        attach = [select id, name , parentid, body from Attachment where parentId =: selectList];
        List<Attachment> attachmnt = new List<Attachment>();
        if(attach.size() > 0){
            for(Attachment att : attach){
                Attachment attch = new Attachment();
                attch.body = att.body;
                attch.name = att.name;
                attch.Parentid = selectListTo;
                attachmnt.add(attch);
            }
            insert attachmnt;
            delete attach;
        List<Note> notes = new List<Note>(); 
        notes =  [select id,   parentid, body, title from Note where parentId =: selectList]; 
        List<Note> noter = new List<Note>(); 
        if(notes.size() > 0){
            for(Note nt : notes){
                Note nts = new Note();
                nts.body = nt.body;
                nts.title = nt.title;
                nts.parentId = selectListTo;
                noter.add(nts);
            }
            insert noter;
            delete notes;
        } 
        }    
        return null;
    }
    

}

No comments:

Post a Comment