+ Start a Discussion
anan 

Query the values above or below specific value in a picklist

Is it possible to query a records based on a picklist value so that I query all objects above or below the specific picklist value. For example if I have pickilist Example__c in my custom object CustomObject__c

 

Example__c contains values contains values

Example1

Example2

Example3

Example4

 

Now I would like to make a SOQL query to fetch all the records who Example__c field set to value above 'Example3'. Is this possible? Could I use getPicklistValues() method to define which values are above the specific value? In other words does the getPicklistValues() return the values in the correct order?

Best Answer chosen by Admin (Salesforce Developers) 
ErikssonEriksson

Hi,

I don't think the answer is what you want. Yes, you can use getPicklistValues() method to define which values are above the specific value, and also this method can return the values in the correct order. So you can define your script something like this:

 

List<String> examples = new List<String>();
Schema.DescribeFieldResult result = CustomObject__c.Example__c.getDescribe();
for(Schema.PicklistEntry entry : result.getPicklistValues())
{
	if(entry.getValue() == 'Example3')
	{
		break;
	}
	else
	{
		examples.add(entry.getValue());
	}
}
List<CustomObject__c> s = [select Id from CustomObject__c where Example__c in :examples];

 

 

All Answers

Ispita_NavatarIspita_Navatar

The answer to the first part of your question is yes it is  possible to query a records based on a picklist value by using includes keyword.

 

Select id, name from CustomObject__c where Example__c includes (Example3)

 

Also if you wish to exclude the value Example3 you can style your query as under:-

Select id, name from CustomObject__c where Example__c includes (Example1;Example2;Example4)

getPicklistValues() returns all the picklist values of a field of type picklist or multipicklist.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

 

ErikssonEriksson

Hi,

I don't think the answer is what you want. Yes, you can use getPicklistValues() method to define which values are above the specific value, and also this method can return the values in the correct order. So you can define your script something like this:

 

List<String> examples = new List<String>();
Schema.DescribeFieldResult result = CustomObject__c.Example__c.getDescribe();
for(Schema.PicklistEntry entry : result.getPicklistValues())
{
	if(entry.getValue() == 'Example3')
	{
		break;
	}
	else
	{
		examples.add(entry.getValue());
	}
}
List<CustomObject__c> s = [select Id from CustomObject__c where Example__c in :examples];

 

 

This was selected as the best answer