function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jerun JoseJerun Jose 

Writing error messages to custom object

Hi,

 

I am using a trigger to show error messages. The requirement is to write the error event into an error object whenever the error occurs.

 

The problem I am having is that whenever I throw an error message, that transaction is treated as an exception and any writes to the DB in that transaction are rolled back.

 

So, the debug logs show one record is inserted, but I guess that is then rolled back. Any ideas on how we can achieve this ?

 

Thanks,

 

Jerun Jose

 

 

sfcksfck

Not sure if I understand you correctly, but it sees like maybe you need to create a custom field on the object called "Error" and then instead of 

someObject.addError('foo');

 you would do

someObject.error__c = 'foo';

 ?

Jerun JoseJerun Jose

Thanks for the response.

 

We are working on an integration project. The error generated needs to be returned to the invoking webservice. So I will have to use the addError method.

 

Thanks,

 

Jerun Jose

Starz26Starz26

There was a similar question to this several days ago....

 

Basically anything you do via DML will get rolled back when you add the error....

 

I would suggest creating an @future method to write the error to an object. That way you can still throw the error and write it to another object via async @future.

Jerun JoseJerun Jose

Thanks again for the response.

 

I actually tried the future call route, but I am not sure if the future method got invoked at all.

 

The debug log shows:

Number of future calls : 1 of 10

 

But the apex jobs page does not show anything. Also, the DML in the future method did not seem to have happened as I could not find any new records.

 

If a transaction hits an exception, will the future callouts made also be rolled back?

Starz26Starz26

Not sure, I was curious as to that myself....

 

 

Damien_Damien_

If a transaction hits an exception, will the future callouts made also be rolled back?

I would imagine no because once it hits the @future method it becomes asynchronous.  But I can't be sure... until I test that.