この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Partner WSDL の使用例

このセクションでは、Partner WSDL を使用して API コールを行う Java および C# の例を示します。これらのサンプルを実行する前に、クイックスタートチュートリアルの次のステップを実行して、Partner WSDL ファイルを取得し、開発環境用のプロキシクライアントコードを生成してください。

プロキシクライアントコードを生成し、開発環境を設定した後に、クライアントアプリケーションの作成を開始できます。まず、パートナー認証エンドポイントを使用して、アプリケーションで Salesforce サービスにログインする必要があります。ログインが成功したら、サンプルメソッドを実行できます。

利便性を実現するため、login コールを実行するテンプレートクラスが 1 つは Java で、または 1 つは C# で提供されています。これらのテンプレートクラスを使用して、このセクションの後で説明するサンプルメソッドを実行できます。

Java 用のサンプルテンプレートクラス: このサンプルでは、ユーザ名、パスワード、認証エンドポイントの入力画面を表示します。次に、ユーザをログインします。認証エンドポイント URL では、Partner WSDL ファイルで見つかったエンドポイントを渡します。

1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17import com.sforce.soap.partner.PartnerConnection;
18import com.sforce.soap.partner.sobject.*;
19import com.sforce.soap.partner.*;
20import com.sforce.ws.ConnectorConfig;
21import com.sforce.ws.ConnectionException;
22import com.sforce.soap.partner.Error;
23import java.io.FileNotFoundException;
24import java.io.IOException;
25import java.io.InputStreamReader;
26import java.io.BufferedReader;
27import java.util.*;
28
29public class PartnerSamples {
30    PartnerConnection partnerConnection = null;
31    private static BufferedReader reader =
32        new BufferedReader(new InputStreamReader(System.in));
33    
34    public static void main(String[] args) {
35        PartnerSamples samples = new PartnerSamples();
36        if (samples.login()) {
37            // Add calls to the methods in this class.
38            // For example:
39            // samples.querySample();
40        }
41    } 
42    
43    private String getUserInput(String prompt) {
44        String result = "";
45        try {
46          System.out.print(prompt);
47          result = reader.readLine();
48        } catch (IOException ioe) {
49          ioe.printStackTrace();
50        }
51        return result;
52    }
53    
54    private boolean login() {
55        boolean success = false;
56        String username = getUserInput("Enter username: ");
57        String password = getUserInput("Enter password: ");
58        String authEndPoint = getUserInput("Enter auth end point: ");
59
60        try {
61          ConnectorConfig config = new ConnectorConfig();
62          config.setUsername(username);
63          config.setPassword(password);
64          
65          config.setAuthEndpoint(authEndPoint);
66          config.setTraceFile("traceLogs.txt");
67          config.setTraceMessage(true);
68          config.setPrettyPrintXml(true);
69
70          partnerConnection = new PartnerConnection(config);          
71
72          success = true;
73        } catch (ConnectionException ce) {
74          ce.printStackTrace();
75        } catch (FileNotFoundException fnfe) {
76          fnfe.printStackTrace();
77        }
78
79        return success;
80      }
81
82    // 
83    // Add your methods here.
84    //
85}
86

C# 用のサンプルテンプレートクラス: このサンプルでは、ユーザ名とパスワードの入力画面を表示します。次に、ユーザをログインします。このサンプルのプロジェクト名は TemplatePartner で、Web 参照名は sforce であることを想定しています。これらの値がプロジェクトで異なると、using ディレクティブを「using your_project_name.web_reference_name;」のように、実際のプロジェクト名と Web 参照名を含む値に変更してください。

1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.Web.Services.Protocols;
22using System.Collections;
23using TemplatePartner.sforce;
24
25namespace TemplatePartner
26{
27    class PartnerSamples
28    {
29        private SforceService binding;
30
31        static void Main(string[] args)
32        {
33            PartnerSamples samples = new PartnerSamples();
34            if (samples.login())
35            {
36            // Add calls to the methods in this class.
37            // For example:
38            // samples.querySample();
39            }
40        }
41
42        private bool login()
43        {
44            Console.Write("Enter username: ");
45            string username = Console.ReadLine();
46            Console.Write("Enter password: ");
47            string password = Console.ReadLine();
48
49            // Create a service object 
50            binding = new SforceService();
51
52            // Timeout after a minute 
53            binding.Timeout = 60000;
54
55            // Try logging in   
56            LoginResult lr;
57            try
58            {
59
60                Console.WriteLine("\nLogging in...\n");
61                lr = binding.login(username, password);
62            }
63
64            // ApiFault is a proxy stub generated from the WSDL contract when     
65            // the web service was imported 
66            catch (SoapException e)
67            {
68                // Write the fault code to the console 
69                Console.WriteLine(e.Code);
70
71                // Write the fault message to the console 
72                Console.WriteLine("An unexpected error has occurred: " + e.Message);
73
74                // Write the stack trace to the console 
75                Console.WriteLine(e.StackTrace);
76
77                // Return False to indicate that the login was not successful 
78                return false;
79            }
80
81            // Check if the password has expired 
82            if (lr.passwordExpired)
83            {
84                Console.WriteLine("An error has occurred. Your password has expired.");
85                return false;
86            }
87
88            // Set the returned service endpoint URL
89            binding.Url = lr.serverUrl;
90
91            // Set the SOAP header with the session ID returned by
92            // the login result. This will be included in all
93            // API calls.
94            binding.SessionHeaderValue = new SessionHeader();
95            binding.SessionHeaderValue.sessionId = lr.sessionId;            
96
97            // Return true to indicate that we are logged in, pointed  
98            // at the right URL and have our security token in place.     
99            return true;
100        }
101
102    // 
103    // Add your methods here.
104    //
105}
106

この Partner WSDL のサンプルは次のとおりです。