LoginSignup
3
3

More than 5 years have passed since last update.

Salesforceで名寄せするためのツールを作成

Last updated at Posted at 2018-03-03

きっかけ

Salesforceってマージ機能はあるけど名寄せする候補の材料をシステム的に出力する機能はないな~と思いJavaですごく簡単な名寄せツール作ったときのメモ書き。

ツールの内容

  • Salesforceの取引先のデータをAPIで取得する
  • Java1.8で取引先名が重複していたらファイル出力する→後は目視確認。

手順

1.WSCを使い自分の環境用のSalesforceのJarファイルを作成する

  • Salesforceにログインして設定、APIからWSDLをダウンロード(名前をwsdl.xmlとつけた)
    sfdc1.png

  • 以下のサイトにアクセスしてプロジェクトのファイルをダウンロード
    https://github.com/forcedotcom/wsc

  • Eclipseを起動してダウンロードしたファイルをインポート

  • 実行、Mavenビルドを選択してゴールPackageを入力してJar作成
    sfdc2.png

  • 作成したら以下のコマンド入力。inputwsdlfileは上記でダウンロードしたWSDLファイルを指定する

Javaコマンド
java -jar target/force-wsc-42.0.0-uber.jar <inputwsdlfile> <outputjarfile>
例)java -jar target/force-wsc-42.0.0-uber.jar wsdl.xml original.jar

2.Eclipseでプロジェクト作成

  • Eclipseで新規プロジェクト作成、Mavenプロジェクトを選択

  • 上記で作成したjarファイルをプロジェクトに配置する
     (今回、libファイルを作成してその直下においた)
    sfdc3.png

  • pom.xmlに上記で作成したJarと関連ライブラリを追加する

pom.xml
<dependency>
    <groupId>net.original</groupId>
    <artifactId>originalJar</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/original.jar</systemPath>
</dependency>
<dependency>
    <groupId>com.force.api</groupId>
    <artifactId>force-wsc</artifactId>
    <version>42.0.0</version>
</dependency>
<dependency>
    <groupId>com.force.api</groupId>
    <artifactId>force-partner-api</artifactId>
    <version>42.0.0</version>
</dependency>
  • public static void mainに以下のメソッドを追加
App.java
package sfdc.api;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

/**
 * Salesforce Connect
 *
 */
public class App
{
    public static void main( String[] args )
    {
        ConnectorConfig config = new ConnectorConfig();
        config.setUsername("ログインID");
        config.setPassword("パスワード+セキュリティトークン");
        // Soapサービスのバージョンは更新されるので以下のサイトを参考
        // https://help.salesforce.com/articleView?id=000126966&language=ja&type=1
        String soapEndpoint = "https://login.salesforce.com/services/Soap/c/40.0";
        config.setAuthEndpoint(soapEndpoint);
        System.out.println("\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

       try {
           EnterpriseConnection connection = new EnterpriseConnection(config);
           QueryResult queryResults = connection.query("SELECT Id,Name,LastModifiedDate FROM Account Order by Name");
           SObject[] records = queryResults.getRecords();
           String beforeId = "";
           String beforeName = "";
           for(SObject record : records) {
               String name = ((Account) record).getName();
               String id = ((Account) record).getId();
               Calendar lastModifiedDate = ((Account) record).getLastModifiedDate();
               System.out.println("sfid{" + id + "}");
               if(name.equals(beforeName)) {
                   fileWriter("重複しています[" +  beforeName + "], sfid{" + beforeId + "}");
                   fileWriter("重複しています[" +  name + "], sfid{" + id + "}");
               }
               beforeId = id;
               beforeName = name;
           }
           System.out.println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        } catch (ConnectionException ce) {
          ce.printStackTrace();
        }
    }

    private static void fileWriter(String name) {
        try {
            File f = new File("nayose.txt");
            BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
            bw.write(name);
            bw.newLine();
            bw.close();
          } catch (IOException e) {
            System.out.println(e);
          }
    }
}

  • Salesforceで重複したデータがある場合、実行すると以下のようなテキストが出力される
重複しています[田中太郎], sfid{0017F10xxxxxxxxxx}
重複しています[田中太郎], sfid{0015A02xxxxxxxxxx}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3