I have an @future method that sets up a user - it is in a seperate future method to avoid errors relating to DML on set-up and non-setup objects in the same transaction.

I would like to implement some kind of logging to track if the method fails. However, since the method is using setup objects and is already in a future method, I'm sort of stuck.

Am I stuck using emails for this or is there another solution where I can write to a log object? The issue is I want to keep records of successes/failures without having to resort to emails.

  • 1
    Making a comment since I'm not sure it will work: can you do a callout in the catch block if it fails? You could create a webservice to receive the error message and do the logging. Just not sure a callout would be allowed in that situation. – Daniel Hoechst May 10 '17 at 22:13

Sending emails sounds like a viable solution to me if you have a ticketing system (ServiceNow, JIRA, Trello, etc.) set up. Your code would send an email on failure (and success, if you choose) to the ticketing system endpoint/inbox, which would create a ticket. Depending on the system you have, it could assign the ticket to a queue for you (or even a delegated admin support team), and reporting on successes/failures is easy. You could have record of the failure, who resolved it, when it was resolved, and an easy way to generate reports for auditing.

If you prefer to avoid emails, consider @DanielHoechst's suggestion and set up a web service to receive the error message and handle the logging. And since it is not possible to call a future method from another future method, if you haven't already, you may want to look into Queueable Apex. I'm not experienced enough with asynchronous Apex programming to say for sure, but this may even allow you to keep the logging in Salesforce and avoid external systems altogether.

  • 2
    I will put my bet on Queueable Apex. This will work like charm if you chain 2 queueable jobs one for the DML on user and another on your logging object. No need to write any webservice. Each Queueable apex anyway runs in a separate transaction. – Santanu Halder May 11 '17 at 6:13

Just do the below steps: -

  1. create one log object.
  2. in your future method catch block just call a function with some parameter(those are the exception, line no.,class Name etc.).
  3. in that method create record record in the log object.

  4. you can refer those records for your future reference.

  • But I'm doing DML on set-up objects and you can't do DML on set-up and non-setup objects in the same transaction. Also, I'd like to be able to report on successful transactions. – brezotom Apr 27 '17 at 18:18
  • then you can call the same method from try block after insert the record. – Avijit Das Apr 27 '17 at 19:21
  • You'll have to forcefully perform the DMLs on the Setup Objects in a seperate transaction. One way to handle this(to put them in a different transaction) is to wrap the code(to process Setup objects) in a different Apex Class and in it's own @future annotated method. This would run the code in a different transaction. – Avijit Das Apr 27 '17 at 19:31
  • This is exactly what I've done, as stated in the question. The question is - how do I log the success or failure inside this method? – brezotom Apr 27 '17 at 20:18

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.