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
sam4n6sam4n6 

Pass filter/id in URl to diplay products based on filter

Hi,

 

I have a Custome object which is a list of products that I am displaying in sites, I have a sub navigation menu that lists the products by category, I would like to pass the category selected as a variable so that the page that is linked to just filters from teh prodycts table based on the selection.

 

Am I correct in thinking I need an ID to do this and in that case I would need a master detail realtionship for my category rather than a picklist?

 

<apex:component controller="SoftwareDFListController">
            <apex:repeat value="{!Software}" var="swdfItem">
                <table id="swtable">
                <tr><th colspan="2">{!swdfItem.Name} - {!swdfItem.Vendor__c}</th></tr>
                <tr><td class="odd">Vendor Description: </td><td><apex:outputText escape="false" value="{!swdfItem.Vendor_Description__c}"/></td></tr>
                <tr><td class="odd">Addtional Links: </td><td><apex:outputLink target="_blank" value="{!swdfItem.Vendor_Link__c}">Vendor Product Site</apex:outputLink></td></tr>
                </table>
            </apex:repeat>            
</apex:component>

 

:

ublic class SoftwareDFListController {
public Software__c[] Software {
		get {
			return [SELECT Name, Category__c, Vendor_Description__c, Vendor__c, Vendor_Link__c FROM Software__c WHERE Category__c ='Product1' ORDER BY Name];
		}
	}
	
	testmethod static void test() {
		SoftwareDFListController swdflc = new SoftwareDFListController();
		Software__c[] items = swdflc.Software;
	}
}

 

Here is my current controller I would like Category_c to be a variable can I pass the category names in a URL or do I need them to be an Id and in that case do I need to make Category it's own table / object as a lookup.

 

Thanks in advance.

 

Scott

<li><apex:outputLink value="{!$Site.Prefix}page?pageid=xxxxMAE">Product1</apex:outputLink></li>
          <li><apex:outputLink value="{!$Site.Prefix}page?pageid=xxxxMAE">Product2</apex:outputLink></li>
          <li><apex:outputLink value="{!$Site.Prefix}page?pageid=xxxxMAE">Product3</apex:outputLink></li>

 

Best Answer chosen by Admin (Salesforce Developers) 
bmabma

Ah, I think I know what you are trying to do. You can actually pass in parameters to your component from your page using apex:attribute.

 

Create an variable name category in your component's controller

 

public class SoftwareDFListController() {
  public String category {get; set;}
  /* your class declaration 
      you now can use the variable category in your query*/
}

 

 

In your component, you add the following markup:

 

<apex:component controller="SoftwareDFListController">
  <apex:attribute name="category" type="String" description="the category for this component" assignTo="category"/>
  <!-- your component markup as before -->         
</apex:component>

 

In your page, you can then pass in the category to the component like this:

 

<apex:page showheader="false" sidebar="false" standardController="Software__c">
<!-- your markup -->
    <!-- this would pass in the text 'Product' into the 'category' valuable of the component controller -->
    <!-- you can use expression if you want this value to come the page controller -->
    <c:IEGDFSoftwareList category="Product"/>
<!-- your markup-->
</apex:page

All Answers

bmabma

You don't need a master detail relationship to do what you want.

 

Using your current setup, you can use apex:param component to append parameter to the URI.

 

Here is an example:

 

<apex:component controller="SoftwareDFListController">
            <apex:repeat value="{!Software}" var="swdfItem">
                <table id="swtable">
                <tr><th colspan="2">{!swdfItem.Name} - {!swdfItem.Vendor__c}</th></tr>
                <tr><td class="odd">Vendor Description: </td><td>
                    <apex:outputText escape="false" value="{!swdfItem.Vendor_Description__c}"/></td></tr>
                <tr><td class="odd">Addtional Links: </td><td>
                    <apex:outputLink target="_blank" value="{!swdfItem.Vendor_Link__c}">
                        <apex:param name="Category" value="{!swdfItem.Category__c}">
                        Vendor Product Site</apex:outputLink></td></tr>
                </table>
            </apex:repeat>            
</apex:component>

 

Now your html output would look like the following:

 

<li><apex:outputLink value="{!$Site.Prefix}page?pageid=xxxxMAE&Category=Product1">Product1</apex:outputLink></li>

 

 

 

sam4n6sam4n6

