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

help with vf reusable component controller and passing values


Hope someone could assist me with this. 


I have a component which I'd like to be reusable.


The only purpose of this component is to find a record (through a series of picklist values) and return the matching record from the list  when the button is clicked (there's only going to be one record always). 


The component should be able to get the record ID and pass it on to the page where the component is used.


The vf page where the controller is used uses another controller.



1. How can I pass the record ID found in the component to the page's controller without using a URL parameter? I do not want the ID to be seen in the URL where it can be edited or removed.


2. To make the vfp reusable, what code should I use so that the PageReference value is dynamic instead of hardcoding the actual page names where the ID will be used?



I'm still trying to learn a lot from Apex/VFP but if anyone could point to the right direction it would be very helpful.






I wrote a blog post on (I think) this very topic at:


This explains how to pass information from the page into the controller, and allow the component to update it so that it is accessible to the page.


this is great! i'll make sure to read on this and report back :)



Please explain the usecase in more detail.


However based on current scenario , why dont you make use of hidden field.





<apex:inputHidden value="{!Id}"/> </apex:component>

Thought you wanted to pass value from Component to Page . Turns out to be opposite


Hi Sammy,

That is correct, I wanted to pass the record ID that is retrieved from the query of the component controller to the VF Page where the component is used.


The use case is this:


We are building a page where we want to display certain information about our products. We've decided to use a custom object to contain all of our products. In the component are three picklist values:

1. Product Family

2. Product Group

3. Version


They are configured with dependencies. 


Each product is stored as record. The custom object was configured so that there is no duplicate record of the same product. The component's controller should only be able to retrieve one record when it queries the custom object.


The goal is to have a page where there is a product selector (in this case a component) and display the resulting record in the page. So naturally, the page's controller should be able to read from the record ID and display the values to the page.




I was looking at your code and have to ask. The command button in the vfpage doesnt have an action method. Only a value (Update). Does the 'Update' value work like an action method here?





You can store the Id in a hidden field and make use of action support or actionfunction and pass the corresponding ID to the controller.


In action Support and actionFunction, you can make use of apex:param to pass values to the controller and make use of rerender attribute to display the details .



Thanks Bob and Sammy for all your help. I've managed to get my page working.


There's one last thing that I need help with.


As I mentioned in my original post, the component does a search on the custom objects record and returns it when found. I've got this part working. What i'm having difficulty is preventing the page from returning an error when there is no record found.


So what happens is as soon as I clicked the button and there is no matching record, it shows me the unauthorized page.


Now, the way this can be solved with my object is to make sure that all picklist values have an associated record, but that's not good because there may be new picklist values added in the future and a record may not be created yet for that.


Here's my search method. I've added an IF statement to return a message that no record was found if the size of the list is not equal to 1. Instead, it throws an error.


The else part is working for me.


By the way, my button is simply reRendering a section of the VFpage with the values from the record so there is no actual page that it needs to be redirected to.



	public pageReference Search() {
		List<Support_Policy__c> qry = [SELECT Id, Support_Policy_Type__c, 
				Product_Image_Link__c, Policy_Identifier__c, Phone__c, Web2Case__c, 
				Chat__c, User_Community_Link__c, Patch_and_Updates__c, 
				Article_API_Name__c, Upgrade_Link__c
				FROM Support_Policy__c 
				WHERE RecordTypeID = :Label.Support_Policy_Record_ID
				AND Version__c = :SupportPolicy.Version__c];
		getPolicy = qry;
			if (getPolicy.size() != 1) {
				ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'No Record Found'));
				return null;
			} else {		
				return null;






What is the variable type of getPolicy?


it's declared as:


public List<Support_Policy__c> getPolicy { get; set; }


Hmm. I don't see why that would cause an unuathorized page - usually that means that an error has occurred.  Have you tried turning on debug logging?


I think it's because the search result did not find any records. If I run the search while in development mode this is what i get:


Subscript is invalid because list is empty

Error is in expression '{!NOT(ISBLANK(getPolicy[0].Article_API_Name__c))}' in component <apex:outputPanel> in page selectproductpage
Would adding an exception handler help in this situation?

Is this formula attempting to stop display of the output panel if the list is empty?