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
Rick RollRick Roll 

Retrieving records from custom object into picklist and show dynamic table

User-added image
Hi everyone!
I have an object Pattern__c with 3 fields.
I want to retrieve the records from Pattern Name(Name) into a selectlist with no duplicates like shown above (I only made it with html).
After choosing the value of the picklist and clicking the button, the pageblocktable should only show the categories under that Pattern Name.(mine shows all at the moment)
ex. selecting Pattern A should only show 3 rows in the pageblocktable
heres what I did:
Page
<apex:page sidebar="false" controller="GetPattern">
  <apex:stylesheet value="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
   <apex:includeScript value="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" />
  <div class="row">
       <div class="col-md-4">
          <apex:pageBlock title="Pattern">
            <apex:form>
            <select>
                <option disabled="true">Select Pattern</option>
                <option>Pattern A</option>
                <option>Pattern B</option>
                <option>Pattern C</option>
            </select>
            <input type="button" value="Process"/>
            </apex:form>
         </apex:pageBlock>         
       </div>
       <div class="col-md-8">
         <apex:pageBlock title="Pattern Summary">
            <apex:pageBlockTable value="{!categorylist}" var="cat">
               <apex:column headerValue="Category A" value="{!cat.CategoryA__c}"/>
               <apex:column headerValue="Category B" value="{!cat.CategoryB__c}"/>
            </apex:pageBlockTable>
         </apex:pageBlock>         
       </div>
   </div>
</apex:page>
class
public class GetPattern{
    public List<Pattern__c> categorylist {get; set;}
    public GetPattern()
    {
        this.GetRecords();
    }
    public void GetRecords()
    {
        categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c];
    }
}
How do I do these things?
 
Best Answer chosen by Rick Roll
Amit Chaudhary 8Amit Chaudhary 8
Hi,

You Can try below full solution :-

Page Should be :-
<apex:page sidebar="false" controller="GetPattern">
        <apex:pageBlock title="Pattern">
            <apex:form>
				<apex:selectList value="{!filterId}" size="1">
					<apex:actionSupport event="onchange" action="{!processRequests}"  rerender="PB1"/>
					<apex:selectOptions value="{!items}"/>
				</apex:selectList>
            </apex:form>
         </apex:pageBlock>         
		 
         <apex:pageBlock title="Pattern Summary" id="PB1">
            <apex:pageBlockTable value="{!categorylist}" var="cat">
               <apex:column headerValue="Name" value="{!cat.Name}"/>
               <apex:column headerValue="Category A" value="{!cat.CategoryA__c}"/>
               <apex:column headerValue="Category B" value="{!cat.CategoryB__c}"/>
            </apex:pageBlockTable>
         </apex:pageBlock>         
</apex:page>

Class Should be :-
public class GetPattern
{
    public string FilterId{get;set;}
    public List<Pattern__c> categorylist {get; set;}
    public GetPattern()
    {
       categorylist = new List<Pattern__c>();
    }
    
    public PageReference processRequests()
    {
        categorylist.clear();
        if(FilterId !='' && FilterId != null)
        {
            categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c where Name = :FilterId ];
        }
        else
        {
            categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c limit 1000 ];
        }
        return null;
    }

    public List<SelectOption> getItems() 
    {
        List<SelectOption> options = new List<SelectOption>();
        List<Pattern__c> categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c limit 1000];
        Set<String> patterns = new Set<String>();
        
        for(Pattern__c pattern :categorylist)
        {
            if(patterns.contains(pattern.Name))
            {
                options.add(new SelectOption(pattern.Name,pattern.Name ));
                patterns.add(pattern.Name);
            }        
        }    
        return options;
    }

}


Test Class Should be :-
 
@isTest 
public class GetPatternTest 
{
	static testMethod void GetPatternT() 
	{
		List<Pattern__c> lstPattern = new List<Pattern__c>();
		Pattern__c p1 = new Pattern__c();
		p1.Name = 'Pattern A';
		P1.CategoryA__c ='Code';
		P1.CategoryB__c ='ItemCode';
		lstPattern.add(p1);

		Pattern__c p2 = new Pattern__c();
		p2.Name = 'Pattern B';
		P2.CategoryA__c ='Code';
		P2.CategoryB__c ='ItemCode';
		lstPattern.add(p2);

		insert lstPattern;

		Test.StartTest(); 
			GetPattern contrl = new GetPattern();
			List<SelectOption> lst = contrl.getItems();
			contrl.FilterId ='Pattern A';
			contrl.processRequests();
		Test.StopTest();
		
	}
 
}

You can check Test class best pratice on below  post :-
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
http://amitsalesforce.blogspot.in/2015/06/salesforce-testing-best-practice.html

