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
Vikas MenonVikas Menon 

Apex Batch to insert record in custom object

Hi All,
Require urgent help. I have custom object of dispatch orders . I want to create a batch class which makes a DO based on the Opportunitie's which are have StageName="Closed Won". Please share a general format along with the cron string value which can initiate record creation every 15 mins.
i have a written a code but the record has failed. 

thanks
 
Vishal Negandhi 16Vishal Negandhi 16
Can you share your code? We can help you correct it.
Vikas MenonVikas Menon
batch class :-
global class createdoforoppsclosedwon implements Database.Batchable<Opportunity>
{
       global Iterable<Opportunity> start(Database.batchableContext info) 
       {
           System.debug('Start method');
           return new callOppsIterable();
       }

       global void execute(Database.BatchableContext bc, List<Opportunity> scope)
       {

           list<Dispatch_Order__c> dispatchestoadd = new list<Dispatch_Order__c>();
           Dispatch_Order__c dispatch;
           if(scope.size()>0)
           {
               dispatch.Quantity__c=1;
               for(integer i=0;i<scope.size();i++)
               {
                   
                   dispatch.To_Student__c=scope[i].Account.Id;
                   
                   if(scope[i].nm_SelfLearningMaterial__c=='Send to my shipping address')
                   {
                       dispatch.From_Center__c=scope[i].nm_InformationCenters__r.nm_LearningCenter__c;
                       
                   }
                   
                   else if(scope[i].nm_SelfLearningMaterial__c=='Send to my Information Centre. I will pick up')
                   {
                   
                       dispatch.From_Center__c=scope[i].nm_InformationCenters__r.nm_LearningCenter__c;
                       dispatch.To_Center__c=scope[i].nm_InformationCenters__c;
                   }
                   
                
               }
               
             dispatchestoadd.add(dispatch);
             insert dispatchestoadd;
           
           }

       }

       global void finish(Database.BatchableContext bc)
       {
          system.debug('SUCCESS');
       }

}
<-------------------------------------------------------------------------------------------------------------------------->

callOppsIterable method :-

global class callOppsIterable implements iterable<Opportunity> 
{
   global Iterator<Opportunity> Iterator()
   {
      return new OppsIterable();
   }
}
<-------------------------------------------------------------------------------------------------------------------------->
OppsIterable :-



global class OppsIterable implements Iterator<Opportunity>

       List<Opportunity> opps {get; set;} 
       Integer i {get; set;} 
       public OppsIterable()
       { 
           opps = [Select Account.Id,nm_InformationCenters__r.nm_LearningCenter__c,nm_SelfLearningMaterial__c,nm_InformationCenters__c From Opportunity where StageName = 'Closed Won' and DO_for_Student__c=false]; 
           i = 0; 
       } 
         
       global boolean hasNext()
       { 
           if(i >= opps.size()) 
           {
               return false; 
           }   
           else 
           {
               return true;
           }    
       }  
         
       global Opportunity next()
       { 
           if(i>opps.size()) 
           {
           return null;
           }
          i++;
          return opps[i-1];
       } 
}