update コール例
次の Java および C# の例では、Partner WSDL の update() コールの使用方法を示します。各例では、更新する取引先責任者の ID を引数として取ります。種別が取引先責任者の 2 つの sObject レコード (1 つは ID で渡された有効な ID を持ち、もう一方は無効な ID を持つ) を作成します。次に、有効な取引先責任者に新しい電話番号を設定し、無効な取引先責任者の姓に null を設定します。その後、update コールを実行し、結果を反復処理します。更新操作が成功した場合は、更新された取引先責任者の ID を書き込みます。更新操作が失敗した場合は、返されたすべてのエラーの詳細をコンソールに書き込みます。この場合、出力は正常に更新された取引先責任者の ID と無効な取引先責任者の更新のエラーです。
サンプルメソッドを実行するには、Partner WSDL の使用例で提供されている対応する Java または C# テンプレートクラスを使用できます。
Java の例
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}setFieldsToNull (または WSC 以外のクライアントツールでの同様の機能) の詳細は、「fieldsToNull」および「値の null へのリセット」を参照してください。
C# の例
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}