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

Partner WSDL の使用例

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

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

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

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

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

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

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

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