[SalesForce] Batch not executing

MBT_SFFB__Listing_Name__c ='xyz' // string
MBT_SFFB__Event_Cancel_Reason__c ='xyz' // string
MBT_SFFB__Event_Cancelled__c='xyz' // string
MBT_SFFB__LHEvent__c // object in which i wanna insert with this above 3 fields.

calling the batch as below…

String query = 'SELECT MBT_SFFB__Listing_Name__c,MBT_SFFB__Event_Cancel_Reason__c,MBT_SFFB__Event_Cancelled__c from MBT_SFFB__LHEvent__c';
insertrecords c = new  insertrecords(query);
Database.executeBatch(c);

want to insert 500 events at a time. object contains 0 records … batch is not inserting records is there any mistake in query ??

if object contains 1 record already in the org than it is working perfectly.

global class insertrecords implements Database.Batchable<sObject> 
{
 global final String query;
 global insertrecords(String q) 
 {
   query = q;
 }   
global Database.QueryLocator start(Database.BatchableContext BC)
{
  return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC,List<sObject> scope)
{      
    List<MBT_SFFB__LHEvent__c> ml = new List<MBT_SFFB__LHEvent__c>();
    for (Integer i=0;i<500;i++) 
    {
        MBT_SFFB__LHEvent__c m = new MBT_SFFB__LHEvent__c(MBT_SFFB__Listing_Name__c='listing ' + i,MBT_SFFB__Event_Cancel_Reason__c='Some reason '+i,MBT_SFFB__Event_Cancelled__c=true);
        ml.add(m);
    }
    insert ml;

}
global void finish(Database.BatchableContext BC)
{

}
}

As i understood the batch apex requires to generate the scope of objects used in the batch job. and start method does IT.
In my case start method will fire query .. and the object contains zero records will provide scope with null . As execute method's syntax is execute(jobId, recordList) recordList will be null. Please Clarify… if i am wrong.

Best Answer

There's no point in using the query, because you just said there's no data. If you want to use Batchable, just use any ordinary list:

public Integer[] start(Database.BatchableContext context) {
    return new Integer[] { 1 };
}

However, that's almost certainly not what you want to do. If you want to monitor your progress, consider using Queueable, otherwise use a simple future method.

// Using a future method
@future public static void insertRecords() {
    List<MBT_SFFB__LHEvent__c> ml = new List<MBT_SFFB__LHEvent__c>();
    for (Integer i=0;i<500;i++) {
        MBT_SFFB__LHEvent__c m = new MBT_SFFB__LHEvent__c(MBT_SFFB__Listing_Name__c='listing ' + i,MBT_SFFB__Event_Cancel_Reason__c='Some reason '+i,MBT_SFFB__Event_Cancelled__c=true);
        ml.add(m);
    }
    insert ml;
}

// Using a Queueable class

public class insertRecords implements Queueable {
    public void execute(QueueableContext context) {
        List<MBT_SFFB__LHEvent__c> ml = new List<MBT_SFFB__LHEvent__c>();
        for (Integer i=0;i<500;i++) {
            MBT_SFFB__LHEvent__c m = new MBT_SFFB__LHEvent__c(MBT_SFFB__Listing_Name__c='listing ' + i,MBT_SFFB__Event_Cancel_Reason__c='Some reason '+i,MBT_SFFB__Event_Cancelled__c=true);
            ml.add(m);
        }
        insert ml;
    }
}
Related Topic