0
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.

2

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;
    }
}
  • Beginner in Batch apex. I wanna insert data in this batch. according to your start method what will it do ? please clarify bit confused. – Innodel Salesforce Dec 17 '16 at 6:35
  • @InnodelSalesforce It just calls a single execution of the execute method. If you're not sure how it works, I suggest experimenting to learn how it works. Generally speaking, though, it's overkill compared to a simple future method or Queueable, as I explained in my answer. – sfdcfox Dec 17 '16 at 7:19
0

Don't use batch for this. Use schedule apex.

public class Event_Scheduler implements Schedulable
{   
    public void execute(SchedulableContext  schedulableContextVar)
    {   
        EventHandlerClass.insertEventRecords('Pass ID of parent object');
    }
}

Schedule the above class.

public class EventHandlerClass()
{
    public static void insertEventRecords(Id ParentObjectID)
    {
        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, LookupField = ParentObjectID ); 

  // <<You should add the parent object ID here in the MBT_SFFB__LHEvent__c  object  . I think its either look up or master detail
            ml.add(m);
        }
        insert ml;
    }
}
0
global list<MBT_SFFB__Facebook_Page__c> start(Database.BatchableContext BC)
{
    MBT_SFFB__Facebook_Page__c objrecfbpage = new MBT_SFFB__Facebook_Page__c(MBT_SFFB__Facebook_ID__c='a',MBT_SFFB__Facebook_Page_ID__c='sd',MBT_SFFB__Facebook_Page_Name__c='asd',MBT_SFFB__Image__c='sd',MBT_SFFB__Facebook_User_Relates_To_Page__c='a0H2800000B6nR0');
    list<MBT_SFFB__Facebook_Page__c> objmtfbpag = new list<MBT_SFFB__Facebook_Page__c>();
     objmtfbpag.add(objrecfbpage);
   return objmtfbpag;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.