1

I have some code below where i want to return the custom error json message to user who is doing inbound call. Also i want to revert back the whole transaction when custom exception came. How i can do both as when i return custom error message, the transaction gets success which I dont want to do in catch block.

@HTTPPost
global static void createCaseFromAPI(){
    RestRequest reqObj = RestContext.request;
    RestResponse resObj = RestContext.response;

    try{
        FirstCalloutToSomeOtherAPI();
        SecondCalloutToSomeOtherAPI();

        insert contact1;
        insert contact 2;


        if(caseMetadataList.size() == 0){       //validations from data of first callout and second callout
            throw new MyCustomAPIException('Necessary attributes has not been passed to identify supported case category, case type and case subtype !');
        }
        
        
        //Create Case based on data from first callout and second callout and after doing validations
        
        insert caseObj;

        responseMap.put('ResponseStatus','SUCCESS');        //some map is there
        
        resObj.statusCode = 200;
        resObj.addHeader('Content-Type', 'application/json');
        resObj.responseBody = Blob.valueOf(JSON.serialize(responseMap));
        createAPILog(reqObj,resObj,'CaseCustomerServiceAPI.createCase',responseMap);
        
        
    }catch(MyCustomAPIException ex) {
        system.debug('ex.getMessage() > '+ex.getMessage());
        system.debug('ex.getMessage() > '+ex.getStackTraceString());
        
        responseMap.put('ResponseStatus','ERROR'); 
        
        resObj.statusCode = 200;
        resObj.addHeader('Content-Type', 'application/json');
        resObj.responseBody = Blob.valueOf(JSON.serialize(responseMap));
//here i am facing issue
    }
}

1 Answer 1

1

After making your callouts (you can't callout again after a DML operation), set a savepoint. You can undo all changes from the time the savepoint is set and the time it is rolled back.

SavePoint sp;
try{
    FirstCalloutToSomeOtherAPI();
    SecondCalloutToSomeOtherAPI();
    sp = Database.setSavePoint();
    ...
} catch(MyCustomAPIException e) {
  Database.rollback(sp); // All changes are undone
  ...
}

In your specific code, it's also worth noting that you don't need to throw an exception, as a normal branch would work fine:

FirstCalloutToSomeOtherAPI();
SecondCalloutToSomeOtherAPI();
if(caseMetadataList.size() == 0){       //validations from data of first callout and second callout
    responseMap.put('ResponseStatus','ERROR'); 
    resObj.statusCode = 400; // Indicate bad request
    resObj.addHeader('Content-Type', 'application/json');
    resObj.responseBody = Blob.valueOf(JSON.serialize(responseMap));
    return; // exit early
}
insert contact1;
insert contact2;
//Create Case based on data from first callout and second callout and after doing validations
insert caseObj;
responseMap.put('ResponseStatus','SUCCESS');        //some map is there

resObj.statusCode = 200;
resObj.addHeader('Content-Type', 'application/json');
resObj.responseBody = Blob.valueOf(JSON.serialize(responseMap));
createAPILog(reqObj,resObj,'CaseCustomerServiceAPI.createCase',responseMap);

If you need to catch errors on the DML operations, that can be a separate step, and you can use the rollback statement to help keep your data correct.

2
  • Thank @sfdcfox for answering. i tried the savepoint and rollback as well and it works fine. Just one que, i hope rollback, roll everything to the set savepoint (including any future method run or queueable jobs or sending of emails) which may run from trigger on dml of contact object as well. Am I right?
    – Sukruti
    Commented Feb 28, 2023 at 4:30
  • 1
    @Rabb It will roll back anything that is possible to roll back, including future, queueable, schedulable, batchable, emails, and "publish after commit" platform events. It will not roll back "publish immediately" platform events, as they will have already been sent out.
    – sfdcfox
    Commented Feb 28, 2023 at 8:31

You must log in to answer this question.

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