2

I am using a custom free app from app exchange to collect feedbacks from contacts about cases. The answers given by the contact are saved in a custom app object, but since the app has some limitations, I am going to transfer the answers data from the app object to a custom object that will be related to the case and the contact.

In order to do this, I was thinking about creating 2 different batches: the first batch will fetch the answers records, take the information I need and move it to my custom object; the second one will delete the records I just moved.

BUT I have some doubts about the batch implementation:

  • do I need to create 2 different batches or can I use just one and do everything there?
  • in order to keep cases updated, I would need to run this job at least every half hour during the day. Is this going to be too much for my org?

Thank you

3

You can do this in a single batch class. Something like below should get you started. You need to obviously change the API names of fields and objects, but you can see the general idea

    public class MyBatchJob implements Database.Batchable<SObject>, Schedulable {

       public final string query;

       public MyBatchJob() {

            query = 'Select Id, Name, Case__c, Contact__c From Your_App_Object__c';

       }    

       public void execute(SchedulableContext sc) {
            Database.executeBatch(this, 100);
       }

       public Database.QueryLocator start(Database.BatchableContext bc) {
            return Database.getQueryLocator(query);
       }

       public void execute(Database.BatchableContext bc, List<sObject> scope) {

           list<Your_New_Custom_Object__c> newObjects = new list<Your_New_Custom_Object__c>();

           for(Sobject s : scope){
               Your_App_Object__c obj = (Your_App_Object__c) s;
               newObjects.add(new Your_New_Custom_Object__c(
                   Case__c = obj.Case__c,
                   Contact__c = obj.Contact__c,
                   Field_1__c = obj.Field_1__c,
                   Field_2__c = obj.Field_2__c,
                   Field_3__c = obj.Field_3__c
               ));
           }
           insert newObjects;
           delete scope;

       }

       public void finish(Database.BatchableContext bc) {
          system.debug('JOB IS FINISHED');
       }

}

Then you would use the following 2 statements in an execute anonymous window. this will schedule your job to run at the top (:00) and bottom (:30) of every hour.

System.schedule('My Batch - Top of Hour', '0 0 * * * ?', new MyBatchJob());
System.schedule('My Batch - Bottom of Hour', '0 30 * * * ?', new MyBatchJob());

And no, running this every half hour shouldn't be all that taxing on your org, assuming you don't have 1 million records being created every half hour :)

This is likely also something that can be done in real time with a trigger. After the object is created, you could create your new object and delete it. The only disadvantage there is you would need to manually create the historical records, where as with a batch class, the first time it runs it will fix all the historical records.

Hope that helps.

  • Yes that really helped! Thanks for clarifying my doubts, I am always worried about performances – tony danza Feb 19 '15 at 10:17

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.