Salesforce オブジェクトをIN句で検索するサンプル

環境
Salesforce
Node.js

/* 名前(Name)がaa社、bb社、cc社のいずれかにマッチする取引先だけ取得する */
List<String> comps = new List<String>{'aa社’, 'bb社’, 'cc社’};
List<Account> accounts = [SELECT Id, Name, CreatedDate FROM Account WHERE Name IN :comps];
System.debug(accounts);

JSforceはJavaScriptからSalesforceのAPIにアクセスするライブラリです。
このJSforceで先述のIN句を使うためには次のようにします。
参考
https://jsforce.github.io/

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const comps = ['aa社', 'bb社', 'cc社']
const accounts = []
conn
.sobject("Account")
.find({
'Name': { $in: comps }
}, ['Id', 'Name', 'CreatedDate'])
.on("record", (record) => {
accounts.push(record)
})
.on("end", () => {
})
.on("error", (err) => {
console.error(err)
})
.run({ autoFetch : true, maxFetch : 100 })
const comps = ['aa社', 'bb社', 'cc社'] const accounts = [] conn .sobject("Account") .find({ 'Name': { $in: comps } }, ['Id', 'Name', 'CreatedDate']) .on("record", (record) => { accounts.push(record) }) .on("end", () => { }) .on("error", (err) => { console.error(err) }) .run({ autoFetch : true, maxFetch : 100 })
const comps = ['aa社', 'bb社', 'cc社']
const accounts = []
conn
  .sobject("Account")
  .find({
    'Name': { $in: comps }
  }, ['Id', 'Name', 'CreatedDate'])
  .on("record", (record) => {
    accounts.push(record)
  })
  .on("end", () => {
  })
 .on("error", (err) => {
    console.error(err)
  })
  .run({ autoFetch : true, maxFetch : 100 })

JSforceのメリット
レコードのCRUD(Create, Retrieve, Update, Upsert, Destroy)が可能
Metadataを扱える
Apex REST APIをコールできる
Bulk処理ができる

IT

Posted by arkgame