• Surinder Bhomra
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
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.
I have been looking into getting information, such as Contacts from a Developer Salesforce account I have created. I downloaded the Sample  .NET Console application from the Force.com Toolkit for .NET Github repository. However, I am getting an "Authentication Error" response when trying to authenticate using the OAuth key and secret ID's.

I haven't modfied the code from the Github sample in any way, so I am at a loss to why this error is being returned.

From reading multiple posts online, this is what I have done to try and fix the issue:
  • Created a new Connected App that has:
    • All users may self-authorize
    • Relax IP Restrictions
    • Application permission set to "Full Access".
    • Generated Consumer Key and Secret.
  • Generated a Security Token from my logged in account (that has full System Admin access)
  • Ensure the user role has API Enabled.
I am at a loss to what else I need to do in order to get API access.

Any help would be appreciated.
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.
I have been looking into getting information, such as Contacts from a Developer Salesforce account I have created. I downloaded the Sample  .NET Console application from the Force.com Toolkit for .NET Github repository. However, I am getting an "Authentication Error" response when trying to authenticate using the OAuth key and secret ID's.

I haven't modfied the code from the Github sample in any way, so I am at a loss to why this error is being returned.

From reading multiple posts online, this is what I have done to try and fix the issue:
  • Created a new Connected App that has:
    • All users may self-authorize
    • Relax IP Restrictions
    • Application permission set to "Full Access".
    • Generated Consumer Key and Secret.
  • Generated a Security Token from my logged in account (that has full System Admin access)
  • Ensure the user role has API Enabled.
I am at a loss to what else I need to do in order to get API access.

Any help would be appreciated.