3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SFDXからJSforceのコネクションを取得&Puppeteerでログイン

Last updated at Posted at 2020-04-16

書き捨てのスクリプトなどでSFDXからjsforceのコネクションを持ってくる方法。

調査

sfdx plugins:generateが吐くクラスが @salesforce/command の SfdxCommand クラスを継承し、
this.org.getConnection() でコネクションを取得してるのでそこから追った。

https://github.com/forcedotcom/cli-packages/blob/%40salesforce/command%403.0.0/packages/command/src/sfdxCommand.ts#L208-L211
ここで

      this.org = await Org.create({
        aliasOrUsername: this.flags.targetusername,
        aggregator: this.configAggregator
      });
      if (this.flags.apiversion) {
        this.org.getConnection().setApiVersion(this.flags.apiversion);
      }

となっているのでこれがOrgオブジェクトの生成して、その直後にコネクションは取得可能っぽい。

ファイルの先頭で

import { ConfigAggregator, Global, Logger, Messages, Mode, Org, SfdxError, SfdxProject } from '@salesforce/core';

してるので@salesforce/coreをインポートして Org.create().getConnection()で取得できる。

取得

$npm i -D jsforce @types/jsforce @salesforce/core @salesforce/ts-types

import { Connection } from "jsforce";
import { Org } from "@salesforce/core";

(async () => {
  const org = await Org.create({
    aliasOrUsername: null,
    aggregator: null
  });
  const conn = org.getConnection() as any as Connection;
})();

取得したアクセストークンでPuppeteerからログイン

import * as puppeteer from "puppeteer";
import { Org } from "@salesforce/core";

(async () => {
    const org = await Org.create({});
    const conn = org.getConnection();
    //   const browser = await puppeteer.launch();
    const browser = await puppeteer.launch({ headless: false });
    const page = await browser.newPage();
    const retURL = encodeURIComponent(`/lightning/o/Account/new`);
    const url = `${conn.instanceUrl}/secur/frontdoor.jsp?sid=${conn.accessToken}&retURL=${retURL}`;
    await page.goto(url);
    await page.waitFor(5e3);
    await page.screenshot({ path: "screenshot.png" });
    await browser.close();
})();
3
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?