[SalesForce] Trigger validation error message on Submit for Approval (Approval Process)

I have recently encountered an issue, which I do not seem to be able to resolve. I have set up an approval process that fires up if my PickList__c is evaluated true to the following value (or entry criterion): 'New'. When clicking on 'Submit for Approval' the PickList value changes to 'Approved', but prior to that I am running a trigger validation on two mandatory fields on the object (Custom_Field1__c and Custom_Field2__c). Currently, I have some logics on a before insert and before update trigger and I can use add.Error to return the error message on the fields. I would like the error(s) to be thrown on the same page, instead I am redirected to a second screen (titled Choose Approver) that looks like this:

enter image description here

I have a couple of questions:

  • Why is it redirecting me to the Choose Approver page?
  • Is it possible to return the error on the object? Is this possible to return it on the actual fields?
  • Am I correct in including the validation on the before insert and before after trigger?

Ideally, I would like to be able to have the error displayed on the fields, but in second instance I can also consider customising the error on the second screen.

Any suggestion is more than welcome!

Thank you for your help,

Regards,

I.

Best Answer

Yes, you need to add the error logic into the trigger before trying to insert

Something like

boolean b = false;

if(custom_field__c == null){ //test for the error
    custom_field__c.addError('your error message');
    b = true; //set the boolean to true so that update can't happen
}
if(b==false){ //only when there are no errors can you update
    update custom_object__c;
}