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
Amit Visapurkar 5Amit Visapurkar 5 

How to pass the id from visualforce page when i click the preview button

User-added image

When i click on the preview button the id of the associated record id should get passed to the controller. Its urgent
Best Answer chosen by Amit Visapurkar 5
Shailendra Singh ParmarShailendra Singh Parmar
Try below page code.
<apex:page standardController="FeedbackMain__c" extensions="FeedbackMainlistController">
<apex:form >
<apex:pageBlock id="pg">
<apex:pageMessages />

<apex:pageBlockTable value="{!feed}" var="a" rowClasses="oddrow,evenrow,dataTableRow" styleClass="fontfamily" id="Details1" style="width:50%;">

<apex:column headerValue="Feedback Number" style="width:10%;">
 <apex:outputField value="{!a.Name}" id="test" />
<apex:variable var="vara" value="{!a.Name}" />
</apex:column>
<apex:column headerValue="Feedback Preview" style="width:45%;" >
  <apex:commandButton value="Preview" style="width:35%;" >
  <apex:actionSupport action="{!queslist}" event="onclick" reRender="pb">
    <apex:param assignTo="{!selectedId}" value="{!a.id}" name="selectedId"/>
  </apex:actionsupport>
 </apex:commandButton>
</apex:column>
</apex:pageBlockTable>


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

Also put System.debug to print selectedid in controller before you assing value into it from request parameter.

All Answers

Amit Visapurkar 5Amit Visapurkar 5
Below is the code :

<apex:pageBlockTable value="{!feed}" var="a" rowClasses="oddrow,evenrow,dataTableRow" styleClass="fontfamily" id="Details1" style="width:50%;"> <apex:column headerValue="Feedback Preview" style="width:35%;" > <apex:inputHidden value="{!a.id}" id="theField" /> <script> var theField = document.getElementById('{!$Component.theField}'); </script> </apex:column> <apex:column headerValue="Feedback Number" style="width:10%;"> <apex:outputField value="{!a.Name}" id="test" /> <apex:param name="Selectedid" value="{!a.Name}"/> <apex:variable var="vara" value="{!a.Name}" /> </apex:column> <apex:column headerValue="Feedback Preview" style="width:45%;" > <apex:commandButton action="{!queslist}" onclick="theField.value=v;" value="Preview" style="width:35%;" /> </apex:column> </apex:pageBlockTable>



