[SalesForce] VF Action to Redirect to a Standard Page with Custom Error Message

I have a VF page that produces an Excel file based upon certain products underneath an Opportunity. How would I go about using a VF action to redirect back to the Opportunity standard screen if no matching products were found and display a custom error message?

I can redirect back but I can't figure out how to display an error, for example like using ApexPages.addMessage

Here's my action/redirect code from my extension so far: validOLIs is a boolean which returns true if the product query returns 0.

//Action to highlight no OLIs were returned
public pageReference noValidOLIS(){
    if(validOLIs){
        return redirectError();
    }
    return null;
}

public PageReference redirectError(){
    PageReference errorRef = new PageReference('/'+theOpp);
    ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.FATAL,'No Products Found'));
    return errorRef;

}

Edit:
I've tried using addmessage and directing back to the standard page. I'm open to using java script if it can be used to intercept, but I'm not a js expert so would need some guidance.

Best Answer

On the Apex controller of the page where you are redirecting add the following logic in the constructor:

On redirect page:

public PageReference redirectError(){
    PageReference errorRef = new PageReference('/'+theOpp+'?showError=true');
    ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.FATAL,'No Products Found'));
    return errorRef;
}

Page where you redirect, add in constructor:

String showError = ApexPages.currentPage().getParameters().get('show_error');
if(showError=='true')
{
   ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.FATAL,'No Products Found'));
}

If you are redirecting to standard page then you won't be able to set error message.