Newer Version Available

This content describes an older version of this product. View Latest

describeSObject()

Describes metadata (field list and object properties) for the specified object.

describeSObjects() supersedes describeSObject(). Use describeSObjects() instead of describeSObject().

Note

Syntax

1DescribeSObjectResult = connection.describeSObject(string sObjectType);

Usage

Use describeSObject() to obtain metadata for a given object. You can first call describeGlobal() to retrieve a list of all objects for your organization, then iterate through the list and use describeSObject() to obtain metadata about individual objects.

Your client application must be logged in with sufficient access rights to retrieve metadata about your organization’s data. For more information, see Factors that Affect Data Access.

Sample Code—Java

This sample calls describeSObject() to perform describes on the Account sObject. It retrieves some properties of the sObject describe result, such as the sObject name, label, and fields. It then iterates through the fields and gets the field properties. For picklist fields, it gets the picklist values and for reference fields, it gets the referenced object names. The sample writes the retrieved sObject and field properties to the console.

1public void describeSObjectSample() {
2    try {
3        // Make the describe call
4        DescribeSObjectResult describeSObjectResult = 
5            connection.describeSObject("Account");
6        
7        // Get sObject metadata 
8        if (describeSObjectResult != null) {
9          System.out.println("sObject name: " + 
10                  describeSObjectResult.getName());
11        if (describeSObjectResult.isCreateable()) 
12            System.out.println("Createable");              
13
14        // Get the fields
15        Field[] fields = describeSObjectResult.getFields();
16        System.out.println("Has " + fields.length + " fields");
17        
18        // Iterate through each field and gets its properties 
19        for (int i = 0; i < fields.length; i++) {
20          Field field = fields[i];
21          System.out.println("Field name: " + field.getName());
22          System.out.println("Field label: " + field.getLabel());
23          
24          // If this is a picklist field, show the picklist values
25          if (field.getType().equals(FieldType.picklist)) {
26              PicklistEntry[] picklistValues = 
27                  field.getPicklistValues();
28              if (picklistValues != null) {
29                System.out.println("Picklist values: ");
30                for (int j = 0; j < picklistValues.length; j++) {
31                  if (picklistValues[j].getLabel() != null) {
32                    System.out.println("\tItem: " + 
33                        picklistValues[j].getLabel()
34                    );
35                  }
36                }
37              }
38          }
39
40          // If a reference field, show what it references
41          if (field.getType().equals(FieldType.reference)) {
42              System.out.println("Field references the " +
43                      "following objects:");
44              String[] referenceTos = field.getReferenceTo();              
45              for (int j = 0; j < referenceTos.length; j++) {
46                  System.out.println("\t" + referenceTos[j]);
47              }
48          }	        
49	    }
50	  }
51	} catch (ConnectionException ce) {
52	    ce.printStackTrace();
53    }
54}

Sample Code—C#

This sample calls describeSObject() to perform describes on the Account sObject. It retrieves some properties of the sObject describe result, such as the sObject name, label, and fields. It then iterates through the fields and gets the field properties. For picklist fields, it gets the picklist values and for reference fields, it gets the referenced object names. The sample writes the retrieved sObject and field properties to the console.

1public void describeSObjectSample() {
2    try {
3        // Make the describe call
4        DescribeSObjectResult describeSObjectResult = 
5            binding.describeSObject("Account");
6            
7        // Get sObject metadata 
8        if (describeSObjectResult != null) {
9            Console.WriteLine("sObject name: " + 
10                    describeSObjectResult.name);
11        if (describeSObjectResult.createable) 
12            Console.WriteLine("Createable");              
13
14        // Get the fields
15        Field[] fields = describeSObjectResult.fields;
16        Console.WriteLine("Has " + fields.Length + " fields");
17            
18        // Iterate through each field and gets its properties 
19        for (int i = 0; i < fields.Length; i++) {
20            Field field = fields[i];
21            Console.WriteLine("Field name: " + field.name);
22            Console.WriteLine("Field label: " + field.label);
23              
24            // If this is a picklist field, show the picklist values
25            if (field.type.Equals(fieldType.picklist)) {
26                PicklistEntry[] picklistValues = 
27                    field.picklistValues;
28                if (picklistValues != null) {
29                    Console.WriteLine("Picklist values: ");
30                    for (int j = 0; j < picklistValues.Length; j++) {
31                        if (picklistValues[j].label != null) {
32                        Console.WriteLine("\tItem: " + 
33                            picklistValues[j].label);
34                        }
35                    }
36                }
37            }
38
39            // If a reference field, show what it references
40            if (field.type.Equals(fieldType.reference)) {
41                Console.WriteLine("Field references the " +
42                      "following objects:");
43                String[] referenceTos = field.referenceTo;              
44                for (int j = 0; j < referenceTos.Length; j++) {
45                    Console.WriteLine("\t" + referenceTos[j]);
46                }
47            }            
48        }
49        }
50    } catch (SoapException e) {
51        Console.WriteLine("An unexpected error has occurred: " +
52            e.Message + "\n" + e.StackTrace);
53    }
54}

Arguments

Name Type Description
sObjectType string Object. The specified value must be a valid object for your organization. For a complete list of objects, see Standard Objects.