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
JoeZaloom.ax395JoeZaloom.ax395 

Using $Action global variable in custom controller

Hello,

Is there a way to use urlfor($Action.Account.New) from a custom controller? I'm trying to redirect a user to a new Account page after performing an action on the server. I have a command button w/ an "action" attribute pointing to a custom save function in the controller named "newAccount()". After updating other Accounts I'd like to redirect the user to a new Account page, passing in certain parameters.

I can't find a way to get to the $Action global variable in the controller. Is there a way to do this? I thought about passing the $Action.Account.New to a property on the controller, but that seems hacky and kinda messy.
Ron HessRon Hess
you could use the describe info to get the prefix, it's kind of heavyweight to read the "001" prefix for account.
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_sobject_describe_methods.htm

then add /e which gets you a new object page.

return new pagereference('/001/e');

the second issue is with passing values to the new page, you would have to override the new page , using a visualforce page for the new functionality, and read the passed parameters yourself once that page loaded.



JoeZaloom.ax395JoeZaloom.ax395
Thanks Ron. I did try just hardcoding the "/001/e?" and appending the args to the end of that sting. If I pass a "RecordType" arg it bypassed the recordtypeselect.jsp and passes in my other args. I figure I'll give the user a RecordType select and pass in the ID they select. I've seen references to hard-coding the url prefixes for different objects. Is this not recommended? Do the prefixes change frequently?

The other issue I've had is passing a param w/ a commandButton. The docs state that a param will work w/ a commandButton, but I've seen other refreences in the boards saying it doesn't work. I can't seem to get it to work w/ a button, but it does work w/ a link.

Works:
Code:
<apex:pageBlockButtons location="top" >
 <apex:commandLink action="{! newAccount}" value="    New    " rendered="{! createAccounts}" >
 <apex:param name="newurl" assignto="{!newAccountUrl}" value="{! urlfor($Action.Account.New)}" />
 </apex:commandLink>
</apex:pageBlockButtons>

 
Doesn't Work:
Code:
<apex:pageBlockButtons location="top" >
 <apex:commandButton action="{! newAccount}" value="    New    " rendered="{! createAccounts}" >
 <apex:param name="newurl" assignto="{!newAccountUrl}" value="{! urlfor($Action.Account.New)}" />
 </apex:commandButton>
</apex:pageBlockButtons>

 
Thanks for your input. Much appreciated.
js



DetlefRDetlefR

Hi!

Exact same thing here with my apex custom controller: param is set via assignTo with commandLink but not with commandButton. It took my a painful lot of time to figure it out - thanks for this post.

 

I would consider this a bug - following the documentation this has to work either way - but it doesn't!

I tried this today in a spring'11 sandbox.

dr