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 

Call controller and pass parameter on initial VF page load

Hi there everyone,

I've looked through previous entries and couldn't find an entry that matched exactly what I was trying to do, so forgive me for yet another post.

I'm finding that my Visualforce page is not loading the rows correctly on the initial load. I've figured out that it is because there are no entries that match the 'null' parameter that is probably being used in my where clause. How do I pass a paremeter to my load routine on the initial load of the visual force page? I find that if I do an 'Insert' the desired table rows show up.  The code for my VF page and Controller are below:

VISUALFORCE PAGE

<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 id="Time_Entry_Form">
        <apex:pageBlock title="CDCR - Salesforce Time Reporting for Requests" id="Time_Entry_List">
            <apex:pageBlockButtons id="Button_area">
                <!-- 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}');" type="submit">New</a>               
                </apex:commandButton>
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
            <div class="displayPeek">
                <label>Result of Peek:</label>
                <apex:outputText value="{!Request__c.Id}"/>
            </div>
            <apex:pageBlockTable value="{!TimeEntries}" var="entry" id="Entry_Table_List">
                <apex:column width="45" headerValue="Action">
                    <a href="javascript:if (window.confirm('Are you sure?')) DeleteEntry('{!entry.Id}');" style="font-weight:bold">Del</a>               
                </apex:column>   
                <apex:column headerValue="Related Object">
                    <apex:inputField style="width:100%" value="{!entry.Related_Object__c}"/>
                </apex:column>  
                <apex:column width="70" headerValue="Activity">
                    <apex:inputField value="{!entry.Activity__c}"/>
                </apex:column>   
                <apex:column width="30" headerValue="Date Worked">
                    <apex:inputField value="{!entry.Date_Worked__c}"/>
                </apex:column>   
                <apex:column width="20" headerValue="Hours">
                    <apex:inputField value="{!entry.Hours_Worked__c}"/>
                </apex:column>   
                <apex:column width="20" headerValue="Worked">
                    <apex:inputField value="{!entry.Minutes_Worked__c}"/>
                </apex:column>   
                <apex:column headerValue="Work Description">
                    <apex:inputField style="width:100%" value="{!entry.Work_Description__c}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:actionFunction action="{!del}" name="DeleteEntry" reRender="Time_Entry_List">
            <apex:param name="EntryID" value="" assignTo="{!SelectedEntryID}"/>
        </apex:actionFunction>
        <apex:actionFunction action="{!add}" name="CurrRequest" reRender="Time_Entry_Form">
            <apex:param name="EntryID" value="" assignTo="{!ReqID}"/>
        </apex:actionFunction>

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




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 Related_Object__c=:ReqId order by ID DESC];

    }
    public void save()
    {
        //Update the Salesforce database with the data entred.
        update TimeEntries;
    }
    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();
    }
    public void del()
    {   
        //if we are missing the reference, then do nothing except return.
        if (SelectedEntryId == null)
        {
            return;
        }
        
        //If the record is within the collection, then delete it.
        Time_Entry__c tobeDeleted = null;
        for(Time_Entry__c a : TimeEntries)
        if (a.Id == SelectedEntryId)
            {
                tobeDeleted = a;
                break;
            }
        //If account record found then delete it.
        if (tobeDeleted != null)
            {
                Delete toBeDeleted;
            }
        //Refresh the list
        LoadData();

    }

}  
Best Answer chosen by Eric Anderson 54
Vivian Charlie 1208Vivian Charlie 1208

Hi Eric,

 

Change the constructor code as follows

public TrController(ApexPages.StandardController controller)
    {
ReqId = Apexpages.currentPage().getparameters().get('Id');        
LoadData();
     }


Thanks

Vivian

All Answers

Vivian Charlie 1208Vivian Charlie 1208

Hi Eric,

 

Change the constructor code as follows

public TrController(ApexPages.StandardController controller)
    {
ReqId = Apexpages.currentPage().getparameters().get('Id');        
LoadData();
     }


Thanks

Vivian

This was selected as the best answer
Eric Anderson 54Eric Anderson 54
Vivian Charlie,
 
Thank you for your reply, but something isn’t quite right. If you may notice in the Original Post, I had a ‘displayPeek’ div class as part of the Visualforce that would return the Object ID. In this case it is showing me a value of: ‘a03r0000000rwC0AAI'
 
I have tried three variants of code to populate the ReqId, and only the hard coded version returns any rows on the VF page. Your version and the one qualified with a ‘Request__c’ did not return anything.
 
    public TrController(ApexPages.StandardController controller)
    {
        //ReqId = Apexpages.currentPage().getparameters().get('Request__c.Id');
        ReqId = Apexpages.currentPage().getparameters().get('Id');
        //ReqId = 'a03r0000000rwC0AAI';
        LoadData();
     }
 
Do you have any other suggestions on where I might have gone wrong?
 
Thank you in advance for your help!
Eric Anderson 54Eric Anderson 54
Hi there Vivian,

Your solution worked, i figured out that part of the ID for the parent object was getting truncated. Once I got my parameter length issues figured out, I was able to get it to work correctly. Thank you for the help!

- Eric -