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
Alex Waddell 18Alex Waddell 18 

Pass Record Id into Visual Force page

I am learning Apex and am trying to get my controller to query for Resource_List__c records where the Survey_Form__c field = :RecordId (the current Survery_Form__c record I am viewing)

VF Page
<apex:page Controller="ResourceListController">
    <apex:form>
    <apex:pageBlock>
<apex:pageBlockTable value="{! ResourceList }" var="RL">
    <apex:column value="{! RL.Id }"/>
    
</apex:pageBlockTable>
    </apex:pageBlock>
        </apex:form>
</apex:page>

Class
public class ResourceListController {
   public Id RecordId {get;set;}    
public List<Resource_List__c> getResourceList() {
    
    List<Resource_List__c> results = Database.query(
        'SELECT Id, Account__c, Distance__c, type__c ' +
        'FROM Resource_List__c ' + 
        'Where Survey_Form__c = :RecordId '
    );
    return results;
}
}

VF Page Output when on the Lightning page
User-added image
but when I put the query into the query editor and hard code the record Id there are values in this table

User-added image

Is there any way to pass the record id into the VF page without invoking aura or LWC? I just want to learn the fundamentals before I step into those new languages
Best Answer chosen by Alex Waddell 18
Dushyant SonwarDushyant Sonwar
Alex,

You did a great job as newbie learning apex, the list is not filling because you need to get recordId and set into "Recordid" variable.

ApexPages.currentPage().getParameters().get('id');

The above line will get id URL parameter from browser URL and set into your declared variable 'RecordId'.
 
public class ResourceListController {
   public Id RecordId {get;set;}    
public List<Resource_List__c> getResourceList() {
    RecordId  = ApexPages.currentPage().getParameters().get('id');
    List<Resource_List__c> results = Database.query(
        'SELECT Id, Account__c, Distance__c, type__c ' +
        'FROM Resource_List__c ' + 
        'Where Survey_Form__c = :RecordId '
    );
    return results;
}
}

Hope this helps. Keep learning!
 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Alex,

I found the below article url's which I think might be useful, please have a look at them:

>> https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_System_ApexPages_currentPage.htm

>> http://www.sfdcpoint.com/salesforce/get-current-record-id-salesforce/

>> http://www.infallibletechie.com/2013/05/visualforce-email-template-with-custom.html

Hope this is useful for you to implement your need. Please close the thread by marking a best answer so that it can be useful for others in the future and can help in keeping the community clean.

Regards,
Anutej
Dushyant SonwarDushyant Sonwar
Alex,

You did a great job as newbie learning apex, the list is not filling because you need to get recordId and set into "Recordid" variable.

ApexPages.currentPage().getParameters().get('id');

The above line will get id URL parameter from browser URL and set into your declared variable 'RecordId'.
 
public class ResourceListController {
   public Id RecordId {get;set;}    
public List<Resource_List__c> getResourceList() {
    RecordId  = ApexPages.currentPage().getParameters().get('id');
    List<Resource_List__c> results = Database.query(
        'SELECT Id, Account__c, Distance__c, type__c ' +
        'FROM Resource_List__c ' + 
        'Where Survey_Form__c = :RecordId '
    );
    return results;
}
}

Hope this helps. Keep learning!
 
This was selected as the best answer
Alex Waddell 18Alex Waddell 18
Dushyant,

Thank you for your help and positivity! The code worked and I'm on to the next challenge