I'm attempting to implement a client-side application in C# (Using the mono framework) and the Salesforce Partner SOAP API.

I can successfully login, however am slightly confused on how to actually retrieve field values from a query() call.

In Java, I was able to do this (for example):

for( SOBject record : queryResult.getRecords() ){
    String name = (String)record.getField("Name");

Whereas the dotNET library does not appear to have any analogue to the 'getField' call.

The official documentation refers to using the record.Any array to retrieve field values by index. i.e.

foreach( var record in queryResult.Records ){
    string name = record.Any[0].InnerText;

however this is not optimal - with a large number of fields I really need to be able to fetch fields by the real field name.

Am I missing something here? Or can I only access field values via a 0-indexed array in the dotNET implementation of the Soap API?


You have to access them via the array, of course if that really bugs you, you can do one pass over the array to put the values in a dictionary, and then lookup values by name.

| improve this answer | |
  • Thanks. I thought this might be the case, but was confused by why by 'Any' array was often coming back as Null. Turns out it was a bug in the Mono Framework. Ended up solving by consuming the WSDL with Visual Studio, then plugging that into my MonoTouch project. – James Davies Aug 24 '12 at 7:13

You can try with the describeSObjects() metadata API call.

For your reference :

public void describeSObjectsSample()

{ try { // Call describeSObjectResults and pass it an array with

// the names of the objects to describe. 

DescribeSObjectResult[] describeSObjectResults = 
                    new string[] { "account", "contact", "lead" });

// Iterate through the list of describe sObject results 

foreach (DescribeSObjectResult describeSObjectResult in describeSObjectResults)
    // Get the name of the sObject 

    String objectName = describeSObjectResult.name;
    Console.WriteLine("sObject name: " + objectName);

    // For each described sObject, get the fields 

    Field[] fields = describeSObjectResult.fields;

    // Get some other properties 

    if (describeSObjectResult.activateable) Console.WriteLine("\tActivateable");

    // Iterate through the fields to get properties for each field 

    foreach (Field field in fields)
        Console.WriteLine("\tField: " + field.name);
        Console.WriteLine("\t\tLabel: " + field.label);
        if (field.custom) 
            Console.WriteLine("\t\tThis is a custom field.");
        Console.WriteLine("\t\tType: " + field.type);
        if (field.length > 0)
            Console.WriteLine("\t\tLength: " + field.length);
        if (field.precision > 0)
            Console.WriteLine("\t\tPrecision: " + field.precision);

        // Determine whether this is a picklist field 

        if (field.type == fieldType.picklist)
            // Determine whether there are picklist values 

            PicklistEntry[] picklistValues = field.picklistValues;
            if (picklistValues != null && picklistValues[0] != null)
                Console.WriteLine("\t\tPicklist values = ");
                for (int j = 0; j < picklistValues.Length; j++)
                    Console.WriteLine("\t\t\tItem: " + picklistValues[j] 

        // Determine whether this is a reference field 

        if (field.type == fieldType.reference)
            // Determine whether this field refers to another object 

            string[] referenceTos = field.referenceTo;
            if (referenceTos != null && referenceTos[0] != null)
                Console.WriteLine("\t\tField references the following objects:");
                for (int j = 0; j < referenceTos.Length; j++)
                    Console.WriteLine("\t\t\t" + referenceTos[j] 
catch (SoapException e)
    Console.WriteLine("An unexpected error has occurred: " + e.Message
        + "\n" + e.StackTrace);


| 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.