public class BatchForMergeAccounts implements Database.Batchable<BatchForMergeAccounts.wrapMergeAccount>{
List<wrapMergeAccount> newRecords;
public BatchForMergeAccounts(){
Document accdocCSV = [SELECT Id, Name, Body FROM Document WHERE Name = 'AccountMergeCSV' LIMIT 1];
newRecords = new List<wrapMergeAccount>();
System.debug('accdocCSV: ' + accdocCSV);
System.debug('accdocCSVBody: ' + string.valueof(accdocCSV.Body));
List<List<String>> csvRecords = CSVReader.doParse(accdocCSV.Body);
System.debug('csvRecords: ' + csvRecords);
import(csvRecords);
}
public Iterable<wrapMergeAccount> start(Database.BatchableContext info) {
system.debug('newRecords-->'+newRecords);
return newRecords;
}
public void execute(Database.BatchableContext context, List<BatchForMergeAccounts.wrapMergeAccount> records) {
list<account> mergeaccountlist = [select id,(select id,ParentId from ChildAccounts),(select id,accountid from contacts),(select id,accountid from Opportunities),(select id,account__c from leads__r) from account where id =:records[0].DuplicateAccountID];
list<account> acclist =new list<account>();
list<contact> conlist =new list<contact>();
list<Opportunity> opplist =new list<Opportunity>();
list<Lead> leadlist = new list<Lead>();
list<account> SurvivingAccountlist = [SELECT Id, Name FROM Account WHERe id =:records[0].SurvivingAccountID LIMIT 1];
system.debug('mergeaccountlist-->'+mergeaccountlist);
if(mergeaccountlist.size()>0 && SurvivingAccountlist.size()>0){
if(mergeaccountlist[0].ChildAccounts!= null){
for(Account acc:mergeaccountlist[0].ChildAccounts){
acc.ParentId = records[0].SurvivingAccountID;
acclist.add(acc);
}
}
/*if(mergeaccountlist[0].contacts!= null){
for(contact con:mergeaccountlist[0].contacts){
con.accountid = records[0].SurvivingAccountID;
conlist.add(con);
}
}*/
if(mergeaccountlist[0].Opportunities!= null){
for(Opportunity opp:mergeaccountlist[0].Opportunities){
opp.accountid = records[0].SurvivingAccountID;
opplist.add(opp);
}
}
if(mergeaccountlist[0].leads__r!= null){
for(Lead led:mergeaccountlist[0].leads__r){
led.account__c = records[0].SurvivingAccountID;
leadlist.add(led);
}
}
if(opplist.size()>0){
database.update(opplist, false);
}
if(leadlist.size()>0){
database.update(leadlist, false);
}
merge SurvivingAccountlist[0] mergeaccountlist[0];
if(acclist.size()>0){
database.update(acclist, false);
}
/*if(conlist.size()>0){
database.update(conlist, false);
}*/
}
}
public void finish(Database.BatchableContext context) {
}
public void import(List<List<String>> csvRecords) {
List<String> fieldNames = csvRecords.remove(0);
System.debug('fieldNames-->'+fieldNames);
import(csvRecords, fieldNames);
}
public void import(List<List<String>> csvRecords, List<String> fieldNames) {
System.debug('csvRecords: ' + csvRecords);
System.debug('fieldNames: ' + fieldNames);
while (csvRecords.size() > 0) {
wrapMergeAccount newRecord = new wrapMergeAccount();
List<String> values = csvRecords.remove(0);
//System.debug('values--->' + values);
if (values.size() > 0) {
newRecord.DuplicateAccountID = values.get(0);
newRecord.SurvivingAccountID = values.get(1);
newRecords.add(newRecord);
System.debug('newRecords--->' + newRecord);
}
}
}
public class wrapMergeAccount {
public string DuplicateAccountID {get; set;}
public string SurvivingAccountID {get; set;}
}
}
0 Comments