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
BobRoyAceBobRoyAce 

Querying for available Picklist values for a particular field for a particular object

Let's say that I want to grab a list of all of the acceptable picklist values that can be selected for a particular field of a particular object (e.g. Account.Type). What VB.NET code could I use to accomplish something like this? Ideally, what I'd like is a function like follows:

 

Public Function GetPicklistEntriesForObjectForField(ByVal sObjectName as string, ByVal sFieldName as string) as string()

 

End Function

 

How could I do this for standard objects (e.g. Account)? How about for custom objects (e.g. Owner__c)?

SuperfellSuperfell

see the describeSObject call, it has all that information in its response.

BobRoyAceBobRoyAce

I created a function that allows for passing in an Object Name and a Field Name. Then, it returns a list of strings containing the picklist values that are defined for the field, if there are any. Here it is for anyone who's interested:

 

--- CODE BEGNS ---

 

  Public Function fnGetPicklistEntriesForObjectForField( _
   ByVal sObjectName As String, _
   ByVal sFieldName As String) As List(Of String)
    Dim oList As New List(Of String)
    If LoginIfNeedTo() = False Then
      Return oList
    End If
    Try
      ' Call the describeSObject method passing the name of the object to get 
      ' metadata on.
      Dim oDescribeSObjectResult As DescribeSObjectResult = _SalsesforceService.describeSObject(sObjectName)
      If (oDescribeSObjectResult IsNot Nothing) Then
        Dim oFields() As Field = oDescribeSObjectResult.fields
        ' Loop through each field until we find the one we're after, if we do.
        If (oFields IsNot Nothing) Then
          For i As Integer = 0 To oFields.GetUpperBound(0)
            Dim oField As Field = oFields(i)
            ' Check to see if we've got the field we're after.
            If (oField.name.Equals(sFieldName)) Then
              Dim oPicklistValues() As PicklistEntry = oField.picklistValues
              ' Check to see if the field has picklist values.
              If (oPicklistValues IsNot Nothing) Then
                ' Loop through the array of picklist values, adding each one
                ' to the list.
                For j As Integer = 0 To oPicklistValues.GetUpperBound(0)
                  oList.Add(oPicklistValues(j).value)
                Next
              End If
            End If
          Next
        End If
      End If
      Return oList
    Catch ex As Exception
      ShowGRPExceptionMessage("SalesforceTesterForm.fnGetPicklistEntriesForObjectForField: Error occurred for " & sObjectName & "." & sFieldName & ".", ex)
    End Try
  End Function

 

--- CODE ENDS ---