/**********************************************************************************************************
* Created By : Harshit Garg
* Created Date : 20/Mar/2018
* Description : This Class is for Person Account Merging.
***********************************************************************************************************/
public class ctrlAccountMerging
{
public string strAccSearchString {get; set;}
public Boolean pbhide {get;set;}
public Boolean pbhide1 {get;set;}
public Boolean Flag{get;set;}
public List<Account> lstAccount {get; set;}
public List<Account> selectedAccount {get; set;}
public Account mergeAccount {get; set;}
public List<Account> mergeAccWithoutCheckbox {get; set;}
public Boolean isResultDisplay {get; set;}
Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
// constructor
public ctrlAccountMerging(){
lstAccount = new List<Account>();
isResultDisplay = false;
pbhide=true;
pbhide1=false;
Flag=false;
}
/**********************************************************************************************************
* Created By : Harshit Garg
* Created Date : 20/Mar/2018
* Description : This Method is for Account searching on the basis of FirstName,LastName,Email
/***********************************************************************************************************/
public void SearchAccountRec(){
isResultDisplay = true;
pbhide=false;
string strLikeString = '%'+strAccSearchString+'%';
string strSOQL = 'Select id,FirstName,LastName,Flyte_Customer_Id__c,Alternate_Email__c,Alternate_Phone__c,Selected_Account__c,RecordType.name,RecordType.id,Brand__c,Phone,PersonEmail,Social_Post_URL__c from Account where (FirstName LIKE: strLikeString and RecordType.id =:devRecordTypeId) OR (LastName LIKE: strLikeString and RecordType.id =:devRecordTypeId) OR (PersonEmail LIKE: strLikeString and RecordType.id =:devRecordTypeId)';
lstAccount = database.query(strSOQL);
}
/**********************************************************************************************************
* Created By : Harshit Garg
* Created Date : 21/Mar/2018
* Description : This Method is for getting selected Account from "lstAccount" List.
/***********************************************************************************************************/
public void AccountSelectedRec(){
pbhide=true;
Flag=true;
selectedAccount = new List<Account>(); //List of Selected Accounts
for(Account acc: lstAccount)
{
if(acc.Selected_Account__c==true && acc.RecordType.id==devRecordTypeId)
{
acc.Selected_Account__c=false;
selectedAccount.add(acc);
}
}
//This error message is for when user will not select any record
if(selectedAccount.size()==0)
{
pbhide=false;
selectedAccount.clear();
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select the records.'));
}
//This error message is for when user will select only one record
else
{
if(selectedAccount.size()==1)
{
pbhide=false;
selectedAccount.clear();
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Select 2 Records, You have selected only 1 record'));
}
//This error message is for when user will select record above to limit
if(selectedAccount.size()>2)
{
pbhide=false;
selectedAccount.clear();
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Sorry, you can merge maximum 2 records at a time.'));
}
}
}
/**********************************************************************************************************
* Created By : Harshit Garg
* Created Date : 23/Mar/2018
* Description : This Method is for Merging Master and slave Account help of "Database.Merge"
/***********************************************************************************************************/
Public void DatabaseMergeAccount()
{
pbhide1=true;
mergeAccount = new Account();
for(Account acc1:selectedAccount) //This for loop is for getting Master from selectedAccount List which Checkbox will check
{
if(acc1.Selected_Account__c==true && acc1.RecordType.id==devRecordTypeId)
{
mergeAccount = acc1;
}
}
//This error is Message for when user will not select any Account as a Master
if(mergeAccount.id == Null)
{
pbhide1=false;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select your Master Record'));
}
mergeAccWithoutCheckbox = new List<Account>(); //List used to getting Slave Account Record which checkbox will false
for(Account acc2:selectedAccount) // This for loop is for to getting slave Account
{
if(acc2.Selected_Account__c==false && acc2.RecordType.id==devRecordTypeId)
{
mergeAccWithoutCheckbox.add(acc2);
}
}
List<Account> AccountToMerge=new list<Account>();//This List is getting both record which we will merge
integer Count=0; //We are intilizing Count as 0
//This for loop is geeting slave which checkbox is not true and checking the count
for(Account a :mergeAccWithoutCheckbox)
{
if(Count <2 && a.RecordType.id==devRecordTypeId)
{
AccountToMerge.add(a);
Count++;
}
else
{
Count = 0;
}
//In this if condition loop is for Merging Master and slave
if(mergeAccount.id != Null)
{
Account mergeAcc2 = [Select id,FirstName,Alternate_Email__c,Flyte_Customer_Id__c,Alternate_Phone__c,LastName,Brand__c,Phone,PersonEmail,Social_Post_URL__c from Account where id=:mergeAccount.id];
Set<id> accIds = new Set<id>();
for(Account a1:AccountToMerge)
{
accIds.add(a1.id);
}
List<Account> AccountToMerge2 = [Select id,FirstName,Alternate_Email__c,Flyte_Customer_Id__c,Alternate_Phone__c,LastName,Brand__c,Phone,PersonEmail,Social_Post_URL__c from Account where id in:accIds];
for(Account acc1:[select id,FirstName,Alternate_Email__c,Flyte_Customer_Id__c,Alternate_Phone__c,LastName,Brand__c,Phone,PersonEmail,Social_Post_URL__c from Account])
{
acc1 = mergeAcc2;
Integer i=0;
if(i<AccountToMerge2.size() && acc1.PersonEmail==null)
{
acc1.PersonEmail = AccountToMerge2[i].PersonEmail;
}
if(i<AccountToMerge2.size() && acc1.Social_Post_URL__c==null)
{
acc1.Social_Post_URL__c = AccountToMerge2[i].Social_Post_URL__c;
}
if(i<AccountToMerge2.size() && acc1.Phone==null)
{
acc1.Phone = AccountToMerge2[i].Phone;
}
if(i<AccountToMerge2.size() && acc1.FirstName==null)
{
acc1.FirstName = AccountToMerge2[i].FirstName;
}
if(i<AccountToMerge2.size() && acc1.Alternate_Email__c==null)
{
acc1.Alternate_Email__c = AccountToMerge2[i].Alternate_Email__c;
}
if(i<AccountToMerge2.size() && acc1.Alternate_Phone__c==null)
{
acc1.Alternate_Phone__c = AccountToMerge2[i].Alternate_Phone__c;
}
if(i<AccountToMerge2.size() && acc1.Flyte_Customer_Id__c==null)
{
acc1.Flyte_Customer_Id__c = AccountToMerge2[i].Flyte_Customer_Id__c;
}
}
//From this we are achieving Merging of Master and Slave Account
Database.merge(mergeAcc2,AccountToMerge2);
}
}
}
}
Vfpage
<!----------------This hole vfpage is doing Account Merging and "ctrlAccountMerging" is controller of this vfPage------------> <apex:page controller="ctrlAccountMerging" sidebar="false" tabStyle="Account"> <!----------------From here Script is Start and Through this script checkbox will work like Radio button----------------------> <script> function oneCheckbox(obj) { if (obj.checked) { var inputs = document.getElementsByTagName("input"); for (var i = 0; i < inputs.length; i++) { var oInput = inputs[i]; if (oInput.type == "checkbox" && oInput.checked && oInput != obj) { oInput.checked = false; } } } } </script> <!--------------------------------------------------------End Of Script--------------------------------------------------------> <!--------------------------------------------------------From here Vf Page start-----------------------------------------------> <apex:form id="frm"> <!-------------------------------------This PageBlock is for Searching Accounts-------------------------------------------------> <apex:pageBlock title="Step 1. Find the duplicate records" id="pb"> <apex:inputtext value="{!strAccSearchString}"/> <apex:commandButton value="Find Accounts" action="{!SearchAccountRec}"/> </apex:pageBlock> <!-------------------------------------This PageBlock is Select Merge Account---------------------------------------------------> <apex:pageMessages id="showmsg"/> <apex:pageBlock rendered="{!If(lstAccount.size>0,true,false) && !(pbhide)}" id="pb2" title="Step 2. Select the records to merge"> <apex:pageBlockButtons location="Top"> <apex:commandButton value="Select Merge Accounts" action="{!AccountSelectedRec}" rerender="showmsg,frm"/> </apex:pageBlockButtons> <apex:pageBlockTable value="{!lstAccount}" var="acc"> <apex:column > <apex:facet name="header">Selected Account</apex:facet> <apex:inputField value="{!acc.Selected_Account__c}"/> </apex:column> <apex:column > <apex:facet name="header">First Name</apex:facet> <apex:outputLink value="/{!acc.FirstName}">{!acc.FirstName}</apex:outputLink> </apex:column> <apex:column > <apex:facet name="header">Last Name</apex:facet> <apex:outputLink value="/{!acc.LastName}">{!acc.LastName}</apex:outputLink> </apex:column> <apex:column > <apex:facet name="header">Brand</apex:facet> <apex:outputLink value="/{!acc.Brand__c}">{!acc.Brand__c}</apex:outputLink> </apex:column> <apex:column > <apex:facet name="header">Email</apex:facet> <apex:outputLink value="/{!acc.PersonEmail}">{!acc.PersonEmail}</apex:outputLink> </apex:column> <apex:column > <apex:facet name="header">Social Post Url</apex:facet> <apex:outputLink value="/{!acc.Social_Post_URL__c}">{!acc.Social_Post_URL__c}</apex:outputLink> </apex:column> <apex:column > <apex:facet name="header">Record Type</apex:facet> <apex:outputLink value="/{!acc.RecordType.Name}">{!acc.RecordType.Name}</apex:outputLink> </apex:column> </apex:pageBlockTable> </apex:pageBlock> <apex:pageBlock rendered="{!If(lstAccount.size==0 && isResultDisplay == true,true,false)}" title="Search Result"> <apex:outputLabel value="No Result Found"></apex:outputLabel> </apex:pageBlock> <!----------------------------This PageBlock is getting Select Record From Step-2 and Merge the records-------------------------------> <apex:pageBlock rendered="{!If(lstAccount.size>0,true,false) && !(pbhide1)}" title="Step 3. Choose any Account as a Master record" id="pb1"> <apex:pageBlockButtons location="Top"> <apex:commandButton value="Process To Merge" action="{!DatabaseMergeAccount}" id="b2" rendered="{!Flag}"/> </apex:pageBlockButtons> <apex:pageBlockTable value="{!selectedAccount}" var="acc1" > <apex:column width="25px"> <apex:facet name="header">Master Account</apex:facet> <apex:inputField value="{!acc1.Selected_Account__c}" onchange="oneCheckbox(this);" /> </apex:column> <apex:column > <apex:facet name="header">First Name</apex:facet> <apex:outputLink value="/{!acc1.FirstName}">{!acc1.FirstName}</apex:outputLink> </apex:column> <apex:column > <apex:facet name="header">Last Name</apex:facet> <apex:outputLink value="/{!acc1.LastName}">{!acc1.LastName}</apex:outputLink> </apex:column> <apex:column > <apex:facet name="header">Brand</apex:facet> <apex:outputLink value="/{!acc1.Name}">{!acc1.Name}</apex:outputLink> </apex:column> <apex:column > <apex:facet name="header">Brand</apex:facet> <apex:outputLink value="/{!acc1.Brand__c}">{!acc1.Brand__c}</apex:outputLink> </apex:column> <apex:column > <apex:facet name="header">Email</apex:facet> <apex:outputLink value="/{!acc1.PersonEmail}">{!acc1.PersonEmail}</apex:outputLink> </apex:column> <apex:column > <apex:facet name="header">Social Post Url</apex:facet> <apex:outputLink value="/{!acc1.Social_Post_URL__c}">{!acc1.Social_Post_URL__c}</apex:outputLink> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>
Test Class
@isTest
private class ctrlAccountMerging_Test{
@testSetup
static void setupTestData(){
test.startTest();
Account account_Obj = new Account(LastName = 'LastName997', FirstName = 'First690', RecordTypeId = '01228000000Q0aUAAS', Brand__c = 'Allen Solly', Selected_Account__c = True);
Insert account_Obj;
Account account_Obj1 = new Account(LastName = 'LastName997', FirstName = 'First690', RecordTypeId = '01228000000Q0aUAAS', Brand__c = 'Allen Solly', Selected_Account__c = false);
Insert account_Obj1;
test.stopTest();
}
static testMethod void test_SearchAccountRec_UseCase1(){
List<Account> account_Obj = [SELECT LastName,FirstName,RecordType.Id,PersonEmail,Brand__c,Social_Post_URL__c,Flyte_Customer_Id__c,Alternate_Phone__c,Alternate_Email__c,Selected_Account__c from Account];
System.assertEquals(true,account_Obj.size()>0);
ctrlAccountMerging obj01 = new ctrlAccountMerging();
obj01.strAccSearchString = 'test data';
obj01.pbhide = false;
obj01.pbhide1 = false;
obj01.Flag = false;
obj01.lstAccount = account_Obj;
obj01.selectedAccount = account_Obj;
obj01.mergeAccount = account_Obj[0];
obj01.mergeAccWithoutCheckbox = account_Obj;
obj01.isResultDisplay = false;
obj01.SearchAccountRec();
}
static testMethod void test_AccountSelectedRec_UseCase1(){
List<Account> account_Obj = [SELECT LastName,FirstName,RecordType.Id,PersonEmail,Brand__c,Social_Post_URL__c,Flyte_Customer_Id__c,Alternate_Phone__c,Alternate_Email__c,Selected_Account__c from Account];
System.assertEquals(true,account_Obj.size()>0);
ctrlAccountMerging obj01 = new ctrlAccountMerging();
obj01.strAccSearchString = 'test data';
obj01.pbhide = false;
obj01.pbhide1 = false;
obj01.Flag = false;
obj01.lstAccount = account_Obj;
obj01.selectedAccount = account_Obj;
obj01.mergeAccount = account_Obj[0];
obj01.mergeAccWithoutCheckbox = account_Obj;
obj01.isResultDisplay = false;
obj01.AccountSelectedRec();
}
static testMethod void test_DatabaseMergeAccount_UseCase1(){
List<Account> account_Obj = [SELECT LastName,FirstName,RecordType.Id,PersonEmail,Brand__c,Social_Post_URL__c,Flyte_Customer_Id__c,Alternate_Phone__c,Alternate_Email__c,Selected_Account__c from Account];
System.assertEquals(true,account_Obj.size()>0);
ctrlAccountMerging obj01 = new ctrlAccountMerging();
obj01.strAccSearchString = 'test data';
obj01.pbhide = false;
obj01.pbhide1 = false;
obj01.Flag = false;
obj01.lstAccount = account_Obj;
obj01.selectedAccount = account_Obj;
obj01.mergeAccount = account_Obj[0];
obj01.mergeAccWithoutCheckbox = account_Obj;
obj01.isResultDisplay = false;
obj01.DatabaseMergeAccount();
}
}