Thanks that is awesome, and I just need to change the Controller to have a variable for Category rather than hardocded like I had it now?

sam4n6sam4n6

It was nearly awesome, this returns everything in my Software Object based on what was hardcoded into the controller, in the page that gets linked to using the bottom part of the solution you wrote the HTML part I would like to then read that Category parameter in and have the displayed records filtered on that category.

bmabma

In Apex, you can ask for the parameters from the URI using ApexPages.currentPage().getParameters(). You can then set that to a variable and pass that variable into your query statement.

 

Also, get and set functions are called multiple time during a request. It would be better to store your query result into a variable and return it, instead of performing a query every time.

 

An example would be the following:

 

public class SoftwareDFListController {
public Software__c[] Software { get; private set;}

public SoftwareDFListController(){
    //getting the value from the URI
    String category = ApexPages.currentPage().getParameters('Category');
    
    //using the variable in a query
Software = [SELECT Name, Category__c, Vendor_Description__c, Vendor__c, Vendor_Link__c FROM Software__c WHERE Category__c =: category ORDER BY Name];

    }
}

 

 

sam4n6sam4n6

Still can't seem to get this to work, is the <apex:param> tag you have described enough in my component? won't I need a CurrentPage.Parameters somewhere?

 

Is the controller complete as you currently have it do I need to get the value of Category e.g. Category=Product1 and then return Product1 for use in the variable? Also as they are String variables and contain spaces do I need to get the ' ' around the returned string?

bmabma

I should have asked this earlier. How are you accessing the component?

 

Can you post an example of the page you would be using the component in? I can probably provide you a better solution if I know how they would be interacting with each other.

sam4n6sam4n6

Hi,

 

Just putting the component in as per below, I can see how I can achieve this by making a component and controller for each category and just havinga  page and inserting a component for each category but that seems like a long way around and I am not sure whether the will impact my Sites page access later 

 

<apex:page showheader="false" sidebar="false" standardController="Software__c">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Software</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<apex:stylesheet value="{!URLFOR($Resource.iegWebMain,'style.css')}"/>
</head>
<body>
<div class="main">
  <c:IEGHeader />
    <div class="clr"></div>
  <div class="body">
     <div class="right3">
        <c:IEGDFSoftwareList />
     <div class="clr"></div>
        </div>
            <div class="right2">
            <c:IEGSWSubNav />
            </div>
        </div>
   <div class="clr"></div>
  <div class="clr"></div>
      <c:IEGSiteFooter />
  <div class="clr"></div>
</div>
</body>
</html>
</apex:page>

 Sorry I am new to this just trying to get efficiency and I can't find any good examples anywhere the Sites documentation is very light on.

 

Thanks.

bmabma

Ah, I think I know what you are trying to do. You can actually pass in parameters to your component from your page using apex:attribute.

 

Create an variable name category in your component's controller

 

public class SoftwareDFListController() {
  public String category {get; set;}
  /* your class declaration 
      you now can use the variable category in your query*/
}

 

 

In your component, you add the following markup:

 

<apex:component controller="SoftwareDFListController">
  <apex:attribute name="category" type="String" description="the category for this component" assignTo="category"/>
  <!-- your component markup as before -->         
</apex:component>

 

In your page, you can then pass in the category to the component like this:

 

<apex:page showheader="false" sidebar="false" standardController="Software__c">
<!-- your markup -->
    <!-- this would pass in the text 'Product' into the 'category' valuable of the component controller -->
    <!-- you can use expression if you want this value to come the page controller -->
    <c:IEGDFSoftwareList category="Product"/>
<!-- your markup-->
</apex:page
This was selected as the best answer
sam4n6sam4n6

Thanks for your assistance and perseverance!  That did the trick with a couple of minor adjustments

 

1) in the component <apex:attribute>  "assignTo={!category}"

 

2) and you did say this in your reply..in the page used an expression <c:componentref category="{!$CurrentPage.parameters.Category}"

 

3) I did seem to have a minor issue when passing parameters that included spaces but using the the legal character for space seemed to fix it:

 

<apex:outputLink value="{!$Site.Prefix}page?pageid=xxxxxxN&Category=Product%20One">

 Thanks again I did fin some examples using the attribute but I couldn't find a complete example anywhere so was having trouble piecing it all together those 3 extra lines of code saved me a lot of time and trouble and implemented a really nice solution for me.