You need to sign in to do that
Don't have an account?
Abhi Malik
Please Help For Batch Class
Hi everyone
am trying to write a batch class for my project
when i will create a pledge for donation then based on the duration our transactions are created by trigger but those transactions are not more then 12 but, if my duration is 24 months then .it will create 12 transaction by trigger after that when my list size of transactions is less then 12 every month then it will create a new transaction for that 12 th month and repeat this process for the complete duration .
code:
global class BatchClassPledgeTransaction implements Database.batchable<sObject> {
String query;
list<Transaction__c > transList=new list<Transaction__c >();
// Start Method
global Database.querylocator start(Database.BatchableContext BC){
Query = 'Select id,name,Duration__c,Start_Date__c,End_Date__c from Pledge__c';
return Database.getQueryLocator(query);
}
// Execute Logic
global void execute(Database.BatchableContext BC, List<Pledge__c> Scope){
Id PledgeRecTypeCard = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('Card').getRecordTypeId();
Id PledgeRecTypeGIRO = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('GIRO').getRecordTypeId();
Id PledgeRecTypeOnline = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('Online').getRecordTypeId();
Id PledgeRecTypeRecurringCash = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('Recurring Cash').getRecordTypeId();
Id PledgeRecTypeRecurringCheque = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('Recurring Cheque ').getRecordTypeId();
Id TransactionRecTypeCard = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('Card').getRecordTypeId();
Id TransactionRecTypeGIRO = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('GIRO').getRecordTypeId();
Id TransactionRecTypeOnline = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('Online').getRecordTypeId();
Id TransactionRecTypeRecurringCash = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('Recurring Cash').getRecordTypeId();
Id TransactionRecTypeRecurringCheque = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('Recurring Cheque ').getRecordTypeId();
for(Pledge__c plg:Scope){
Integer i=0;
Integer loopiterater=0;
Integer j=integer.valueof(plg.Duration__c)-12;
Integer monthDiff = plg.Start_Date__c.monthsBetween(plg.End_Date__c);
loopiterater=monthDiff;
if(integer.valueof(plg.Duration__c)>12||loopiterater>12){
for(i=0;i<=j;i++){
Transaction__c tran= new Transaction__c ();
tran.Transaction_Date__c=plg.Start_Date__c.addMonths(i);
tran.Pledge__c=plg.id;
tran.Amount_Reconciled__c=plg.Amount__c;
tran.Donor__c=plg.Account__c;
tran.Status__c=plg.Status__c;
if(plg.recordtypeid==PledgeRecTypeCard){
tran.recordtypeid=TransactionRecTypeCard;
}
if(plg.recordtypeid==PledgeRecTypeGIRO){
tran.recordtypeid=TransactionRecTypeGIRO;
}
if(plg.recordtypeid==PledgeRecTypeOnline){
tran.recordtypeid=TransactionRecTypeOnline;
}
if(plg.recordtypeid==PledgeRecTypeRecurringCash){
tran.recordtypeid=TransactionRecTypeRecurringCash;
}
if(plg.recordtypeid==PledgeRecTypeRecurringCheque){
tran.recordtypeid=TransactionRecTypeRecurringCheque;
}
translist.add(tran);
}
}
}
}
global void finish(Database.BatchableContext BC){
}
}
am trying to write a batch class for my project
when i will create a pledge for donation then based on the duration our transactions are created by trigger but those transactions are not more then 12 but, if my duration is 24 months then .it will create 12 transaction by trigger after that when my list size of transactions is less then 12 every month then it will create a new transaction for that 12 th month and repeat this process for the complete duration .
code:
global class BatchClassPledgeTransaction implements Database.batchable<sObject> {
String query;
list<Transaction__c > transList=new list<Transaction__c >();
// Start Method
global Database.querylocator start(Database.BatchableContext BC){
Query = 'Select id,name,Duration__c,Start_Date__c,End_Date__c from Pledge__c';
return Database.getQueryLocator(query);
}
// Execute Logic
global void execute(Database.BatchableContext BC, List<Pledge__c> Scope){
Id PledgeRecTypeCard = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('Card').getRecordTypeId();
Id PledgeRecTypeGIRO = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('GIRO').getRecordTypeId();
Id PledgeRecTypeOnline = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('Online').getRecordTypeId();
Id PledgeRecTypeRecurringCash = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('Recurring Cash').getRecordTypeId();
Id PledgeRecTypeRecurringCheque = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('Recurring Cheque ').getRecordTypeId();
Id TransactionRecTypeCard = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('Card').getRecordTypeId();
Id TransactionRecTypeGIRO = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('GIRO').getRecordTypeId();
Id TransactionRecTypeOnline = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('Online').getRecordTypeId();
Id TransactionRecTypeRecurringCash = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('Recurring Cash').getRecordTypeId();
Id TransactionRecTypeRecurringCheque = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('Recurring Cheque ').getRecordTypeId();
for(Pledge__c plg:Scope){
Integer i=0;
Integer loopiterater=0;
Integer j=integer.valueof(plg.Duration__c)-12;
Integer monthDiff = plg.Start_Date__c.monthsBetween(plg.End_Date__c);
loopiterater=monthDiff;
if(integer.valueof(plg.Duration__c)>12||loopiterater>12){
for(i=0;i<=j;i++){
Transaction__c tran= new Transaction__c ();
tran.Transaction_Date__c=plg.Start_Date__c.addMonths(i);
tran.Pledge__c=plg.id;
tran.Amount_Reconciled__c=plg.Amount__c;
tran.Donor__c=plg.Account__c;
tran.Status__c=plg.Status__c;
if(plg.recordtypeid==PledgeRecTypeCard){
tran.recordtypeid=TransactionRecTypeCard;
}
if(plg.recordtypeid==PledgeRecTypeGIRO){
tran.recordtypeid=TransactionRecTypeGIRO;
}
if(plg.recordtypeid==PledgeRecTypeOnline){
tran.recordtypeid=TransactionRecTypeOnline;
}
if(plg.recordtypeid==PledgeRecTypeRecurringCash){
tran.recordtypeid=TransactionRecTypeRecurringCash;
}
if(plg.recordtypeid==PledgeRecTypeRecurringCheque){
tran.recordtypeid=TransactionRecTypeRecurringCheque;
}
translist.add(tran);
}
}
}
}
global void finish(Database.BatchableContext BC){
}
}
Please feel free to reply with questions if you have any further concerns.
as for the schedule class
Reffer to the documentation (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm)if you have any questions regarding the schedule class.
Hope this helps!
All Answers
I cleaned up your code a bit before I start to work on it but I dont think I understand just what it is you want the code to do. From my understanding, you want to create transactions based on the following criteria on the duration of the pledge:
1. if the duration is 12 months then create 12 transactions via some trigger
2. if the duration is 24 months then create the first 12 transactions monthly by the 12-th month and reevaluate the criteria.
Please clarify. This is what i can gather so far.
let me clear the logic , if somebody do a pledge for donation and the duration of pledge is16 months then my trigger will create only 12 transactions(like start date is 1 sep 2016 and end date is 1 dec 2017 ) then my trigger will create the records of transaction till 1-august-2017 ,trigger will not create all 16 records, on 1st sep 2016 when my 1st transaction is done then my 11 transections are left then my batch class will create a record for 1 sep 2017 same process when second transection is done on 1 oct 2016 then batch class will create a record for 1 oct 2017 till complete 16 transactions are not done.
let say duration is 24 and your trigger created transaction form 1 Sept 2016 to 1 Aug 2017.
now when your batch class will create a new record for 1Sept 2017 on 1Aug 2016.
Please feel free to reply with questions if you have any further concerns.
as for the schedule class
Reffer to the documentation (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm)if you have any questions regarding the schedule class.
Hope this helps!