function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MohandaasMohandaas 

Action Method: Add Apex Message

In my controller I have an action method that returns a new pagereference. Is there a way I can add an apex message on the landing page?

 

I tried something like this which is not supported.

 

public pagereference redirect(){

 

 Apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.ERROR,'Unable to create New Proof of Performance');
 Pagereference detail = new Pagereference('/'+program.Id);
 detail .addMessage(msg); // not supported
  return detail;
 }
Has anyone tried this before?

 

bob_buzzardbob_buzzard

If the controllers of the two pages are not the same, I think the only option that you have is to pass the error message (or an error code etc) as an URL parameter to the target page.  Then in the constructor of the controller backing the target page, pull the information from the URL and add the message via the ApexPages.addMessage() mechanism.

MohandaasMohandaas

Bob,

The controllers are not the same. From a custom controller I am navigating to a SOBJECT standard detail page. I would love to have a message on the detail page.

bob_buzzardbob_buzzard

Ah, that's a different kettle of fish. You may be able to go the route of adding a sidebar component that pulls the error from the URL and puts it into the page via javascript.  

Kirill_YunussovKirill_Yunussov

Is there still not an easy way of doing this using URL parameters which would populate certain fields/parts of the page being edited?  Something like 

 

return new PageReference('/' + objectId + '/e?errormessageontop=' + urlEncodedMessage);

//or reference that error message object by its ID, whatever it is

return new PageReference('/' + objectId + '/e?CF00N30000000wIzw_lkid=' + urlEncodedMessage);

 

bob_buzzardbob_buzzard

Not if you are using a standard detail page I'm afraid - there's no way to make the page pull that information from the URL.  You could do something along those lines with an edit page, but that would just populate an editable field with the message which wouldn't really make the error jump out at the user.