create コール例
次の Java および C# の例では、Partner WSDL の create() コールの使用方法を示します。各例では、複数の項目を持つ取引先責任者レコードを作成します。create コールの結果を反復処理し、操作が成功したかどうかを確認します。作成操作が成功した場合は、作成された取引先責任者の ID をコンソールに書き込みます。成功しなかった場合は、エラーを反復処理し、各エラーの詳細をコンソールに書き込みます。この例では、出力は新しい取引先責任者の ID です。
サンプルメソッドを実行するには、Partner WSDL の使用例で提供されている対応する Java または C# テンプレートクラスを使用できます。
Java の例
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public String createSample() {
18 String result = null;
19 try {
20 // Create a new sObject of type Contact
21 // and fill out its fields.
22 SObject contact = new SObject();
23 contact.setType("Contact");
24 contact.setField("FirstName", "Otto");
25 contact.setField("LastName", "Jespersen");
26 contact.setField("Salutation", "Professor");
27 contact.setField("Phone", "(999) 555-1234");
28 contact.setField("Title", "Philologist");
29
30 // Add this sObject to an array
31 SObject[] contacts = new SObject[1];
32 contacts[0] = contact;
33 // Make a create call and pass it the array of sObjects
34 SaveResult[] results = partnerConnection.create(contacts);
35
36 // Iterate through the results list
37 // and write the ID of the new sObject
38 // or the errors if the object creation failed.
39 // In this case, we only have one result
40 // since we created one contact.
41 for (int j = 0; j < results.length; j++) {
42 if (results[j].isSuccess()) {
43 result = results[j].getId();
44 System.out.println(
45 "\nA contact was created with an ID of: " + result
46 );
47 } else {
48 // There were errors during the create call,
49 // go through the errors array and write
50 // them to the console
51 for (int i = 0; i < results[j].getErrors().length; i++) {
52 Error err = results[j].getErrors()[i];
53 System.out.println("Errors were found on item " + j);
54 System.out.println("Error code: " +
55 err.getStatusCode().toString());
56 System.out.println("Error message: " + err.getMessage());
57 }
58 }
59 }
60 } catch (ConnectionException ce) {
61 ce.printStackTrace();
62 }
63 return result;
64}C# の例
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public void createSample()
18{
19 try
20 {
21 // Create a new sObject of type Contact
22 // and fill out its fields.
23 sObject contact = new sforce.sObject();
24 System.Xml.XmlElement[] contactFields = new System.Xml.XmlElement[6];
25
26 // Create the contact's fields
27 System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
28 contactFields[0] = doc.CreateElement("FirstName");
29 contactFields[0].InnerText = "Otto";
30 contactFields[1] = doc.CreateElement("LastName");
31 contactFields[1].InnerText = "Jespersen";
32 contactFields[2] = doc.CreateElement("Salutation");
33 contactFields[2].InnerText = "Professor";
34 contactFields[3] = doc.CreateElement("Phone");
35 contactFields[3].InnerText = "(999) 555-1234";
36 contactFields[4] = doc.CreateElement("Title");
37 contactFields[4].InnerText = "Philologist";
38
39 contact.type = "Contact";
40 contact.Any = contactFields;
41
42 // Add this sObject to an array
43 sObject[] contactList = new sObject[1];
44 contactList[0] = contact;
45
46 // Make a create call and pass it the array of sObjects
47 SaveResult[] results = binding.create(contactList);
48 // Iterate through the results list
49 // and write the ID of the new sObject
50 // or the errors if the object creation failed.
51 // In this case, we only have one result
52 // since we created one contact.
53 for (int j = 0; j < results.Length; j++)
54 {
55 if (results[j].success)
56 {
57 Console.Write("\nA contact was created with an ID of: "
58 + results[j].id);
59 }
60 else
61 {
62 // There were errors during the create call,
63 // go through the errors array and write
64 // them to the console
65 for (int i = 0; i < results[j].errors.Length; i++)
66 {
67 Error err = results[j].errors[i];
68 Console.WriteLine("Errors were found on item " + j.ToString());
69 Console.WriteLine("Error code is: " + err.statusCode.ToString());
70 Console.WriteLine("Error message: " + err.message);
71 }
72 }
73 }
74 }
75 catch (SoapException e)
76 {
77 Console.WriteLine("An unexpected error has occurred: " + e.Message +
78 " Stack trace: " + e.StackTrace);
79 }
80}