Classic UI session helper
scripts/dia-scripts/utils/sf-ui-session.mjs drives Setup pages that have no API, from a custom
step or a workaround. It opens a browser-like session on the org's Classic UI from the CLI's access
token: the single-access OAuth endpoint mints a one-time front door, and its redirects hand out the
UI session cookie.
| Call | Does |
|---|---|
SalesforceUiSession.open(targetOrg) |
Reads the token with sf org auth show-access-token (older CLIs: org display), logs in, returns the session. |
session.getPage(path) |
GETs a Classic page by path (/0I4…/e) and returns its HTML; throws when the session was lost. |
parseClassicForm(html, formName) |
The fields a browser would submit from the named form, as [name, value] pairs in document order. |
session.postForm(path, fields, submit) |
Re-posts the fields with the named submit button; returns accepted, the redirect target path, and any Classic error text. |
session.redact(text) |
Replaces the token and any sid= cookie in a string; pass every string you log through it. |
The usage pattern
Find the record through the API, GET the Classic edit form, change or keep the fields, post them with the form's save button, then read the value back: through the API where one exposes it, otherwise from the form again. An unchanged save is how Classic rewrites derived state that no API exposes.
Classic paths
A Classic Setup page is addressed by its record id or its list prefix, as the browser's address bar shows it with the Classic UI switched on:
| Path | Page |
|---|---|
/<orgId>/e |
Company Information, edit form (currency, locale, fiscal year) |
/<recordId>/e |
The edit form of any Setup record that has a Classic page |
/0I4 |
A Setup list page by key prefix (here: domains) |
Example: set the company currency
A post-deployment custom step that sets the org's default currency. In a single-currency org no
API exposes it: the Organization object has no currency field, only the Company Information edit
form in Classic does. Path, form name, field name and button name below were read from that page
(Edit Organization Profile): dump the parsed fields once, redacted, when you drive a new page.
// scripts/dia-scripts/deploy/custom-steps-post-deployment.mjs
import { parseArgs } from "node:util";
import { runSalesforceCommand } from "../sf-cli.mjs";
import { printHeader } from "../utils/log.mjs";
import {
SalesforceUiSession,
parseClassicForm,
} from "../utils/sf-ui-session.mjs";
// The option value is the locale-currency key of the Company Information picklist.
const CURRENCY = "de_DE_EURO";
const FORM = "editPage";
const FIELD = "DefaultCurrencyIsoCode";
// The hook loader calls this before run(); keep the export the shipped stub has.
export function parseArgumentsToOptions(argv) {
const { values } = parseArgs({
args: argv,
options: { "target-org": { type: "string", short: "o" } },
strict: false,
allowPositionals: true,
});
return { targetOrg: values["target-org"] };
}
function currencyOf(fields) {
return (fields.find(([name]) => name === FIELD) ?? [])[1];
}
export async function run({ targetOrg } = {}) {
printHeader(`Post-deployment: set the company currency to ${CURRENCY}`);
const { records } = await runSalesforceCommand([
"data",
"query",
"--query",
"SELECT Id FROM Organization",
"--target-org",
targetOrg,
]);
const editPath = `/${records[0].Id}/e`;
const session = await SalesforceUiSession.open(targetOrg);
const fields = parseClassicForm(await session.getPage(editPath), FORM);
// First run on a new page: console.log(session.redact(JSON.stringify(fields)));
if (currencyOf(fields) === CURRENCY) return;
const changed = fields.map(([name, value]) =>
name === FIELD ? [name, CURRENCY] : [name, value],
);
const outcome = await session.postForm(editPath, changed, "save");
if (!outcome.accepted) {
throw new Error(
`Company Information not saved: ${
outcome.error || `HTTP ${outcome.status}`
}`,
);
}
const after = currencyOf(
parseClassicForm(await session.getPage(editPath), FORM),
);
if (after !== CURRENCY) {
throw new Error(`Currency still ${after} after the save`);
}
}
The step is idempotent (it returns when the form already shows the value) and fails loudly when the save was rejected or did not take, which is what a deploy pipeline needs from it.
The token rule
The helper stops accidental exposure. Logging or serialising the session shows the token and
the cookies redacted, and every string that leaves it passes through redact(). Never write
a page's raw HTML to a log or an artifact without it.