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
ira42ira42 

How to query Picklist values?

Since I can't use "DISTINCT" in a SOQL query,  I'm trying instead to return the unique values of a picklist,  using the PHP toolkit.

In the Products2 table,  I am trying to query the distinct values from the Products2.Family picklist.  I am using a query along the lines of:

Code:
$fam_query = "Select Family FROM Product2";
$fam_response = $mySforceConnection->query(($fam_query));

...

foreach ($fam_response->records as $fam_record) {
echo "<h3>".$fam_record->Family."</h3><br>";
}

Rather than returning the unique values from the picklist,  it's returning multiple family names (I guess for each product in the Products2 table?? Not sure).  So instead of:

Code:
Product Family One
Product Family Two
Product Family Three
Product Family Four
...

 I am getting:
Code:
Product Family One
Product Family One
Product Family One
Product Family Two
Product Family Two
Product Family Two
Product Family Two
Product Family Two
Product Family Three
...

I am stumped... I really just want to return the families as headers and then display the products (assets) under each family header.

Any ideas?

 

Best Answer chosen by Admin (Salesforce Developers) 
DeanFDeanF
If you are still searching for a solution for this, here's some Flex code that will return a collection of Type picklist values for the Opportunity object.  You can try to adapt this to PHP and the Product2 object...

        private function GetOppTypes():void
        {
            apex.describeSObjects(["Opportunity"],
                  new AsyncResponder(GetOppTypes_CB, genericFault));
        }   

        private function GetOppTypes_CB(result:Object):void
        {
              arrOppTypes = result[0].fields['Type'].picklistValues as ArrayCollection;
        }

Hope this helps!

-Dean

All Answers

SuperfellSuperfell
you can get the set of defined picklist values via the describeSObject call.
ira42ira42
Thanks for the reply SimonF.  I missed the 'describeSObject'  listing in the Toolkit samples.

I am very new to SF,  and had a look through the API docs,  and am a little confused about the syntax for PHP (no PHP examples anywhere in there...).  Also,  the docs say that 'describeSObject' is now superceded by 'describeSObjects',  and again,  there aren't any PHP examples, just Java and C#.

Could anyone please provide an example of how to return just the values for the Products2.Family picklist in PHP (I'd rather not return the entire Product2 sObject first)?  I'd really appreciate it.

- Ira
DeanFDeanF
If you are still searching for a solution for this, here's some Flex code that will return a collection of Type picklist values for the Opportunity object.  You can try to adapt this to PHP and the Product2 object...

        private function GetOppTypes():void
        {
            apex.describeSObjects(["Opportunity"],
                  new AsyncResponder(GetOppTypes_CB, genericFault));
        }   

        private function GetOppTypes_CB(result:Object):void
        {
              arrOppTypes = result[0].fields['Type'].picklistValues as ArrayCollection;
        }

Hope this helps!

-Dean
This was selected as the best answer
ZCJZCJ

If anyone has a update to this article, regarding a query in PHP for specifying a picklist value in the WHERE clause, please post it!

ZCJZCJ

I did find a post in he forums on this topic.  It should be more widely disseminated.  

 

You can query a picklist just like a string, if you only want one value.  for two values  "... WHERE Field_Name__c includes ('val1','val2') LIMIT 10"  or  for multi-select: "... WHERE Field_Name__c = 'val1;val2' LIMIT 10" 

 

one side reference:

http://boards.developerforce.com/t5/Apex-Code-Development/Selecting-a-picklist-value-programmatically/m-p/214229

 

 

 

 

stepanstepan

for the other users of Force.com Flex (flexforforce), this seems the simplest way in Flex:

BaseApplication.metadataCache.getField("type", "field");

Simple, but AFAIK undocumented.