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
esbium99esbium99 

Dynamic action value in component for buttons and links

I am trying to develop a reusable component that contains some stylized buttons.  I want to include this component on all of the pages for a force.com app.  The problem I am running into is that I want to pass the action for the commandlink/commandbutton tag into the component as an attribute and its not working.  When I click a button that was included via the component with an action that was passed in, it just refreshes the page (via a post which, in the end, is what I want) and doesn't call my action.  I am pretty sure its refreshing because the action method I am passing isn't being interpreted correctly in the component but I am not sure.

 

 

I am wondering if I need to change the type of the attribute in the component but not sure what to change it to...

Here is some code to help explain (lots of things ommitted to keep it simple).

 

Page:

 

<div>Some html

<c:myComponent myAction="myCustomAction"/>

 

Component

 

<apex:component>

<apex:attribute name="myAction" required="true" type="String" description=""/>

     <apex:commandButton action="{!myAction}">My Button</apex:commandButton>

</apex:component>

 

Thanks for any help anyone can provide!

Best Answer chosen by Admin (Salesforce Developers) 
esbium99esbium99

Bob, Thanks for your quick and most helpful reply! Your solution worked with one change.

 

We are now using the component in our pages as follows:

 

 

<c:myComponent myAction="{!ApexPages.Action.myCustomAction}"/>

 

Thanks again!

 

All Answers

bob_buzzardbob_buzzard

You need to change the attribute type to ApexPages.Action, and change your component reference in the page to:

 

<c:myComponent myAction="{!myCustomAction}"/>

 

esbium99esbium99

Bob, Thanks for your quick and most helpful reply! Your solution worked with one change.

 

We are now using the component in our pages as follows:

 

 

<c:myComponent myAction="{!ApexPages.Action.myCustomAction}"/>

 

Thanks again!

 

This was selected as the best answer