I have an approval process on Case object. Once an approver clicks on Approve button, I have configured a field update approval action, which triggers a Case trigger. This trigger carries out some validations at the backend and throws a validation error on the case object which is under update:

caseObject.addError('Please attach the required files at this stage');

This prevents approval of the case record by throwing an error which looks on the UI as follows:

enter image description here

Now I want some enhancement. I want to make this error look like a Salesforce Validation error. Is there a way I can attach this error to the approval process within apex trigger? I don't want the error to look like the system error as shown in the above screenshot and i perfectly know the reason why it is looking so.

Any help would be appreciated!


You're really asking about performing UI logic based on a trigger impacting classes that are related to the Approval Namespace and the System Approval Class. Unlike visualforce controllers, triggers don't have an easy means of adding page messages to the UI without the use of other classes/handlers. I'm assuming your trigger failure causes a roll back of the approval because its part of the same transaction context.

To do more than what's currently happening, it would seem that you'd want to query to get the Approval Process Id to relate the failure to the correct approval action (and process). I'd expect you'd also need to use something like Platform Events in conjunction with the UI API to create a nicer looking error message. To me, that seems like quite a lot of effort that's not exactly trivial.

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.