0

I am writing a trigger in which I want to throw a custom exception to display only the exception message instead of all the details. I cannot use addError() method because its not on trigger context variable, I am passing ids of records to the trigger handler method and fetching them again to use them, this is required for me as I am using same handler for 4 different SObjects.

The output that I am getting is as below

In the above exception thrown on the page, I just want to show error message as 'Other Sales Rep is not found for bundled Opportunity with Id 00kn0000003lKllAAE' instead of all other details.

Exception class,

public class TriggerException  extends Exception {}

Code used to throw exception from trigger handler.

throw new TriggerException (
                    ' Other Sales Rep is not found for bundled Opportunity with Id ' + currentOppLineItem.Id);

is there any way to achieve this?

Any type of help is much appreciated.

1

Try the Id.addError method, which will work as long as you know the Id of the Opportunity that should show this error. I'm assuming you can pull it from currentOppLineItem:

Id parentId = currentOppLineItem.OpportunityId;
parentId.addError('<Message goes here>');

Your idea that you cannot call addError to map back to the source records is not correct. Even with SObject.addError you could do it, as long as you saved the trigger context records in your state somewhere.

If you are not willing or able to figure out addError, you need to catch the error you've thrown and add it to the page.

try
{
    // operation that throws
}
catch (TriggerException tex)
{
    ApexPages.addMessages(tex);
}
  • Thanks @Adrian but I am not using trigger context variable as records are fetched again using ID in generic handler for 4(may be 5 in future) different SObjects. – Ishwar Mete Jul 19 '16 at 13:26
  • Can I use Custom generated exception to show only error message? – Ishwar Mete Jul 19 '16 at 13:36
0

The flaw in your design is that you are re-fetching the records again. You should be passing the Trigger context into your handler as a generic SObject list or, better yet, just call reference Trigger.new directly within your handler. I'm not sure what you're doing within your handler, but if it's being used for 4 different SObject types, then I'm assuming it's generic enough that you can use the SObject.get and SObject.put methods to get and assign values to the SObject. You can call .addError on the generic SObject class, which should solve your problem. Here's some code (not sure it will compile as I'm just writing it here, but you should get the idea):

trigger MyTrigger on MyCustomObjectOne__c (before insert, before update, blah blah) {
    (new MyGenericTriggerHandler()).doAction();
}

trigger MyTrigger on MyCustomObjectTwo__c (before insert, before update, blah blah) {
    (new MyGenericTriggerHandler()).doAction();
}

public class MyGenericTriggerHandler {
    public MyGenericTriggerHandler() {}

    public void doAction() {
        // Trigger.new is valid here
        SObject[] sobjects = Trigger.new;
        for (Sobject sobj : sobjects) {
            if (isThereAnError(sobject)) {
                sobject.addError('my error');
            }
        }
    }
}
0

You mentioned that your function receive ids and it's used by 4 objects

You still can pass list of sObjects and in the function get their IDs, this way you will be able to use addError.

Alternative approach is that the function will return map - per each Id it's error message (if found any).

-1

One most obvious way is to create a custom textfield 'ShowError', fill it with the needed error text and update the record. In trigger just check if the field is not empty and if it isn't - erase it and user .addError method. The disadvantage is that you must create additional custom field on every Object.

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.