Find your content:

Search form

You are here

Retrieving field values from Partner API in C#

 
Share

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?


Attribution to: James Davies

Possible Suggestion/Solution #1

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.


Attribution to: superfell

Possible Suggestion/Solution #2

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 = 
                    binding.describeSObjects(
                    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] 
.label);
                }
            }
        }

        // 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);
}

}


Attribution to: Roshan
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/863

My Block Status

My Block Content