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
MattLMattL 

Returning Valid Picklist Values

I'm developing an S-control where one of the fields is a picklist value. Ideally, I wouldn't need to hard-code the values in. Going through the Sforce Explorer, I can navigate under the field name down to "type - picklist" and then "Picklist Values", but I can't figure out how to structure my SOQL query to gain access to this data. Can anyone advise if it's even possible, or will I need to resort to hard-coding it in?  
Greg HGreg H
You need to use a describe call to get the picklist values.  Don't hard code the values becuase
you'll cause issues down the road when you update those values in the UI but not in the sControl.
Code:
//returns an array of picklist values for the passed table and field
function getPickVals(table,field) {
 var returnArray = new Array(); //return array holding all values
 var oTable = sforce.connection.describeSObject(table); //connect to table
 var oFields = oTable.fields; //look at fields on table
 for (var a=0; a<oFields.length; a++) { //loop through all the fields
  if (oFields[a].name==field) { //if the field is the requested one
   var oPicklistVals = oFields[a].picklistValues; //look at all the picklist values for field
   for (var b=0; b<oPicklistVals.length; b++) { //loop through all picklist values
    returnArray.push(oPicklistVals[b].value); //push all picklist options to return array
   }
   break; //we found field and picklist values so exit loop
  }
 }
 return returnArray; //send back data
}

 
I use this code all the time and it should help out tremendously.
-greg
MattLMattL
Thanks Greg. That worked flawlessly.