global class BatchMergeSubscription implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.
String query = 'select id, OrderApi__Contact__r.Name, OrderApi__Status__c, OrderApi__Item__r.Name, OrderApi__Subscription_Plan__
return Database.getQueryLocator(
}
global void execute(Database.
Map<String, OrderApi__Subscription__c> mapSub = new Map<String, OrderApi__Subscription__c>();
List<OrderApi__Subscription__
String condition = '';
for(OrderApi__Subscription__c sub : subList){
condition = '' + sub.OrderApi__Contact__r.Name + ' ' + sub.OrderApi__Status__c + ' ' + sub.OrderApi__Item__r.Name + ' ' + sub.OrderApi__Subscription_
//Checking subscriptions based on above conditions
if(mapSub.containsKey(
OrderApi__Subscription__c chk = mapSub.get(condition);
//Checking subscriptions based on Start Date
if(sub.OrderApi__Current_Term_
//Checking subscriptions based on the total dues
//if dues are equals delete the sub record
if(sub.OrderApi__Subscription_
toDeleteSub.add(sub);
}else{
//if dues are not equal compare sub's dues with the chk's dues
//if sub's dues > chk's dues
//delete chk's dues
if(sub.OrderApi__Subscription_
mapSub.put(condition, sub);
toDeleteSub.add(chk);
}//else delete sub's dues
else{
toDeleteSub.add(sub);
}
}
}//if Start date are not equals than compare the start dates
else{
//if sub.startdate > chk.startdate
//delete chk.startdate
//Calculate days of lapse and add all
Integer totalDays = 0;
if(sub.OrderApi__Current_Term_
totalDays = (Integer)(sub.OrderApi__Days_
Date d = sub.OrderApi__Current_Term_
sub.OrderApi__Current_Term_
mapSub.put(condition, sub);
toDeleteSub.add(chk);
}//else delete sub.startdate
else{
totalDays = (Integer)(sub.OrderApi__Days_
Date d = sub.OrderApi__Current_Term_
chk.OrderApi__Current_Term_
mapSub.put(condition, chk);
toDeleteSub.add(sub);
}
}
}else{ //if conditions doesn't exists than add the conditions
mapSub.put(condition, sub);
}
}
if(toDeleteSub.size()>0){
System.debug('To Delete');
for(OrderApi__Subscription__c sub : toDeletesub){
System.debug('Subscription to deleted : ' + sub);
}
delete toDeleteSub;
}
}
global void finish(Database.
}
}
No comments:
Post a Comment