Newer Version Available

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

Sample update Call

The following Java and C# examples show how to use the update() call for the Partner WSDL. Each example takes the ID of the contact to update as an argument. It creates two sObject records of type Contact—one to hold the valid passed in ID and the other has an invalid ID. Next, it sets a new phone number for the valid contact and null for the last name of the invalid contact. It then makes the update call and iterates through the results. For a successful update operation, it writes the ID of the contact that got updated. For a failed update operation, it writes the details of all returned errors to the console. In this case, the output is the ID of the contact that was successfully updated and an error for the invalid contact update.

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");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public void updateSample(String id) {
18  try {
19      // Create an sObject of type contact
20      SObject updateContact = new SObject();
21      updateContact.setType("Contact");
22      
23      // Set the ID of the contact to update
24      updateContact.setId(id);
25      // Set the Phone field with a new value
26      updateContact.setField("Phone", "(415) 555-1212");
27
28      // Create another contact that will cause an error
29      // because it has an invalid ID.
30      SObject errorContact = new SObject();
31      errorContact.setType("Contact");
32      // Set an invalid ID on purpose
33      errorContact.setId("SLFKJLFKJ");      
34      // Set the value of LastName to null
35      errorContact.setFieldsToNull(new String[] {"LastName"});
36
37      // Make the update call by passing an array containing
38      // the two objects. 
39      SaveResult[] saveResults = partnerConnection.update(
40         new SObject[] {updateContact, errorContact}
41      );
42      // Iterate through the results and write the ID of 
43      // the updated contacts to the console, in this case one contact.
44      // If the result is not successful, write the errors
45      // to the console. In this case, one item failed to update.
46      for (int j = 0; j < saveResults.length; j++) {
47          System.out.println("\nItem: " + j);
48          if (saveResults[j].isSuccess()) {
49              System.out.println("Contact with an ID of " +
50                      saveResults[j].getId() + " was updated.");
51          }
52          else {                        
53            // There were errors during the update call,
54            // go through the errors array and write
55            // them to the console.
56            for (int i = 0; i < saveResults[j].getErrors().length; i++) {
57              Error err = saveResults[j].getErrors()[i];
58              System.out.println("Errors were found on item " + j);
59              System.out.println("Error code: " + 
60                  err.getStatusCode().toString());
61              System.out.println("Error message: " + err.getMessage());
62            }
63          }      
64      }      
65  } catch (ConnectionException ce) {
66      ce.printStackTrace();
67  }
68}

For more information about setFieldsToNull (or its equivalent in client tools other than WSC), see fieldsToNull and Resetting Values to null.

C# Example

1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public void updateSample(String id) {
18    try
19    {
20        // Create an sObject of type contact
21        sObject updateContact = new sObject();
22        updateContact.type = "Contact";
23      
24        // Set the ID of the contact to update
25        updateContact.Id = id;
26        // Set the Phone field to a new value.
27        // The Phone field needs to be created as an XML element.
28        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
29        System.Xml.XmlElement phoneField = doc.CreateElement("Phone");
30        phoneField.InnerText = "(415) 555-1212";  
31            
32        // Add the Phone field to the contact
33        updateContact.Any = new System.Xml.XmlElement[] {phoneField};
34
35        // Create another contact that will cause an error
36        // because it has an invalid ID.
37        sObject errorContact = new sObject();
38        errorContact.type = "Contact";
39        // Set an invalid ID on purpose
40        errorContact.Id = "SLFKJLFKJ";
41        // Set the value of LastName to null
42        errorContact.fieldsToNull = new String[] { "LastName" };
43
44        // Make the update call by passing an array containing
45        // the two objects.             
46        SaveResult[] saveResults = binding.update(
47            new sObject[] {updateContact, errorContact});
48        // Iterate through the results and write the ID of 
49        // the updated contacts to the console, in this case one contact.
50        // If the result is not successful, write the errors
51        // to the console. In this case, one item failed to update.
52        for (int j = 0; j < saveResults.Length; j++) {
53            Console.WriteLine("\nItem: " + j);
54            if (saveResults[j].success) 
55            {
56                Console.WriteLine("Contact with an ID of " +
57                saveResults[j].id + " was updated.");
58            }
59            else 
60            {                        
61                // There were errors during the update call,
62                // go through the errors array and write
63                // them to the console.
64                for (int i = 0; i < saveResults[j].errors.Length; i++) {
65                    Error err = saveResults[j].errors[i];
66                    Console.WriteLine("Errors were found on item " + j.ToString());
67                    Console.WriteLine("Error code: " + 
68                        err.statusCode.ToString());
69                    Console.WriteLine("Error message: " + err.message);
70                }
71            }      
72        }
73    }
74    catch (SoapException e)
75    {
76        Console.WriteLine("An unexpected error has occurred: " + e.Message +
77            " Stack trace: " + e.StackTrace);
78    }
79}