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

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

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

C# Example

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