4

I need to get the label name from custom object. I'm using the C# .Net SalesForce API and try to use the DescribeAsync call on my Objects.

It seems to be working, but I don't know how to parse the return Object. Where can I find the definition of the equivalent Apex DescribeSObjectResult?

Do somebody have an experience/sample using calls in C# like

var result = await ForceClient.DescribeAsync<T>("Account");
1

It looks like you are using the Force.com Toolkit for .NET. It is based the the Salesforce REST API rather than the SOAP API.

You can see the underlying method in GitHub for public Task<T> DescribeAsync<T>(string objectName). It is basically returning the sobjects/SObjectName/describe/ resource. It will be a json eqaivalent to the SOAP DescribeSObjectResult.

There is an example of the describe JSON output in Get Field and Other Metadata for an Object.

One option to make it more workable in C# would be to use an online json2csharp tool to create POCO objects that you can deserialize the JSON to How to Deserialize JSON data?.

Since you will have also pulled in the Newtonsoft.Json package you can also use it to deserialize the JSON - Deserialize an Object.

| improve this answer | |
  • Thank you for your answer. I'll explore all the solutions described in your answer. I've tried the DescribeAsync Method, but was not able to find out how to use the result. Using Newtonsoft.Json, i'll try this way first. – Olivier Crivelli Jul 14 '16 at 7:40
3

Thank you Daniel, I've followed your recommandations and it works. What I needed in my C# .Net application was to get the Labels and Field names for custom objects, but also the content of the Picklists of these custom objects. Please find here after the code that I'm pleased to share, and which could perhaps help other developpers.

Step 1 - Create Description Object

public class picklistValues
{
    public bool active { get; set; }
    public bool defaultValue { get; set; }
    public string label { get; set; }
    public string validFor { get; set; }
    public string value { get; set; }
}
public class fields
{
    public string label { get; set; }
    public string name { get; set; }
    public IList<picklistValues> pickListValues { get; set; }
}
public class ObjectDescription
{
    public IList<fields> fields { get; set; }
}

Step 2 - Get JSON Object Description

The url parameter is like /services/data/[APIVerion]/sobjects/[ObjectName]/describe/

    public async Task<string> DescribeObject(string url)
    {
        string descriptionContent = string.Empty;
        HttpClient queryClient = new HttpClient();
        string restQuery = serviceUrl + url;

        // Create the request
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, restQuery);

        // Add Token to the header
        request.Headers.Add("Authorization", "Bearer " + oauthToken);

        //return JSON to the caller
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        // Call the endpoint async
        try
        {
            HttpResponseMessage response = await queryClient.SendAsync(request);

            descriptionContent = await response.Content.ReadAsStringAsync();
        }
        catch (Exception e)
        {
            descriptionContent = e.Message;
        }

        queryClient.Dispose();

        return descriptionContent;
    }

Step 3 - Convert the JSON result

The myDescribeAsync method prepares the connection (with ConsumerKey, ConsumerSecret, etc.), prepares the url (with the right API Version and the Object Name), initializes the connection and call asynchronously the DescribeObject (url) method.

// Get the Json Description 
string objdescr = await myDescribeAsync("Account");

// Deserialize the Json result the appropriate structure 
ObjectDescription objDescr = JsonConvert.DeserializeObject<ObjectDescription>(objdescr);

Hope this could help. Olivier Crivelli

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.