1

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.

1

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.

|improve this answer|||||
  • Sorry I should have made it clear in my question that I had tried addmessage. I'm open to using java script if that is an option. – Girbot Feb 13 '15 at 17:55
  • @Girbot you cannot show a error message on standard page. You can only show error on custom vf page. You can ake user 4-5 seconds on you vf page unit it shows a message then redirect the user to standard page. – Ashwani Feb 13 '15 at 18:16
1

A good way to show error messages on Standard pages is to use the soft alert technique as outlined here

basically, you are setting values in custom fields on the object and using a formula field on that custom object to display (with image flags), the error message. With clever use of formulas, you can even make the message disappear after a few minutes/hours - this requires timestamping a custom field with the error datetime

|improve this answer|||||
0

This is the approach I took, feel free to shout me down if anything I've done here is bad practice! Seems to be working fine for me so far.

I created an execute javascript button (Opportunity detail page button) using the below:

var query = "SELECT Id, test__c FROM OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}' AND test__c = True"; 
var oli = sforce.connection.query(query).getArray("records"); 
if(oli=="") 
{ 
alert('No Items Found'); 
} 
else 
{ 
//redirect to VF page 
window.location = "/apex/VfPage?id={!Opportunity.Id}"; 
}
|improve this answer|||||

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.