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
Eric Anderson 54Eric Anderson 54 

command button pass param to apex code

I have only been working in Visualforce for a week, so forgive me if this is a stupid question.

I have a 'Custom Object' called 'Request__c'. I also have a custom object called 'Time_Entry__c' that is managed through a Visualforce page and is functionally a child of 'Request__c' and is displayed on the 'Request__c' form in Salesforce. I have some code where I am trying to pass the object id of the 'Request__c' object to the Apex method so that its value is included when I create a new instance (row) of the 'Time_Entry__c' object. However, the code that I have developed is not working. Please see the relevent code below. I would appreciate any help that I can get. Thank you so much.

VISUALFORCE code

<apex:page standardController="Request__c" extensions="TrController">
    <!--Because we will be defining 'Input' fields, we must wrap our code in a 'Form' block. -->
    <apex:form >
        <apex:pageBlock title="CDCR - Salesforce Time Reporting for Requests">
            <apex:pageBlockButtons >
                <!-- The following Button is defined in a more complicated fashion so that a parameter can be passed. -->
                <apex:commandButton >
                    <a href="javascript: CurrRequest('{!Request__c.Id}');" >New</a>               
                </apex:commandButton>
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>

...

       <apex:actionFunction action="{!add}" name="CurrRequest" reRender="">
            <apex:param name="EntryID" value="" assignTo="{!ReqID}"/>
        </apex:actionFunction>

    </apex:form>
</apex:page>

APEX Controller

public with sharing class TrController
{
    public List<Time_Entry__c> TimeEntries {get;set;}
    public string reqRslt {get;set;}
    public string showMessage {get;set;}
    //Used to get a hold of the entry record that is selected for deletion.
    public string SelectedEntryID {get;set;}
    public string ReqID {get;set;}
    public TrController(ApexPages.StandardController controller)
    {
        LoadData();
     }

...

    private void LoadData()
    {
        //Load the related time entry records from the database.
        //TimeEntries = [select id, Activity__c, Date_Worked__c, Hours_Worked__c, Minutes_Worked__c, Work_Description__c from Time_Entry__c WHERE name=:profileName order by ID DESC];

        TimeEntries = [select id, Activity__c, Date_Worked__c, Hours_Worked__c, Minutes_Worked__c, Work_Description__c, Related_Object__c from Time_Entry__c order by ID DESC];
    }
...

    public void add()
    {  
        //The following line will obetain the request id (ReqId) that is passed from the VisualForce page
        string RequestID = ReqId;
        //Build the default values to the new time entry record.
        Time_Entry__c entry = new Time_Entry__c(Activity__c='Research', Date_Worked__c=System.today(), Hours_Worked__c=' 0', Minutes_Worked__c='00', Related_Object__c=RequestID);
        //Insert the new default time entry row into the Salesforce database.
        Insert entry;
        //Call the method to reload the Visualforce list.
        LoadData();
    }


   
Best Answer chosen by Eric Anderson 54
Suraj SinghSuraj Singh
<apex:actionFunction action="{!add}" name="CurrRequest" reRender="">
            <apex:param name="EntryID" value="" assignTo="{!ReqID}"/>
        </apex:actionFunction>

reRender="" , there must be a value(either id of `Form` or any other section that you want to render) in rerender part. there after it will send params to controller.

All Answers

Suraj SinghSuraj Singh
<apex:actionFunction action="{!add}" name="CurrRequest" reRender="">
            <apex:param name="EntryID" value="" assignTo="{!ReqID}"/>
        </apex:actionFunction>

reRender="" , there must be a value(either id of `Form` or any other section that you want to render) in rerender part. there after it will send params to controller.
This was selected as the best answer
Eric Anderson 54Eric Anderson 54
Hey Suraj,

Thanks for the help. I didn't understand how the rerender worked, it's syntax, or what it was referencing. After going back through some of the Trailhead tutorials again, I figured it out.

Thanks!
TheDeckbladTheDeckblad
The rerender attribute is what I needed, too. I wasn't seeing any variable setters running until I added this tag to my actionFunction