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
tsalbtsalb 

Dynamic queries from booleans - Advice?

I have an existing search (contacts) page, but wanted to add booleans to filter out results. So, if boolean A is ticked, i only want accounts with a picklist value of "A" and if B is ticked, only accounts with picklist values of B. If both ticked, both sets.

 

With the help of a peer, I've gotten a little further on this - but I'm wondering why this throws the following error:

 

caused by: System.NullPointerException: Attempt to de-reference a null object

External entry point

Class.ProposalExtension.vendorSearch: line 325, column 1

 

Visualforce page snippet: Extensions = ProposalExtension

 

            <apex:pageBlockSection title="Vendor Panel Options">
            	<apex:pageBlockSectionItem >
            		<apex:outputLabel for="civasPanel" value="Enable Shared Panel"/>
            		<apex:inputCheckbox id="civasPanel" value="{!includeA}"/>
            	</apex:pageBlockSectionItem>
            	<apex:pageBlockSectionItem >
            		<apex:outputLabel for="civasPanel2" value="Enable Private Panel"/>
            		<apex:inputCheckbox id="civasPanel2" value="{!includeB}"/>
            	</apex:pageBlockSectionItem>
            </apex:pageBlockSection>

 

ProposalExtension line 325 highlighted below

 

public with sharing class ProposalExtension {

    public Boolean includeA {get; set;} //Experimenting with vPanel toggle
    public Boolean includeB {get; set;}
    
    private final Proposal__c p;
    
    public ProposalExtension(ApexPages.StandardController pCon) {
        this.p = (Proposal__c)pCon.getRecord();
    }
    
    //Used to create lookup fields for Vendor Search
    Contact con;
    public Contact getCon() {
      if(con == null) con = new Contact();
      con.RecordTypeId = '012A00000007dJ4'; //Set the Record Type to "Vendor"   
      return con;
    }
    
    //Create a Wrapper/Container class for Vendors to be used in implementing a checkbox list
    public class checkedVendor{
    	public Contact ven {get;set;}
    	public Boolean checked {get;set;}
    	
    	//Constructor method for checkedVendors class
    	public checkedVendor(Contact v){
    		ven = v;
    		checked = false;
    	}
    }

	//Create a list of new wrapper class/container object checkedVendors 
	public List<checkedVendor> vendorList {get;set;}
	
	//Create a PageReference to execute the Vendor Search
	public PageReference vendorSearch() {
		vendorList = new List<checkedVendor>();
		
		List<String> picklistVals = new List<String>();
		if(includeA) {
			picklistVals.add('Shared');
		}
		if(includeB) {
			picklistVals.add('Private');
		}
				
		if(radius == 0) {
			radius = 1;
		}
		//Creates an instance of Vendor Search
		VendorSearch vs = new VendorSearch();
		
		try {			
			//Query for Vendors within the selected Radius, matching Type, and Primary Specialty
//LINE 325	for(Contact v : vs.searchVendors(con.Primary_County__c, radius, con.Type__c, con.Primary_Specialty__c, picklistVals)){
				//Add a checkbox to each Vendor that is returned in the search
				vendorList.add(new checkedVendor(v));
        	}
		}
		catch(QueryException q) {
		}
		return null;
	}

}

 VendorSearch, i've highlighted the problem line.

 

public with sharing class VendorSearch {

    public Id countyId; //County to be used as starting point
    public Integer radius = 1; //Desired radius in miles from starting point
    public String vType; //Desired Vendor Type
    public String specialty; //Desired Vendor Specialty
    public String noSpecialty; //No Vendor Specialty place holder
    public List<String> picklistVals; //experimenting with panel pickval
    
    public VendorSearch() {
    }   
 	 	
    //Create a list of Contacts to hold available Vendors 
	public List<Contact> searchVendors(Id startingCounty, Integer searchRadius, String vendorType, String vendorSpecialty, List <String> pVals) {
		
		countyId = startingCounty;
    	radius = searchRadius;
    	vType = vendorType;
    	specialty = vendorSpecialty;
    	noSpecialty = vendorSpecialty;
    	pVals = picklistVals;
    	
    	if(noSpecialty == NULL){
    		noSpecialty = '%';
    	}
		
		List<Contact> vendors;
						
		try {						
			//Get the County Ids from the map and create Set for the search query
			Set<Id> radiusSet = radiusMap().keySet();  
			
			//Query for Vendors within the selected Radius, matching Type, and Primary Specialty
			vendors = (List<Contact>)[SELECT Id, Name, Account.Name, Account.Revos_Status__c, Designation__c, Phone, Email,
									 Primary_County__c, Type__c, Status__c, Status_Sort__c,
									 Overall_Rating__c, RecordTypeId, Minority_Status__c
			FROM Contact 
			WHERE Primary_County__c IN :radiusSet 
//PROBLEM	AND Account.Revos_Status__c IN :pVals  //Trying to filter out results from booleans on visualforce page
			AND Type__c INCLUDES (:vType) 
			AND (Primary_Specialty__c LIKE :noSpecialty OR Primary_Specialty__c LIKE :specialty)
			ORDER BY Status_Sort__c ASC, Overall_Rating__c DESC];
		}
		catch(QueryException q) {
		}	
		return vendors;
	}    

}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
tsalbtsalb

Found it.

 

Sigh.

 

vendorSearch:

pVals = picklistVals;

 needs to be:

picklistVals = pVals;

 

All Answers

tsalbtsalb

Updated after following some advice. Tried to refactor existing code with this psuedo-code.

 

Boolean includeA = getPanelA();
Boolean includeB = getPanelB();

List<String> picklistVals = new List<String>();

if (includeA) {
    picklistVals.add('A');
}

if (includeB) {
    picklistVals.add('B');
}

List<MyObjects__c> myObjs = [
    select Id
    from MyObjects__c
    where MyFeild__c in :picklistVals
];

 

tsalbtsalb

Anyone?

tsalbtsalb

Found it.

 

Sigh.

 

vendorSearch:

pVals = picklistVals;

 needs to be:

picklistVals = pVals;

 

This was selected as the best answer