function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhi MalikAbhi Malik 

Please Help to write 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 .
NagendraNagendra (Salesforce Developers) 
Hi Abhi,

Please find the batch class below:
global class BatchClassPledgeTransaction implements Database.batchable<sObject> {
    final String query;
    list<Transaction__c> transList = new list<Transaction__c>();
    
	// constructor
	global BatchClassPledgeTransaction() {
		query = 'SELECT Id, ' +
					   'Name, ' +
					   'Duration__c, ' +
					   'Start_Date__c, ' +
					   'End_Date__c ' +
				'FROM Pledge__c';
	}
	
	// Start Method
    global Database.querylocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }
    
	// Execute Logic
    global void execute(Database.BatchableContext BC, List<Pledge__c> Scope){
		Map<String, Schema.RecordTypeInfo> pledgeRecordTypeInfosByNameMap = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName();
		Map<String, Schema.RecordTypeInfo> transactionRecordTypeInfosByNameMap = Schema.SObjectType.Transaction__c.getRecordTypeInfosByName();
		
		Id PledgeRecTypeCard = pledgeRecordTypeInfosByNameMap.get('Card').getRecordTypeId();
        Id PledgeRecTypeGIRO = pledgeRecordTypeInfosByNameMap.get('GIRO').getRecordTypeId();
        Id PledgeRecTypeOnline = pledgeRecordTypeInfosByNameMap.get('Online').getRecordTypeId();
        Id PledgeRecTypeRecurringCash = pledgeRecordTypeInfosByNameMap.get('Recurring Cash').getRecordTypeId();
        Id PledgeRecTypeRecurringCheque = pledgeRecordTypeInfosByNameMap.get('Recurring Cheque').getRecordTypeId();
        
		Id TransactionRecTypeCard = transactionRecordTypeInfosByNameMap.get('Card').getRecordTypeId();
        Id TransactionRecTypeGIRO = transactionRecordTypeInfosByNameMap.get('GIRO').getRecordTypeId();
        Id TransactionRecTypeOnline = transactionRecordTypeInfosByNameMap.get('Online').getRecordTypeId();
        Id TransactionRecTypeRecurringCash = transactionRecordTypeInfosByNameMap.get('Recurring Cash').getRecordTypeId();
        Id TransactionRecTypeRecurringCheque = transactionRecordTypeInfosByNameMap.get('Recurring Cheque').getRecordTypeId();
		
		Set<Id> pledgeIdSet = new Map<Id,SObject>(Scope).keySet()
		
		// Query once per batch job
		// SOQL limits
		AggregateResult[] numberOfTransactionsPerPledge = [SELECT COUNT(id)
														   FROM Transaction__c
														   WHERE Pledge__c IN :pledgeIdSet
														   GROUP BY Pledge__c];
		
        for (Pledge__c plg : Scope){
			// make sure that we even need to create transactions. 
			// avoid going over
			if (numberOfTransactionsPerPledge.get(plg.Id) < integer.valueof(plg.Duration__c)) {
				Integer j = integer.valueof(plg.Duration__c) - 12;
			
				for (Integer i = 0; i <= j; i++){
					Transaction__c tran = new Transaction__c();
					
					// Transactions start where trigger left off
					tran.Transaction_Date__c = plg.Start_Date__c.addMonths(i + 12); 
					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);
				}
			}
		}
		
		// insert all at once
		// DML limits
		insert transList;
    }
    
	// finish
	global void finish(Database.BatchableContext BC){}
}
Schedule Class:
// Sample call from anonymous apex window:
//
// Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
// example last day of the month at 12am
// String cronStr = '0 0 0 ? * L'; 
// BatchClassPledgeTransactionSchedule sched = BatchClassPledgeTransactionSchedule();
// System.schedule('BatchClassPledgeTransaction ', cronStr, sched);
global class BatchClassPledgeTransactionSchedule implements Schedulable {
	global void execute(SchedulableContext sc) {
        //  Instantiate batch class 
		Id batchInstanceId = Database.executeBatch(new BatchClassPledgeTransaction());
    }
}
I kindly request you to close this thread by marking it as solved if it helps.So that experts in the community can answer the other posts which are not solved.

Best Regards,
Nagendra.P