+ Start a Discussion
sumit dsumit d 

develop a batch which will create investment projects for all accounts having minimum one opportunity

Need to develop a batch which will create investment projects for all accounts having minimum one opportunity

NOTE : If already project available on account then we don't have to create new, if it not exist then create new project and run batch  with batch size 5. 


Thanks
amit
                 
HARSHIL U PARIKHHARSHIL U PARIKH
Hi Amit, this is surely possible via Batch Apex. Hope you find below batch helpfull. I donot have same needed objects in my org so expact some different names but however, overall idea is there.
 
Public class InvestmentProjCreation_Batch Implements Database.Batchable<SObject>{
	
    
    //1. Start
    Public Database.QueryLocator Start(Database.BatchableContext dBC){
        return database.getQueryLocator('Select Id FROM Account WHERE Total_Investment_Records__c = 0');
    }
    
    //2. Execute
    Public void Execute(Database.BatchableContext dBC, List<Account> scope){
        
      	List<Account> actsWithOpps = [Select Id, (Select Id FROM Opportunities LIMIT 1) FROM Account WHERE Id =: scope];
       
        List<Investment_Project__c> investProjListToInsert = New List<Investment_Project__c>();
        For(Account EveryAccount : actsWithOpps)
        {
            If(EveryAccount.Opportunities.size() > 0)
            {
                Investment_Project__c investmentProj = New Investment_Project__c();
                
                investmentProj.Custom_Field_1__c = 'Some Value';
                investmentProj.Custom_Field_2__c = 'Some Value';
                investmentProj.Custom_Field_3__c = 'Some Value';
                investProjListToInsert.add(investmentProj);
            }
        }
        
        If(!investProjListToInsert.isEmpty()) insert investProjListToInsert;
        
        
    }
    
    //3. Finish
    Public void Finish(Database.BatchableContext dBC){
        // You can implement send an email method here if you like.
    }
}
If this solves your question then kindly mark is solved!