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
aamDevaamDev 

Passing variable to StandardSetController through form

I need to pass a string from a form to my StandardSetController. So far what I'm doing is not working. I would appreciate any help. Thanks.

 

VF:

 

 

<apex:sectionHeader title="Zip Map" subtitle="Map Company/Branch by Zip" />
<apex:form >
	<apex:pageBlock title="Search by Zip">
		<apex:inputText value="{!zips}" />
		<apex:commandButton value="Search" action="{!find}" />
	</apex:pageBlock>
	
	<apex:pageBlock rendered="{!(zips != null) && (pages == 0)}">
		<apex:outputText value="Your search returned no values" />
	</apex:pageBlock>

	<apex:pageBlock title="Nearby Company/Branch" rendered="{!pages > 0}">
		<apex:pageBlockTable value="{!accounts}" var="acct" id="accountTable">
			<apex:column headerValue="Company/Branch">
				<apex:outputLink value="/{!acct.Id}" target="_top">{!acct.Name}</apex:outputLink>
			</apex:column>
			<apex:column headerValue="Address" colspan="2">
				<apex:outputText >{!acct.BillingStreet}, {!acct.BillingCity}, {!acct.BillingState}, {!acct.BillingPostalCode}</apex:outputText>
			</apex:column>
			<apex:column >
				<apex:outputLink value="http://maps.google.com/maps?f=d&source=s_d&saddr=&daddr={!acct.BillingStreet},{!acct.BillingCity},{!acct.BillingState},{!acct.BillingPostalCode}" target="_blank">Get Directions</apex:outputLink>
			</apex:column>
		</apex:pageBlockTable>
	</apex:pageBlock>

	<apex:panelGrid columns="4" style="margin-bottom: 25px;" rendered="{!pages > 0}">
		<apex:commandLink action="{!first}" rendered="{!pages > 1}">First</apex:commandLink>
		<apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandLink>
		<apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandLink>
		<apex:commandLink action="{!last}" rendered="{!pages > 1}">Last</apex:commandLink>
	</apex:panelGrid>
</apex:form>

 

 

Controller:

 

 

public with sharing class zipMap {
	
	public String zips { get; set; }
	
	public PageReference find() {
		
		return null;
	
	}
	
	public ApexPages.StandardSetController accts {
		
		get {
			
			if(accts == null) {
			
				//List<String> zList;
				
				//zList = zips.split(',');	
				
				accts = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, Name, BillingStreet, BillingCity, BillingState, BillingPostalCode FROM Account WHERE BillingPostalCode =: zips ORDER BY BillingPostalCode, Name]));
				
				accts.setPageSize(2);
				
			}
				
			return accts;
			
		}
		
		set;
	
	}
	
	public List<Account> getAccounts() {
		
		return (List<Account>) accts.getRecords();
		
	}
	
	public Boolean hasNext {
		
		get {
			
			return accts.getHasNext();
			
		}
		
		set;
		
	}
	
	public Boolean hasPrevious {
		
		get {
			
			return accts.getHasPrevious();
			
		}
		
		set;
		
	}
	
	public Integer pageNumber {
		
		get {
			
			return accts.getPageNumber();
			
		}
		
		set;
		
	}

	public Integer pages {
		
		get {
		
			pages = accts.getResultSize()/accts.getPageSize();
			
			return pages;
			
		}
		
		set;
		
	}
	
	public void first() {
		
		accts.first();
		
	}
	
	public void last() {
		
		accts.last();
		
	}
	
	public PageReference previous() {
		
		Integer p = accts.getPageNumber();
		
		if(hasPrevious) {
		
			accts.setPageNumber(p-1);
		
		}
		
		return null;
		
	}
	
	public PageReference next() {
		
		Integer p = accts.getPageNumber();
		
		if(hasNext) {
		
			accts.setPageNumber(p+1);
		
		}
		
		return null;
		
	}

	public void cancel() {
		
		accts.cancel();
		
	}
}

 

 

_Prasu__Prasu_

Could you please explain what exactly you are trying?

aamDevaamDev

I have a form where I'll take a zip from a user and then run a query through the StandardSetController that will return Accounts based on that zip.

 

Thanks.

Jeremy_nJeremy_n

I think you need to specify a rerender attribute on your Find button. Without it, it will refresh the whole page, which will reload the class constructor, and reset "zips" back to null.

 

Jeremy

sinsiterrulezsinsiterrulez

Hi,

As per your code the action method find has no query in it to get updated queried data.You are just returning null from the find method.

Query the DB in the method & then return null so as to get the updated values.

aamDevaamDev

Thanks for the responses, I've been busy with other projects.

 

How do I query that data within the find method and still use the StandardSetController? I probably should've asked that at the beginning, because I've been able to run this query outside of SSC, but need it for pagination.

 

Thanks.

aamDevaamDev

Here's my latest bit of non-working code:

 

 

public with sharing class zipMap {
	
	public String zips { get; set; }
	
	public List<String> zList { get; set; }
	
	public PageReference find() {

		zList = zips.split(',');

		return null;
	
	}
	
	public ApexPages.StandardSetController accts {
		
		get {
			
			if(accts == null) {
				
				String query = 'SELECT Id, Name, BillingStreet, BillingCity, BillingState, BillingPostalCode FROM Account WHERE BillingPostalCode =: ' + zList + ' ORDER BY BillingPostalCode, Name';
		
				accts = new ApexPages.StandardSetController(Database.getQueryLocator(query));
				accts.setPageSize(2);
			
			}
			
			return accts;
		
		}
		
		set;
	
	}
	
	public List<Account> getAccounts() {
		
		return (List<Account>) accts.getRecords();
		
	}
	
	public Boolean hasNext {
		
		get {
			
			return accts.getHasNext();
			
		}
		
		set;
		
	}
	
	public Boolean hasPrevious {
		
		get {
			
			return accts.getHasPrevious();
			
		}
		
		set;
		
	}
	
	public Integer pageNumber {
		
		get {
			
			return accts.getPageNumber();
			
		}
		
		set;
		
	}

	public Integer pages {
		
		get {
		
			pages = accts.getResultSize()/accts.getPageSize();
			
			return pages;
			
		}
		
		set;
		
	}
	
	public void first() {
		
		accts.first();
		
	}
	
	public void last() {
		
		accts.last();
		
	}
	
	public PageReference previous() {
		
		Integer p = accts.getPageNumber();
		
		if(hasPrevious) {
		
			accts.setPageNumber(p-1);
		
		}
		
		return null;
		
	}
	
	public PageReference next() {
		
		Integer p = accts.getPageNumber();
		
		if(hasNext) {
		
			accts.setPageNumber(p+1);
		
		}
		
		return null;
		
	}

	public void cancel() {
		
		accts.cancel();
		
	}
}

 

It's funny because I'm finally not receiving the dreaded "Attempt to de-reference a null object" but instead my pageblocktable has nothing in it.

 

Any ideas?  Thanks!