public class FeedbackMainlistController { Public Id qid{get;set;} public string Selectedid {get;set;} public List <FeedbackMain__c>queslist; public List<FeedbackMain__c> showQues; public List<FeedbackMain__c> feed; public List<Feedback_Question_Main__c> question; public FeedbackMainlistController(ApexPages.StandardController controller) { selectedid= System.currentPageReference().getParameters().get('Selectedid '); system.debug('*********Selected id is:*******************'+selectedid); qid=controller.getId(); //system.debug('*********Selected id is:*******************'+qid); //system.debug('*********Selected id is:*******************'+Selectedid); } public List<FeedbackMain__c> getFeed() { feed=[SELECT id,name,Feedback_Name__c,Related_Question_Bank__r.name FROM FeedbackMain__c]; return feed; } public List<Feedback_Question_Main__c> getQuestion() { question=[SELECT name,Related_Feedback__c,Option_Five__c,Option_Four__c,Option_One__c,Option_Three__c,Option_Two__c,Question__c FROM Feedback_Question_Main__c where Related_Feedback__c='a0036000003zDGR']; return question; } public pageReference queslist() { selectedid= System.currentPageReference().getParameters().get('Selectedid'); showQues=[SELECT Related_Question_Bank__r.Name FROM FeedbackMain__c where id=:'a0036000003zDGR']; system.debug('*********Selected id is:*******************'+Selectedid); PageReference pageRef = new PageReference('/apex/FeedbackQuestionMain'); pageRef.setRedirect(true); return pageRef ; } }

 
Shailendra Singh ParmarShailendra Singh Parmar
You have 2 way to do this.
#1. Using apex Actionsupport tag like below
#2. Using action function and on button click call that JS function which in return will call action by passing parameter that you passed to js function
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm (Use this along with apex:param to pass parameter)
 
<apex:commandButton  value="Preview" style="width:35%;" >
<apex:actionSupport action="{!queslist}" event="onclick" >
<apex:param assignTo="{!selectedId}"  value="{!a.id}" name="selectedI"/>
</apex:actionsupport>
</apex:commandButton>
Let me know if that help, i might get some example if case you need.

Thanks!
Amit Visapurkar 5Amit Visapurkar 5
@ Shailendra 

<apex:column headerValue="Feedback Preview" style="width:45%;" >
<apex:commandButton value="Preview" style="width:35%;" >
<apex:actionSupport action="{!queslist}" event="onclick" >
<apex:param assignTo="{!selectedId}" value="{!a.id}" name="selectedI"/>
</apex:actionsupport>
</apex:commandButton>
</apex:column>

Used the following code.still its not working
Shailendra Singh ParmarShailendra Singh Parmar
Could you post complete code. I just suspect if you have selectedId variable at control side and apex:pagesMessage tag on page to display if their are some validation errors.
Amit Visapurkar 5Amit Visapurkar 5
Here is the complete code

<apex:page standardController="FeedbackMain__c" extensions="FeedbackMainlistController">
<apex:form >
<apex:pageBlock >
<Script>
variable v=

</script>

<apex:pageBlockTable value="{!feed}" var="a" rowClasses="oddrow,evenrow,dataTableRow" styleClass="fontfamily" id="Details1" style="width:50%;">


<apex:column width="1%" >
<apex:inputHidden value="{!a.id}" id="theField" />
<apex:param name="Selectedid" value="{!a.id}"/>
<script> var theField = document.getElementById('{!$Component.theField}'); </script>

</apex:column>

<apex:column headerValue="Feedback Number" style="width:10%;">



<apex:outputField value="{!a.Name}" id="test" />
<apex:param name="Selectedid" value="{!a.Name}"/>
<apex:variable var="vara" value="{!a.Name}" />
</apex:column>

<apex:column headerValue="Feedback Preview" style="width:45%;" >
<apex:commandButton value="Preview" style="width:35%;" >
<apex:actionSupport action="{!queslist}" event="onclick" >
<apex:param assignTo="{!selectedId}" value="{!a.id}" name="selectedI"/>
</apex:actionsupport>
</apex:commandButton>
</apex:column>
</apex:pageBlockTable>


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



public class FeedbackMainlistController
{
Public Id qid{get;set;}
public string Selectedid {get;set;}
public List <FeedbackMain__c>queslist;
public List<FeedbackMain__c> showQues;
public List<FeedbackMain__c> feed;
public List<Feedback_Question_Main__c> question;

public FeedbackMainlistController(ApexPages.StandardController controller)
{
selectedid= System.currentPageReference().getParameters().get('Selectedid ');
system.debug('*********Selected id is:*******************'+selectedid);
qid=controller.getId();
//system.debug('*********Selected id is:*******************'+qid);
//system.debug('*********Selected id is:*******************'+Selectedid);
}

public List<FeedbackMain__c> getFeed()
{
feed=[SELECT id,name,Feedback_Name__c,Related_Question_Bank__r.name FROM FeedbackMain__c];
return feed;
}

public List<Feedback_Question_Main__c> getQuestion()
{
question=[SELECT name,Related_Feedback__c,Option_Five__c,Option_Four__c,Option_One__c,Option_Three__c,Option_Two__c,Question__c FROM Feedback_Question_Main__c where Related_Feedback__c='a0036000003zDGR'];
return question;
}


public pageReference queslist()
{
selectedid= System.currentPageReference().getParameters().get('Selectedid');

showQues=[SELECT Related_Question_Bank__r.Name FROM FeedbackMain__c where id=:'a0036000003zDGR'];

system.debug('*********Selected id is:*******************'+Selectedid);
PageReference pageRef = new PageReference('/apex/FeedbackQuestionMain');
pageRef.setRedirect(true);

return pageRef ;
}
}
Amit Visapurkar 5Amit Visapurkar 5
@ Shailendra 

Can you please post the solution again
Shailendra Singh ParmarShailendra Singh Parmar
Hi Amit,
It looks good. Could you make following 2 changes.
  1.  Add <apex:pageMessages /> at very first statement of <apex:pageBlock > and give id to pageblock and add rerender= pageblock id to apex:actionSupport stement
  2. get parameter has capital s for selectedid so fix it also put sop before you populate it from request parameter because salesforce automatically set value from fields and you may not additionally need to do that.
Thanks!
 
Amit Visapurkar 5Amit Visapurkar 5
@ Shailendra 
showQues=[SELECT Related_Question_Bank__r.Name FROM FeedbackMain__c where id=:'a0036000003zDGR'];

I want the id to be dynamic that is why i want the associated id on button click in the controller..
Shailendra Singh ParmarShailendra Singh Parmar
Yes. That's what we are doing in actionsupport, we are passing a.id to selectedId field which you use in your query. 
Amit Visapurkar 5Amit Visapurkar 5
@ Shailendra 

But in the controller i am getting the id as null.

Can you please post the corrected code as i am not able to figure out the exact way that you have suggested
Shailendra Singh ParmarShailendra Singh Parmar
Try below page code.
<apex:page standardController="FeedbackMain__c" extensions="FeedbackMainlistController">
<apex:form >
<apex:pageBlock id="pg">
<apex:pageMessages />

<apex:pageBlockTable value="{!feed}" var="a" rowClasses="oddrow,evenrow,dataTableRow" styleClass="fontfamily" id="Details1" style="width:50%;">

<apex:column headerValue="Feedback Number" style="width:10%;">
 <apex:outputField value="{!a.Name}" id="test" />
<apex:variable var="vara" value="{!a.Name}" />
</apex:column>
<apex:column headerValue="Feedback Preview" style="width:45%;" >
  <apex:commandButton value="Preview" style="width:35%;" >
  <apex:actionSupport action="{!queslist}" event="onclick" reRender="pb">
    <apex:param assignTo="{!selectedId}" value="{!a.id}" name="selectedId"/>
  </apex:actionsupport>
 </apex:commandButton>
</apex:column>
</apex:pageBlockTable>


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

Also put System.debug to print selectedid in controller before you assing value into it from request parameter.
This was selected as the best answer
Amit Visapurkar 5Amit Visapurkar 5
@ Shailendra 

I was able to get the id in the queslist() successfully .However when i try to get the id in getQuestion() the debug log is showing it as null.

Not able to get why this is happening. 

public class FeedbackMainlistController
{
Public Id qid{get;set;}
public string Selectedid {get;set;}
String idfetch;
public List <FeedbackMain__c>queslist;
public List<FeedbackMain__c> showQues;
public List<FeedbackMain__c> feed;
public List<Feedback_Question_Main__c> question;

public FeedbackMainlistController(ApexPages.StandardController controller)
{
selectedid= System.currentPageReference().getParameters().get('Selectedid');
}

public List<FeedbackMain__c> getFeed()
{
feed=[SELECT id,name,Feedback_Name__c,Related_Question_Bank__r.name FROM FeedbackMain__c];
return feed;
}

public pageReference queslist()
{
//selectedid= System.currentPageReference().getParameters().get('Selectedid');

showQues=[SELECT Related_Question_Bank__r.Name FROM FeedbackMain__c where id=:Selectedid];

system.debug('*********Selected id is in ques list :*******************'+Selectedid);
idfetch=Selectedid;
system.debug('*********Selected id fetched :*******************'+idfetch);
PageReference pageRef = new PageReference('/apex/FeedbackQuestionMain');
// PageReference pageRef = new PageReference('/apex/FeedbackQuestionMain?param='+Selectedid);
pageRef.setRedirect(false);

return pageRef ;
}


public List<Feedback_Question_Main__c> getQuestion()
{
system.debug('*********Selected id fetched :*******************'+idfetch);
question=[SELECT name,Related_Feedback__c,Option_Five__c,Option_Four__c,Option_One__c,Option_Three__c,Option_Two__c,Question__c FROM Feedback_Question_Main__c where Related_Feedback__c=:Selectedid];
system.debug('**************************************'+question);
return question;
}
}