2
  • Whenever a case is updated by an agent, a batch job posts the new values to external system. With the new flex queue, we can have upto 100 jobs in holding and 5 in processing/ preparing. We have tested batch job and it is efficiently running even for large quantities of data loaded in a single instance.

  • I got chaining in batch class so once the batch class is instantiated, it can handle even large amounts of data. But my concern is: My organization has around 1500 case representatives. I am trying to handle the following scenario: In any situation lets say all these agents update their cases individually which means 1500 batch jobs, right ?

  • I went through this link tried to add schedulable interface to my class and wondering if this will help me. Reason why I am not sure if this works or not is because I can load 100000 cases from data loader which splits into batches of 200 i.e., 50 batches and as I said my batch job is able to handle any number of cases loaded in single instance. But to test the above scenario, I need some ~500 agents updating their respective assigned case at almost the same time.

Code:

    global class postClass /*implements Schedulable*/ {
        public void caseUpdate(list<case> c){
            //list of cases matching specific criteria
        }

        public string buildCaseString(case c){
            //builds a json case string for case c
        }

        public static void postCases(list<case> cList){
            List<string> postList = new List<String>();
            for(case c : cList){
                String postBody = buildCaseString(c);
                postList.add(c);
            }
            if([SELECT count() FROM AsyncApexJob WHERE JobType = 'BatchApex' AND Status = 'Holding')] < 100){
                Database.executeBatch(new caseBatch(postList));
            } else {
//not sure if below code works
                postClass sc = new postClass();
                Datetime dt = Datetime.now() + (0.024305); // i.e. 30 mins
                String sch = dt.format('s m H d M \'?\' yyyy');
                String jobID = system.schedule('Another Job', sch, sc);
            }
        }

    }

Any other ideas, please let me know.

Thanks.

2 Answers 2

3

There's a webinar from DF13 with Dan Appleman titled Design Patterns for Asynchronous Apex that you'd likely benefit from. The sample code from it is available at http://advancedapex.com/dreamforce13/. He discusses chaining patterns using a Schedulable Class along with a Schedulable Handler and repeating Batchable class.

I should add that he also takes the same material even further in the 3rd edition of his Advanced Apex Programming book that was released earlier this month. The book's implementation includes the use of Queueable Classes along with Triggers. The source code for that is available from http://advancedapex.com/samplecode/.

The material and code for either of these is more than I feel I can cover in a response to your question here. Dan does a good job of covering it in the resources I've pointed you to.

0
2

You may want to consider having your trigger on the Case object write the data to another table to be queued for transfer to the external system. Then write your batch job to read and process the data that has been staged for transfer. This will separate the creation of information from the transfer to the external system. It will then not matter if 500 or 1500 agents are updating cases and you only need one recurring batch job to read the data and transfer. Your unit tests will also be easier to write.

3
  • This is one of the idea i thought of.. But trying to find if there is something that can be done from code
    – sf_user
    Commented Sep 19, 2015 at 21:14
  • Another table.. so you mean creating another object, right ?
    – sf_user
    Commented Sep 19, 2015 at 21:30
  • 1
    Yes, for example StageCaseDataToSystemXYZ. You can either delete the records after you have sent or use a checkbox column DataSent__c and mark the records as processed. I prefer to mark the records as processed so I can monitor operations and use the information to debug any issues with trigger logic. Commented Sep 19, 2015 at 21:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .