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
Scott0987Scott0987 

Command button pass ID to function

I have a datatable on a visualforce page that lists tickets.  I have one column on the datatable that has a button that will redirect to a page with details about the ticket.  

Here is the portion of the visualforce page that affects the current issue:

<apex:form >
<apex:dataTable value="{!openTickets}" var="OT" width="80%" rowClasses="odd,even" cellspacing="1px" cellpadding="5px">
<apex:column value="{!OT.id}"><apex:facet name="header">Ticket ID</apex:facet></apex:column>
<apex:column ><apex:commandButton value="Ticket Details" action="{!redirectTicketDetails}"/></apex:column>
</apex:dataTable>
</apex:form>

 Here is the apex extension:

    public PageReference redirectTicketDetails(){
        PageReference pageRef = new PageReference('/apex/ticketDetails');
        return pageRef;
    }

 The button works correctly.  What I would like to be able to do though is have the page it redirects to be /apex/ticketDetails?id=******* but replace the ****** with the id from the ticket where they clicked on the button.  I am pretty new to apex and am learning as I go.  How would I go about doing this?  Any help would be appreciated. 

Best Answer chosen by Admin (Salesforce Developers) 
Scott0987Scott0987

I found my own answer.  For anyone that needs it here is what I did.  I changed the command button from 

<apex:commandButton value="Ticket Details" action="{!redirectTicketDetails}"/>

To

<apex:commandButton value="Ticket Details" onclick="window.top.location='/apex/TicketDetails?id={!OT.id}';"/>

 Then I was just able to get rid of the method entirely.  

All Answers

Scott0987Scott0987

I found my own answer.  For anyone that needs it here is what I did.  I changed the command button from 

<apex:commandButton value="Ticket Details" action="{!redirectTicketDetails}"/>

To

<apex:commandButton value="Ticket Details" onclick="window.top.location='/apex/TicketDetails?id={!OT.id}';"/>

 Then I was just able to get rid of the method entirely.  

This was selected as the best answer
OviOvi

you should have a look at the apex:param

 

Something like this

<!-- page -->
<apex:form>
<apex:commandButton action="{!redirect}" value="Ticket details">
<apex:param name="idParam" value="{!OT.id}"/>
</apex:commandButton>
</apex:form>


// Controller
public PageReference redirect() {
    string idParam = apexpages.currentpage().getparameters().get('id');
    return new PageReference('/apex/DetailPage?id=' + idParam);
}