Apex best Pratice at below post:-
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Please mark this as solution if this will help you. So if some one has same issue this post can help others.

Thanks
Amit Chaudhary
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi Rick Roll,

Please try below code. I hope that will help you:-

Class should be :-
public class GetPattern
{
	public string FilterId{get;set;}
	
    public List<Pattern__c> categorylist {get; set;}
    public GetPattern()
    {
       categorylist = new List<Pattern__c>();
	   GetRecords();
    }
	
    public PageReference processRequests()
    {
		categorylist.clear();
        categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c where Name = :FilterId ];
        return null;
    }
	
    public void GetRecords()
    {
        categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c];
    }
	
	public List<SelectOption> getItems() 
	{
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Pattern A','Pattern A'));
        options.add(new SelectOption('Pattern B','Pattern B'));
        options.add(new SelectOption('Pattern C','Pattern C'));
        return options;
    }

}
Page Should be:-
<apex:page sidebar="false" controller="GetPattern">

         <apex:pageBlock title="Pattern">
            <apex:form>
				<apex:selectList value="{!filterId}" size="1">
					<apex:actionSupport event="onchange" action="{!processRequests}"  rerender="cases_table"/>
					<apex:selectOptions value="{!items}"/>
				</apex:selectList>
            </apex:form>
         </apex:pageBlock>         
		 
         <apex:pageBlock title="Pattern Summary">
            <apex:pageBlockTable value="{!categorylist}" var="cat">
               <apex:column headerValue="Category A" value="{!cat.CategoryA__c}"/>
               <apex:column headerValue="Category B" value="{!cat.CategoryB__c}"/>
            </apex:pageBlockTable>
         </apex:pageBlock>         
		 
</apex:page>

Please mark this as solution if this will help you.

Thanks,
Amit Chaudhary
amit.salesforce21@gmail.com
Rick RollRick Roll
@Amit Chaudhary 8
Thanks for spending time in providing some good code. I added id="cases_table" to the pageBlock and it worked great. My only concern is that you statically added options which was one of my main concerns that's why I asked for help (I read most examples were also static) and I need it to be dynamic (from the Pattern__c : Name) with no repeating values on selectlist if possible.
surasura
try below code  I think it will serve your purposse, let me know if it has any errors

class 
-------------------------------------
public class GetPattern{
    public List<Pattern__c> categorylist {get; set;}
	public List<SelectOption> patternOptions {get;set;}
	public String SelectedPattern {get;set;}
    public GetPattern()
    {
        this.GetRecords();
    }
    public void GetRecords()
    {
	    patternOptions = new List<SelectOption>();
		patternOptions.add(new SelectOption('', 'Select'));

        categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c];
		Set<String> patterns = new Set<String>();
		for(Pattern__c pattern :categorylist)
		{
		    if(patterns.contains(pattern.Name))
			{
			    patternOptions.add(new SelectOption(pattern.Name,pattern.Name ));
				patterns.add(pattern.Name);
			}
		}
    }
	
	public void  searchPatterns()
    {

        categorylist.clear();
        if(SelectedPattern !='' && SelectedPattern != null)
		{
			categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c where Name = :SelectedPattern ];
		}
        else
		{
		    categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c ];

        }		

        

    }

	
}

Page
-----------------------------------
 
<apex:page sidebar="false" controller="GetPattern">

         <apex:pageBlock title="Pattern">
            <apex:form>
				<apex:selectList value="{!SelectedPattern}" size="1">
					<apex:actionSupport event="onchange" action="{!searchPatterns}"  rerender="results"/>
					<apex:selectOptions value="{!patternOptions}"/>
				</apex:selectList>
            </apex:form>
         </apex:pageBlock>         
		 
         <apex:pageBlock title="Pattern Summary" id="results">
            <apex:pageBlockTable value="{!categorylist}" var="cat">
               <apex:column headerValue="Category A" value="{!cat.CategoryA__c}"/>
               <apex:column headerValue="Category B" value="{!cat.CategoryB__c}"/>
            </apex:pageBlockTable>
         </apex:pageBlock>         
		 
</apex:page>



 
Rick RollRick Roll
@sura It only shows Select on the selectlist...
surasura
my bad pleace replace the controller with below code 
 
