0

I have a list of contacts which get inserted/Updated in salesforce database. After this those contacts assosiated with new case/existing case. If case fails to update or insert then rollback the changes made for contact. All this processing is in bulk.

  • if the loading of the cases is not in the same transaction then no. If you upset the case in a trigger on the contact as a result of the insert then yes. You could also delete the contacts based on the result file if you can match them up – Eric Jun 29 '17 at 1:37
  • Thanks for your response. I am doing all this in a single transaction. I have a csv file which has multiple rows with case and contact details where Case and Contact can be either inserted or updated. First while processing the file, I insert or update the contact details and then pass the map to the case processing function. If any case gets failed to be inserted or updated then I wan't to rollback the changes for Contact as well(To fail the complete row in csv). I am unable to use the savepoint because of bulk operation. I can delete the new contact being created but not for updated contact. – Pawan Tiwari Jun 29 '17 at 1:38
  • use database.saveresult, pass results back to contact, use add error on contact to error the row – Eric Jun 29 '17 at 2:34
0

Order of Execution can be:

Create a savepoint first and then Save contact record. Then call the function to save case record . if there is an error while saving case record throw an exception. This will take you back to the function that saved the contact there you can rollback. Thus in effect cancelling the complete transaction.

Eg:

// At contact level in code

Savepoint sp = Database.setSavepoint();

.... do something ...

try{

// perform an database operation update or insert or Upsert

update RecordsToUpdate

..... then call

UpdateCaseFunction(bulkifiedListofContact , otherParameters)

} Catch(Exception ex){

Database.rollback(sp);

}

// UpdateCaseFunction can have its own try catch statement and throw exception on database failure

Have you tried the database version for update or insert , it has an option to not throw an error if the operation fails . This gives more granular control on what failed and what succeeded at row level.

Database.SaveResult[] srList = Database.update(recordsToUpdate, allOrNone)

allOrNone : If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. An example of how to use SaveResult can be found here :

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_saveresult.htm#apex_methods_system_database_saveresult

Hope this helps.

|improve this answer|||||
  • I will insert/update the case after the all contacts processing either failed or success. I want to perform rollback on map or list for case failed records. This need to be done only in bulk to avoid DML count limit as Savepoint and Rollback counts against the governor limit for DML statements. – Pawan Tiwari Jun 29 '17 at 3:05

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.