1

Within one of my Salesforce object, I have cascading picklists where the selection of one picklist displays values for the next level picklist. So I have three dependent picklists.

What I would like to do within my C# application using the REST API approach is to get back the dependent values based on the object field name and value I pass.

At the moment, my method to do this looks like the following:

public static async Task<List<ObjectFieldPicklistValue>> GetPicklistFieldItems(string objectApiName, string pickListFieldName)
{
    string cacheKey = "{objectApiName}|{pickListFieldName}";

    List<ObjectFieldPicklistValue> pickListValues = CacheEngine.Get<List<ObjectFieldPicklistValue>>(cacheKey);

    if (pickListValues == null)
    {
        Authentication salesforceAuth = await AuthenticationResponse.GetAccessToken();

        HttpClient queryClient = new HttpClient();

        string apiUrl = $"{SalesforceConfig.PlatformUrl}/services/data/v37.0/sobjects/{objectApiName}/describe";

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, apiUrl);
        request.Headers.Add("Authorization", $"Bearer {salesforceAuth.AccessToken}");
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = await queryClient.SendAsync(request);

        string outputJson = await response.Content.ReadAsStringAsync();

        if (!string.IsNullOrEmpty(outputJson))
        {
            // Get all the fields information from the object.
            ObjectFieldInfo objectField = JsonConvert.DeserializeObject<ObjectFieldInfo>(outputJson);

            // Filter the fields to get the required picklist.
            ObjectField pickListField = objectField.Fields.FirstOrDefault(of => of.Name == pickListFieldName && of.Type == "picklist");

            List<ObjectFieldPicklistValue> picklistItems = pickListField?.PicklistValues.ToList();

            #region Set cache

            pickListValues = picklistItems;

            // Add collection of pick list items to cache.
            CacheEngine.Add(picklistItems, cacheKey, 15);

            #endregion
        }
    }

    return pickListValues;
}

I am getting back the items of a picklist based on the object and field name. From my research online, I can see in the REST request that some of my picklist values have a "validFor" field. But don't exactly understand how to decipher this to get the dependent values.

1 Answer 1

0

There is a good summary of using validFor in Get lists of dependent picklist options in Apex.

In short, the validFor field is a Base64 encoded string where, when read from left to right, each encoded bit indicates if the dependent value is valid for each controlling value.

See the explanation and sample code in About Dependent Picklists.

There is additional Apex sample code in Salesforce: Acquiring Dependent Picklists in Apex

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .