These 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 contact4 SObject updateContact = new SObject();5 updateContact.setType("Contact");67 // Set the ID of the contact to update8 updateContact.setId(id);9 // Set the Phone field with a new value10 updateContact.setField("Phone", "(415) 555-1212");1112 // Create another contact that will cause an error13 // because it has an invalid ID.14 SObject errorContact = new SObject();15 errorContact.setType("Contact");16 // Set an invalid ID on purpose17 errorContact.setId("SLFKJLFKJ"); 18 // Set the value of LastName to null19 errorContact.setFieldsToNull(new String[] {"LastName"});2021 // Make the update call by passing an array containing22 // 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 errors29 // 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 write39 // 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 try3 {4 // Create an sObject of type contact5 sObject updateContact = new sObject();6 updateContact.type = "Contact";78 // Set the ID of the contact to update9 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"; 1516 // Add the Phone field to the contact17 updateContact.Any = new System.Xml.XmlElement[] {phoneField};1819 // Create another contact that will cause an error20 // because it has an invalid ID.21 sObject errorContact = new sObject();22 errorContact.type = "Contact";23 // Set an invalid ID on purpose24 errorContact.Id = "SLFKJLFKJ";25 // Set the value of LastName to null26 errorContact.fieldsToNull = new String[] { "LastName" };2728 // Make the update call by passing an array containing29 // 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 errors35 // 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 write47 // 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}