Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a trigger on Case that adds an error onto the trigger.new records for certain conditions. This works exactly as expected.

When introducing a visualforce page that queries for existing cases linked to another custom object and automatically closes them down I get an issue. Basically, I have a try / catch around my DML that processes the trigger errors if any occur and adds more detail (specifically the case number), however the page shows my custom error AND the original trigger error. How do I stop the trigger error from showing so that I only get my custom errors?

share|improve this question
add comment

4 Answers

The only solution to your dilemma that I can think of is to make the logic in the trigger smarter and able to know some context of the calling page because you cannot later clear or alter the Messages collection, nor modify entries once they have been added by the trigger.

One manner of altering the trigger's behavior would be to use the request URL to detect whether or not you're on a known VF page that should be handled differently.

trigger OppyControlledMessageText on Opportunity (after update) {

    // the pRef reference will be null if the calling code is a native page
    // this could also be utilized as a method to detect the caller context
    // but is not really necessary in this example
    // PageReference pRef = ApexPages.currentPage();

    URL currentURL = URL.getCurrentRequestUrl();
    URL comparisonURL = new URL(URL.getSalesforceBaseUrl().toExternalForm() + Page.YourVFPageName.getUrl());

    boolean knownPath = currentURL.getPath() == comparisonURL.getPath();

    for (Opportunity oppy : trigger.new) {
            if (knownPath) {
                oppy.addError('Error text with a case number');
            } else {
                oppy.addError('Error text without a case number');
            }
    }   
}
share|improve this answer
    
This is very interesting. I think if I was to implement this approach I would replace the URL checking with a simple static boolean so that I would be able to apply this to future visualforce pages if I need to. –  Ben Jan 7 '13 at 12:46
    
Expanding on your 'simple static boolean' approach... I would personally do this using a custom setting instead. The custom setting would contain at a minimum the name(s) of the VF pages. This would allow you to use values which are defined outside the trigger to control its behavior. (I did not want to complicate the functional example with custom settings logic.) –  Mark Pond Jan 7 '13 at 16:00
    
Did you find an acceptable solution to this? –  Mark Pond Jan 8 '13 at 7:42
add comment

If you really only ever need to show your own custom errors created in your controller and not any Salesforce validation errors or your trigger errors you could use the apex:pageMessage component and remove the apex:pageMessages from your page.

For example:

public class YourController {
    public Account account { get; set; }

    // store the message you build dynamically in here
    // make it transient so it isn't part of the view state and
    // "resets" between requests.
    public transient String errorMessage { get; set; }

    public PageReference saveItWithErrors() {
        try {
            upsert a; // causes the trigger error message
        } catch (Exception e) {
            // dynamically build / add to your custom error message
            // possibly get information from the ApexPages.Messages from Exception e.
            // or just set it to some static text.  the sky's the limit...
        }
        // somehow detect your custom errors
        if (hasErrors()) {
            errorMessage = 'Error message from Controller';
        }
        return null;
    }
}

And in the Visualforce:

<apex:pageMessage severity="error" 
    strength="2" 
    summary="Error:"
    detail="{!errorMessage}" 
    rendered="{!errorMessage != null}"/> 
share|improve this answer
add comment

Add the <apex:pageMessages/> markup to your visual force page.

Handle the exception in your vf controller and within the try catch block, you need to construct a new pageMessage, and add it to the collection

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'error message'));
share|improve this answer
    
I have already wrapped my DML around a try / catch block as mentioned in my question. The issue I am experiencing is that the pageMessages tag is showing my custom error messages (added using ApexPages.addMessage) AND also the original error from the trigger. I want to only show my custom error, NOT the original one. –  Ben Jan 4 '13 at 14:30
add comment

I thought I'd post up my actual solution to this just for completeness. Basically, I stored my error messages from the try / catch into a string list, then used an apex:repeat on my page to show them to the user (with some css styling added to make it look like the pageMessages component). It's not a solution I prefer as it removes the standard salesforce error handling in favour of my own but it does the job.

share|improve this answer
    
You might want to consider looking at the apex:pageMessage component (as I mentioned in my answer), which would take care of the styling for you. Note that it not the same as the apex:pageMessages (plural). –  Peter Knolle Jan 7 '13 at 13:02
    
Thanks, Peter. I initially dismissed it as I won't know how many errors I will have as I will be updating multiple case records at the same time so there is a potential of more than one error. I was able to wrap the apex:pageMessage around an apex:repeat though, which looked ok. –  Ben Jan 7 '13 at 13:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.