1

I've got this problem to solve.

I've an apex batch that updates some records all together when a condition is verified. This triggers a Process builder that calls an Apex Class or whatever it might help me to reach my goal.

I've multiple records in input and for each of them I need to do some stuff (queries, inserts, updates, etc..etc..) so let's say DML operation, which way can u suggest in order to process those records arrived in a List once a time?

for example:

AList is a list of 2 records, for each record I've to do stuff ( based on values of this records) but the two executions must be different one from the other in order to handle a rollback in case something goes wrong.

A detailed Example:

for each record in input I've to get ServiceContract and a Contract related to a value passed in input, after that I should check if an Account exists associated to the Contract, the type of the Account to check is based on values in the current processed record.

Thanks in advance.

Kind Regards.

2 Answers 2

1

If you want to have each record handled in its own context, the simplest way to do that would be to simply set the scope size of your batch class to 1. That way, it will iterate through the records one at a time and execute (and possibly rollback, if needed) your changes. To do that, simply add an extra parameter when you invoke the batch job:

Database.executeBatch(new yourBatchClass(), 1);
0

If you are looking to enable partial successes, you can use Database.update methods and specify the allOrNone optional parameter to false:

Database.update(someRecords, /*allOrNone*/ false);

The above should work pretty well as long as you don't actually use Database.rollback as part of your error handling scheme. Such error handling would look something like:

Savepoint initialState = Database.setSavepoint();
try
{
    update someRecords;
    update relatedRecords;
}
catch (DmlException dmx)
{
    Database.rollback(initialState);
}

If you do indeed have such error handling, then you will need to perform your updates using the Database.Batchable interface as @RenegadeCoder suggests and set the batch size to 1.

You must log in to answer this question.

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