public class GetPattern{
    public List<Pattern__c> categorylist {get; set;}
	public List<SelectOption> patternOptions {get;set;}
	public String SelectedPattern {get;set;}
    public GetPattern()
    {
        this.GetRecords();
    }
    public void GetRecords()
    {
	    patternOptions = new List<SelectOption>();
		patternOptions.add(new SelectOption('', 'Select'));

        categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c];
		Set<String> patterns = new Set<String>();
		for(Pattern__c pattern :categorylist)
		{
		    if(!patterns.contains(pattern.Name))
			{
			    patternOptions.add(new SelectOption(pattern.Name,pattern.Name ));
				patterns.add(pattern.Name);
			}
		}
    }
	
	public void  searchPatterns()
    {

        categorylist.clear();
        if(SelectedPattern !='' && SelectedPattern != null)
		{
			categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c where Name = :SelectedPattern ];
		}
        else
		{
		    categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c ];

        }		

        

    }

	
}

 
Rick RollRick Roll
Thx so much! @sura...it works now. I wanna ask for a few more things if its not too much.
1. A test class for your code (I don't get much of it yet so it'll be hard for me to build one)
2. How do I disable the Select in the selectlist? jquery?
surasura
test Class 
 
@isTest 
private class GetPatternTestClass {
    static testMethod void GetPatternTest() {
	
	   Pattern__c p1 = new Pattern__c();
	   p1.Name ='Test Patttern'
	   p1.CategoryA__c = 'xxxx';
	   p1.CategoryB__c = 'yyyyy';
	   
	   insert p1;
	   
	   GetPattern gp = new GetPattern();
	   gp.searchPatterns();
	   gp.SelectedPattern ='Test Patttern';
	   gp.searchPatterns();
	
       
    }
}

try replacing line 12 of my class with below line 
 
patternOptions.add(new SelectOption('', 'Select',true));

 
Amit Chaudhary 8Amit Chaudhary 8
Hi,

You Can try below full solution :-

Page Should be :-
<apex:page sidebar="false" controller="GetPattern">
        <apex:pageBlock title="Pattern">
            <apex:form>
				<apex:selectList value="{!filterId}" size="1">
					<apex:actionSupport event="onchange" action="{!processRequests}"  rerender="PB1"/>
					<apex:selectOptions value="{!items}"/>
				</apex:selectList>
            </apex:form>
         </apex:pageBlock>         
		 
         <apex:pageBlock title="Pattern Summary" id="PB1">
            <apex:pageBlockTable value="{!categorylist}" var="cat">
               <apex:column headerValue="Name" value="{!cat.Name}"/>
               <apex:column headerValue="Category A" value="{!cat.CategoryA__c}"/>
               <apex:column headerValue="Category B" value="{!cat.CategoryB__c}"/>
            </apex:pageBlockTable>
         </apex:pageBlock>         
</apex:page>

Class Should be :-
public class GetPattern
{
    public string FilterId{get;set;}
    public List<Pattern__c> categorylist {get; set;}
    public GetPattern()
    {
       categorylist = new List<Pattern__c>();
    }
    
    public PageReference processRequests()
    {
        categorylist.clear();
        if(FilterId !='' && FilterId != null)
        {
            categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c where Name = :FilterId ];
        }
        else
        {
            categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c limit 1000 ];
        }
        return null;
    }

    public List<SelectOption> getItems() 
    {
        List<SelectOption> options = new List<SelectOption>();
        List<Pattern__c> categorylist = [select Name,CategoryA__c,CategoryB__C from Pattern__c limit 1000];
        Set<String> patterns = new Set<String>();
        
        for(Pattern__c pattern :categorylist)
        {
            if(patterns.contains(pattern.Name))
            {
                options.add(new SelectOption(pattern.Name,pattern.Name ));
                patterns.add(pattern.Name);
            }        
        }    
        return options;
    }

}


Test Class Should be :-
 
@isTest 
public class GetPatternTest 
{
	static testMethod void GetPatternT() 
	{
		List<Pattern__c> lstPattern = new List<Pattern__c>();
		Pattern__c p1 = new Pattern__c();
		p1.Name = 'Pattern A';
		P1.CategoryA__c ='Code';
		P1.CategoryB__c ='ItemCode';
		lstPattern.add(p1);

		Pattern__c p2 = new Pattern__c();
		p2.Name = 'Pattern B';
		P2.CategoryA__c ='Code';
		P2.CategoryB__c ='ItemCode';
		lstPattern.add(p2);

		insert lstPattern;

		Test.StartTest(); 
			GetPattern contrl = new GetPattern();
			List<SelectOption> lst = contrl.getItems();
			contrl.FilterId ='Pattern A';
			contrl.processRequests();
		Test.StopTest();
		
	}
 
}

You can check Test class best pratice on below  post :-
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
http://amitsalesforce.blogspot.in/2015/06/salesforce-testing-best-practice.html

Apex best Pratice at below post:-
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Please mark this as solution if this will help you. So if some one has same issue this post can help others.

Thanks
Amit Chaudhary
 
This was selected as the best answer
Rick RollRick Roll
Thx everyone for spending some time with my post...