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 

Dynamic SOQL and StandardSetController

I'm attempting to created a VF page that takes a zip (or series of zips) and create a paginated list of related Accounts. I'm having issues retrieving the data, namely receiving the Attempt to de-reference a null object error.

 

Here's my VF:

 

<apex:form >
	<apex:pageBlock title="Search by Zip">
		<apex:inputText value="{!zips}" />
		<apex:commandButton value="Search" action="{!find}" />
	</apex:pageBlock>

	<apex:pageBlock title="Nearby Company/Branch" rendered="{!zips != null}">
		<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="{!zips != null}">
		<apex:commandLink action="{!first}" rendered="{!(pages > 1) && (hasPrevious)}">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) && (hasNext)}">Last</apex:commandLink>
	</apex:panelGrid>
</apex:form>

 

Here's my controller class:

 

 

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

		zList = zips.split(',');

		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 null;
	
	}
	
	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 void cancel() { accts.cancel(); }
		
	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;
		
	}

}

 

 

Can someone please help? Thanks

 

Adriel

Pradeep_NavatarPradeep_Navatar

It seems that there is something wrong with your controller code. Find method returns null in your code, so that getaccounts() does not show any records so it get an error Attempt a deference.

 

Try out the sample code given below :

 

           public with sharing class zipMap

           {

            public String zips { get; set; }

           public List<String> zList { get; set; }

           public ApexPages.StandardSetController accts

           {

                get {accts = new ApexPages.StandardSetController

                  (

                Database.getQueryLocator([SELECT Id, Name, BillingStreet, BillingCity, BillingState, BillingPostalCode FROM Account WHERE BillingPostalCode ='283203']));

                accts.setPageSize(2);

                return accts;

          }set;}

          public PageReference find(){return null;}

           public List<Account> getAccounts() {return (List<Account>) accts.getRecords();}

          }

 

Hope this helps.

aamDevaamDev

Thanks for the response, you'd be amazed at how little input I've received on this issue.

 

With that said, I've already tried that, and it works fine. The problem is, I don't want to hard code the zip in query, it needs to come from the user.

 

Thanks again, please let me know if you have any further suggestions.

 

Adriel

aballardaballard

zips needs to be initialized to something... <apex:inputField> will try to display the initial value.  Initialize it to an empty string. 

Amita_AccAmita_Acc

I have a dynamic query :

 

queryUnacceptedLeads = 'SELECT Name, CreatedDate ,Company,LeadSource, Lead_Source_Details_del__c,Owner.Name, Days_Open_Unaccepted_leads__c FROM Lead WHERE Sales_Accepted__c = FALSE AND Status IN (\'Contacting\',\'New\',\'Qualified\') AND OwnerId IN '+getCommaSeperatedStringList(UserIdsSet) + 'ORDER BY Days_Open_Unaccepted_leads__c DESC LIMIT 200 ' ;

 

setConUnacceptedLeads = new ApexPages.StandardSetController(Database.query(
queryUnacceptedLeads));

 

//setConUnacceptedLeads = new ApexPages.StandardSetController(Database.getQueryLocator(
queryUnacceptedLeads));

 

and I am getting System.QueryException: unexpected token: ORDER

 

Does anyone know if Dynamic query can be executed in this senario ??

 

 

 

 

AAMAAM

Amita

 

Assuming your comma separated string list doesn't add a space to the end, you need to add space before ORDER:

 

' ORDER BY...'