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
Saurabh Singh 133Saurabh Singh 133 

Hi, I want to pass an Id from VF Page to Controller by using apex:param. Following is my code please check and provide best code for it.

 <apex:commandLink value="{!stu.Student_Name__c}" action="{!StudentDetailsPage}">
                                                            <apex:param name="Id" value="{!stu.Id}" assignTo="{!StuDetailId}"/>
                                                        </apex:commandLink>


Please provide code for assign this parameters value to controller page variable.
Best Answer chosen by Saurabh Singh 133
Khan AnasKhan Anas (Salesforce Developers) 
Hi Saurabh,

Greetings to you!

The apex:param tag is used to pass values from the Visualforce Page to the Apex Controller. Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

sObject used: Student__c

Visualforce:
<apex:page controller="ApexParamDemoController">
  <apex:form id="form">
    <apex:pageBlock>
      <apex:pageBlockTable value="{!AllStu}" var="stu">
        <apex:column value="{!stu.Name}"/>
        <apex:column value="{!stu.Student_Name__c}"/>
        <apex:column>
          <apex:commandLink value="Delete" action="{!deleteStu}">
            <apex:param name="studentToDelete" value="{!stu.Id}" assignTo="{!stuDetailId}"/>
          </apex:commandLink>
        </apex:column>
      </apex:pageBlockTable>
    </apex:pageBlock>
  </apex:form>
</apex:page>

Controller:
public class ApexParamDemoController{

    //Property to hold the Id of the Record to be Deleted
    public Id stuDetailId {get; set;}
    
    //Query all the Students from the Database
    public List<Student__c> AllStu{
        get{ return [SELECT Id, Name, Student_Name__c FROM Student__c]; }
    }
    
    //Delete the Record from the Database
    public void deleteStu(){
        DELETE new Student__c(Id = stuDetailId);
    }
}

Or you can use below controller:
public class ApexParamDemoController{

    public List<Student__c> AllStu {get;set;}
    
    //Property to hold the Id of the Record to be Deleted
    public Id stuDetailId {get; set;}
    
    //Query all the Students from the Database
    public ApexParamDemoController{
        AllStu = [SELECT Id, Name, Student_Name__c FROM Student__c];
    }
    
    //Delete the Record from the Database
    public void deleteStu(){
        try {
            AllStu = [SELECT Id FROM Student__c WHERE Id = :stuDetailId];
            DELETE AllStu;
        } 
        catch (Exception e) {
            ApexPages.addMessages (e);
        }    
    }
}

The apex:param tag is responsible for passing the Id of the Student record. The apex:param passes the value contained in {!stu.Id} and assigns it to the property back in the Apex Class - RecordToDelete. So when the Delete button is clicked first the value will be assigned to the stuDetailId via the apex:param tag and then the method - deleteStu will be executed.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Saurabh,

Greetings to you!

The apex:param tag is used to pass values from the Visualforce Page to the Apex Controller. Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

sObject used: Student__c

Visualforce:
<apex:page controller="ApexParamDemoController">
  <apex:form id="form">
    <apex:pageBlock>
      <apex:pageBlockTable value="{!AllStu}" var="stu">
        <apex:column value="{!stu.Name}"/>
        <apex:column value="{!stu.Student_Name__c}"/>
        <apex:column>
          <apex:commandLink value="Delete" action="{!deleteStu}">
            <apex:param name="studentToDelete" value="{!stu.Id}" assignTo="{!stuDetailId}"/>
          </apex:commandLink>
        </apex:column>
      </apex:pageBlockTable>
    </apex:pageBlock>
  </apex:form>
</apex:page>

Controller:
public class ApexParamDemoController{

    //Property to hold the Id of the Record to be Deleted
    public Id stuDetailId {get; set;}
    
    //Query all the Students from the Database
    public List<Student__c> AllStu{
        get{ return [SELECT Id, Name, Student_Name__c FROM Student__c]; }
    }
    
    //Delete the Record from the Database
    public void deleteStu(){
        DELETE new Student__c(Id = stuDetailId);
    }
}

Or you can use below controller:
public class ApexParamDemoController{

    public List<Student__c> AllStu {get;set;}
    
    //Property to hold the Id of the Record to be Deleted
    public Id stuDetailId {get; set;}
    
    //Query all the Students from the Database
    public ApexParamDemoController{
        AllStu = [SELECT Id, Name, Student_Name__c FROM Student__c];
    }
    
    //Delete the Record from the Database
    public void deleteStu(){
        try {
            AllStu = [SELECT Id FROM Student__c WHERE Id = :stuDetailId];
            DELETE AllStu;
        } 
        catch (Exception e) {
            ApexPages.addMessages (e);
        }    
    }
}

The apex:param tag is responsible for passing the Id of the Student record. The apex:param passes the value contained in {!stu.Id} and assigns it to the property back in the Apex Class - RecordToDelete. So when the Delete button is clicked first the value will be assigned to the stuDetailId via the apex:param tag and then the method - deleteStu will be executed.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Madhukar_HeptarcMadhukar_Heptarc
Hi Saurabh Singh ,
I understand your requirement based on the requirement i made some examplee code 
Apex code :

public class Community_Post_Param {
// assigning for static method
    public string value { get; set;} 
    public void testdirect(){
        system.debug(value);
    }
// Assigning the param id 
    public void testinput(){
        value = apexpages.currentPage().getParameters().get('inpval');
        system.debug(value);
    }
}
    
Visualforce page code :    

<apex:page controller="Community_Post_Param">
<apex:form>
<!-- Code for Static method --> 
<apex:commandbutton action="{!testdirect}" reRender="test" value="Static value">
<apex:param assignTo="{!value}" value="The static value that was set from vf page"/>
</apex:commandbutton><br/>
    <!-- Code for dynamic method -->
<input type="text" id="testinput"/>
<input type="button" onclick="testinputJS()" value="Dynamic Value" class="btn"/>
    <!-- Rendering thee Test Direct -->
<apex:outputPanel id="test">
<apex:outputText value="{!value}"/>
</apex:outputPanel>
    <!-- accepting the input from visual force code -->
<apex:actionFunction action="{!testinput}" name="passToController" rerender="test">
<apex:param value="" name="inpval"/>
</apex:actionFunction>
</apex:form>
<script>
function testinputJS(){
var str = document.getElementById('testinput').value;
if(str.length >20){
str= str.substring(0,20);
}
passToController(str);
}
</script>
</apex:page>
hope it helps you
 
Saurabh Singh 133Saurabh Singh 133
Thanks To both of you. 
Thanks Khan anas its works and its solved my query.

Thanks and Regards 
Saurabh Singh