Newer Version Available
describeSObject()
Describes metadata (field list and object properties) for the specified object.
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.
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public void describeSObjectSample() {
18 try {
19 // Make the describe call
20 DescribeSObjectResult describeSObjectResult =
21 connection.describeSObject("Account");
22
23 // Get sObject metadata
24 if (describeSObjectResult != null) {
25 System.out.println("sObject name: " +
26 describeSObjectResult.getName());
27 if (describeSObjectResult.isCreateable())
28 System.out.println("Createable");
29
30 // Get the fields
31 Field[] fields = describeSObjectResult.getFields();
32 System.out.println("Has " + fields.length + " fields");
33
34 // Iterate through each field and gets its properties
35 for (int i = 0; i < fields.length; i++) {
36 Field field = fields[i];
37 System.out.println("Field name: " + field.getName());
38 System.out.println("Field label: " + field.getLabel());
39
40 // If this is a picklist field, show the picklist values
41 if (field.getType().equals(FieldType.picklist)) {
42 PicklistEntry[] picklistValues =
43 field.getPicklistValues();
44 if (picklistValues != null) {
45 System.out.println("Picklist values: ");
46 for (int j = 0; j < picklistValues.length; j++) {
47 if (picklistValues[j].getLabel() != null) {
48 System.out.println("\tItem: " +
49 picklistValues[j].getLabel()
50 );
51 }
52 }
53 }
54 }
55
56 // If a reference field, show what it references
57 if (field.getType().equals(FieldType.reference)) {
58 System.out.println("Field references the " +
59 "following objects:");
60 String[] referenceTos = field.getReferenceTo();
61 for (int j = 0; j < referenceTos.length; j++) {
62 System.out.println("\t" + referenceTos[j]);
63 }
64 }
65 }
66 }
67 } catch (ConnectionException ce) {
68 ce.printStackTrace();
69 }
70}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.
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public void describeSObjectSample() {
18 try {
19 // Make the describe call
20 DescribeSObjectResult describeSObjectResult =
21 binding.describeSObject("Account");
22
23 // Get sObject metadata
24 if (describeSObjectResult != null) {
25 Console.WriteLine("sObject name: " +
26 describeSObjectResult.name);
27 if (describeSObjectResult.createable)
28 Console.WriteLine("Createable");
29
30 // Get the fields
31 Field[] fields = describeSObjectResult.fields;
32 Console.WriteLine("Has " + fields.Length + " fields");
33
34 // Iterate through each field and gets its properties
35 for (int i = 0; i < fields.Length; i++) {
36 Field field = fields[i];
37 Console.WriteLine("Field name: " + field.name);
38 Console.WriteLine("Field label: " + field.label);
39
40 // If this is a picklist field, show the picklist values
41 if (field.type.Equals(fieldType.picklist)) {
42 PicklistEntry[] picklistValues =
43 field.picklistValues;
44 if (picklistValues != null) {
45 Console.WriteLine("Picklist values: ");
46 for (int j = 0; j < picklistValues.Length; j++) {
47 if (picklistValues[j].label != null) {
48 Console.WriteLine("\tItem: " +
49 picklistValues[j].label);
50 }
51 }
52 }
53 }
54
55 // If a reference field, show what it references
56 if (field.type.Equals(fieldType.reference)) {
57 Console.WriteLine("Field references the " +
58 "following objects:");
59 String[] referenceTos = field.referenceTo;
60 for (int j = 0; j < referenceTos.Length; j++) {
61 Console.WriteLine("\t" + referenceTos[j]);
62 }
63 }
64 }
65 }
66 } catch (SoapException e) {
67 Console.WriteLine("An unexpected error has occurred: " +
68 e.Message + "\n" + e.StackTrace);
69 }
70}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. |