7
5

More than 3 years have passed since last update.

【AWS】【Lambda】 SalesforceのREST APIを呼び出す

Last updated at Posted at 2020-08-16
  • 表題の通り、LambdaからSalesforceのREST APIを呼び出してみます
  • Lambdaの言語はNode.jsを使用します

Salesforce接続アプリケーションの設定

  • 外部サービスとSalesforceを接続する際にOAuth認証を使います
  • OAuthの設定をするには、Salesforceで接続アプリケーションの設定を行います

設定方法

  • Salesforceにログインした後、[設定]>[アプリケーション - アプリケーションマネージャ]と選んで
  • [新規接続アプリケーション]ボタンをクリックします
  • 以下を入力します
    • 接続アプリケーション名
      • 適当に「AWS Lambda」とでも
    • API参照名
      • 自動入力されます
    • 取引先責任者メール
      • とりあえずシステム管理者のアドレスにしておきましょう
    • OAuth設定の有効化
      • チェックします
    • コールバックURL
      • 今の所は適当に
    • 選択したOAuth範囲
      • フルアクセス・ユーザに代わっていつでも要求を実行
    • Webサーバフローの秘密が必要
      • チェックします
  • 作成した後、[参照]画面から、コンシューマ鍵とコンシューマの秘密を確認します
  • コンシューマ鍵はクライアントID、コンシューマの秘密はシークレットキーに該当します

Salesforce接続ライブラリの選定

  • Node.jsで使えるSalesforce接続ライブラリで有名なものに、jsForceとnForceがあります

jsForce

nForce

比較すると・・・

Lambdaの記述サンプル

jsForceを使用した例(Typescript)

import * as jsforce from 'jsforce';

exports.handler =  (event: any) => {
    console.log('Lambda Start');
    const conn = new jsforce.Connection({
        loginUrl: 'https://test.salesforce.com', // 本番なら'https://login.salesforce.com'
        clientId: 'クライアントID',
        clientSecret: 'シークレットキー',
        redirectUri: 'https://login.salesforce.com/services/oauth2/success'
    });
    const username = 'Salesforceユーザー名';
    const password = 'Salesforceパスワード';
    conn.login(username, password, (err: any, userInfo: any) => {
        if (!err) {
            // SELECT
            conn.query("SELECT Id, Name FROM Account", undefined, (err: any, result: any) => {
                if (!err) {
                    console.log(result);
                } else {
                    console.log(err);
                }
            });
            // INSERT
            conn.sobject("Account").create({ Name: 'Lambda of jsforce' }, (err: any, result: any) => {
                if (!err) {
                    console.log(result);
                } else {
                    console.log(err);
                }
            })
        } else {
            console.log(err);
        }
    });
    console.log('Lambda End');
};

nForceを使用した例(Javascript)

const nforce = require('nforce');

exports.handler = function(event) {

    console.log('Lambda Start');
    let org = nforce.createConnection({
        clientId: 'クライアントID', 
        clientSecret: 'シークレットキー',
        redirectUri: 'https://login.salesforce.com/services/oauth2/success',
        environment: 'sandbox', // 本番なら'product'
        mode: 'multi'
    });

    const username = 'Salesforceユーザー名';
    const password = 'Salesforceパスワード';

    org.authenticate({ username: username, password: password }, function(err, oauth){
        if(!err) {
            // SELECT
            org.query({ oauth, query: 'SELECT id, name FROM Account'}, function(err, results) {
                if (err) {
                    console.log(err);
                } else {
                    console.log(results.records);
                }
            });
            // INSERT
            const acc = nforce.createSObject('Account');
            acc.set('Name', 'Lambda of nforce');
            org.insert({ sobject: acc, oauth: oauth}, function(err, resp) {
                if (err) {
                    console.log(err);
                } else {
                    console.log(resp);
                }
            });

        } else {
            console.log('Error: ' + err.message);
        }
    });
    console.log('Lambda End');
};
  • 個人的にはjsForceの方が使いやすいですね
7
5
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
7
5