Newer Version Available

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

Sample create Call

The following Java and C# examples show how to use the create() call for the partner WSDL. Each example creates a contact record with several fields. It iterates through the results of the create call and checks whether the operation was successful or not. If the create operation was successful, it writes the ID of the contact created to the console. Otherwise, it iterates through the errors and writes details of each error to the console. In this case, the output of the example is the ID of the new contact.

To execute the sample method, you can use the corresponding Java or C# template class provided in Examples Using the Partner WSDL.

Java Example

1swfobject.registerObject("clippy.codeblock-0", "9");public String createSample() {
2    String result = null;
3    try {
4        // Create a new sObject of type Contact
5           // and fill out its fields.
6        SObject contact = new SObject();
7        contact.setType("Contact");
8        contact.setField("FirstName", "Otto");
9        contact.setField("LastName", "Jespersen");
10        contact.setField("Salutation", "Professor");
11        contact.setField("Phone", "(999) 555-1234");
12        contact.setField("Title", "Philologist");
13    
14        // Add this sObject to an array 
15        SObject[] contacts = new SObject[1];
16        contacts[0] = contact;
17        // Make a create call and pass it the array of sObjects
18        SaveResult[] results = partnerConnection.create(contacts);
19    
20        // Iterate through the results list
21        // and write the ID of the new sObject
22        // or the errors if the object creation failed.
23        // In this case, we only have one result
24        // since we created one contact.
25        for (int j = 0; j < results.length; j++) {
26            if (results[j].isSuccess()) {
27                result = results[j].getId();
28                System.out.println(
29                    "\nA contact was created with an ID of: " + result
30                );
31             } else {
32                // There were errors during the create call,
33                // go through the errors array and write
34                // them to the console
35                for (int i = 0; i < results[j].getErrors().length; i++) {
36                    Error err = results[j].getErrors()[i];
37                    System.out.println("Errors were found on item " + j);
38                    System.out.println("Error code: " + 
39                        err.getStatusCode().toString());
40                    System.out.println("Error message: " + err.getMessage());
41                }
42             }
43        }
44    } catch (ConnectionException ce) {
45        ce.printStackTrace();
46    }
47    return result;
48}

C# Example

1swfobject.registerObject("clippy.codeblock-1", "9");public void createSample()
2{
3    try
4    {
5        // Create a new sObject of type Contact
6        // and fill out its fields.
7        sObject contact = new sforce.sObject();
8        System.Xml.XmlElement[] contactFields = new System.Xml.XmlElement[6];
9
10        // Create the contact's fields
11        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
12        contactFields[0] = doc.CreateElement("FirstName");
13        contactFields[0].InnerText = "Otto";
14        contactFields[1] = doc.CreateElement("LastName");
15        contactFields[1].InnerText = "Jespersen";
16        contactFields[2] = doc.CreateElement("Salutation");
17        contactFields[2].InnerText = "Professor";
18        contactFields[3] = doc.CreateElement("Phone");
19        contactFields[3].InnerText = "(999) 555-1234";
20        contactFields[4] = doc.CreateElement("Title");
21        contactFields[4].InnerText = "Philologist";
22
23        contact.type = "Contact";
24        contact.Any = contactFields;
25
26        // Add this sObject to an array
27        sObject[] contactList = new sObject[1];
28        contactList[0] = contact;
29
30        // Make a create call and pass it the array of sObjects 
31        SaveResult[] results = binding.create(contactList);
32        // Iterate through the results list
33        // and write the ID of the new sObject
34        // or the errors if the object creation failed.
35        // In this case, we only have one result
36        // since we created one contact.
37        for (int j = 0; j < results.Length; j++)
38        {
39            if (results[j].success)
40            {
41                Console.Write("\nA contact was created with an ID of: "
42                                + results[j].id);
43            }
44            else
45            {
46                // There were errors during the create call,
47                // go through the errors array and write
48                // them to the console
49                for (int i = 0; i < results[j].errors.Length; i++)
50                {
51                    Error err = results[j].errors[i];
52                    Console.WriteLine("Errors were found on item " + j.ToString());
53                    Console.WriteLine("Error code is: " + err.statusCode.ToString());
54                    Console.WriteLine("Error message: " + err.message);
55                }
56            }
57        }
58    }
59    catch (SoapException e)
60    {
61        Console.WriteLine("An unexpected error has occurred: " + e.Message +
62            " Stack trace: " + e.StackTrace);
63    }
64}