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
coolkrishcoolkrish 

Passing Selected Record Id in a related list on click of button

Hi,

I am trying to pass a selected record in a related list to controller from VFP.
I am getting error: Unknown constructor 'RR_Multi_Emp_Trainings.RR_Multi_Emp_Trainings(ApexPages.StandardController controller)'

apex class:

public class RR_Multi_Emp_Trainings {   
          
  public  ApexPages.StandardSetController setCon;
        
    public RR_Multi_Emp_Trainings(ApexPages.StandardSetController controller) {
           setCon = controller;
               }    
    
public integer getMySelectedSize() { 
return setCon.getSelected().size(); 


public integer getMyRecordsSize() { 
return setCon.getRecords().size(); 


}

VFP:

<apex:page standardController="RREmployee_Trainings__c" extensions="RR_Multi_Emp_Trainings" showHeader="false" sidebar="false" setup="false">

</apex:page>

Please help.

Thanks,
Krishna.
jigarshahjigarshah
A StandardSetController is used to operate over a list of records and has the following 2 permissible signatures for its constructor.
  • Accepts a Database.QueryLocator
public StandardSetController(Database.QueryLocator queryLocator)
  • Accepts a List of custom or standard Sobject records
List<account> accountList = [SELECT Name FROM Account LIMIT 20];
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(accountList);

I think since you are only passing a single Sobject Record Id, hence, you should use the StandardController instead. Update the constructor code to as follows and then query the related records to display on your related list using a Soql Query.
public with sharing class RR_Multi_Emp_Trainings {   
    
  public ApexPages.StandardController stdController {get; set;}
      
  //Constructor
  public RR_Multi_Emp_Trainings(ApexPages.StandardController pStdController) {
	
	stdController = pStdController;
	
	//Query related records associated with the RREmployee_Trainings__c Record Id
	List<RelatedSobjectRecord> relatedSobjectRecordList = 
		[Select Id, Name 
		 From <YourSobjectName> 
		 Where <ParentIdField> = :stdController.getId()]; 
  }    
    
  //Remaining code goes here
  .
  .
  .
  
}
Morevoer, I see your Apex Controller does not have the with sharing keyword which should be included unless there is a specific reason.
coolkrishcoolkrish
Hi,

Thanks for your reply.

I have a button on the related list and I need to pass the record Id of the selected record on click of that button to my extension controller.
This very Id is not getting passed.

Here is the code of my extension controller:

public with sharing class RR_Multi_Emp_Trainings {           

    public ApexPages.StandardController setCon{get;set;}
    public RREmployee_Trainings__c etObj {get;set;}  
    
    public RR_Multi_Emp_Trainings(ApexPages.StandardController controller) {
        this.setCon = controller;
        this.etObj = (RREmployee_Trainings__c)setCon.getRecord();
        Id aId = etObj.Id;
        System.debug('etObj:'+ etObj);

What is wrong in this....I am not getting value into etObj.
Expectation is to get the record selected in the related list into this variable on click of a button.

 
jigarshahjigarshah
I am assuming that the related list is a custom Visualforce Page which is added to a standard Page layout as a section. Hence the controller for the Visualforce page should be set to the SObject type of the standard page and extensions as you have written.

Moreover, you will need to use any of the following techniques to make the selected Id available to your controller extension.
  • In order to make the Record Id value available from the UI to the Apex Controller code, you can use the <apex:param> component offered by Visualforce to pass the value to the serverside Controller code.
    • You will also need a property of type Id within your Extension to hold the record Id value.
    • Refer the following link to understand further on using apex:param within your code - https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_param.htm
  • If there is redirection happening on the click of the button then you will need to add the selected Record Id as a query string parameter which can be done as follows
public Pagereference redirectPage(){
	Pagereference pg = Page.YourVisualforcePageName;
	pg.getParameters().put('id', your_selected_id_value)
	pg.setredirect(true);
	return pg;
}