salesforce

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Salesforce Skill

Salesforce Skill

Use the Salesforce CLI (
sf
) to interact with Salesforce orgs. The CLI must be authenticated before use. Always add
--json
for structured output.
If the
sf
binary is not available, install it via npm (
npm install -g @salesforce/cli
) or download it from https://developer.salesforce.com/tools/salesforcecli. After installing, authenticate immediately with
sf org login web
to connect to a Salesforce org.
使用Salesforce CLI(
sf
)与Salesforce组织交互。使用前必须先完成CLI身份验证。请始终添加
--json
参数以获取结构化输出。
如果没有
sf
二进制文件,可通过npm安装(
npm install -g @salesforce/cli
),或从https://developer.salesforce.com/tools/salesforcecli 下载。安装完成后,请立即通过
sf org login web
命令进行身份验证,连接到Salesforce组织。

Authentication and Org Management

身份验证与组织管理

Log in (opens browser)

登录(打开浏览器)

bash
sf org login web --alias my-org
Other login methods:
bash
undefined
bash
sf org login web --alias my-org
其他登录方式:
bash
undefined

JWT-based login (CI/automation)

基于JWT的登录(CI/自动化场景)

sf org login jwt --client-id <consumer-key> --jwt-key-file server.key --username user@example.com --alias my-org
sf org login jwt --client-id <consumer-key> --jwt-key-file server.key --username user@example.com --alias my-org

Login with an existing access token

使用现有访问令牌登录

sf org login access-token --instance-url https://mycompany.my.salesforce.com
sf org login access-token --instance-url https://mycompany.my.salesforce.com

Login via SFDX auth URL (from a file)

通过SFDX认证URL登录(从文件读取)

sf org login sfdx-url --sfdx-url-file authUrl.txt --alias my-org
undefined
sf org login sfdx-url --sfdx-url-file authUrl.txt --alias my-org
undefined

Manage orgs

管理组织

bash
undefined
bash
undefined

List all authenticated orgs

列出所有已认证的组织

sf org list --json
sf org list --json

Display info about the default org (access token, instance URL, username)

显示默认组织的信息(访问令牌、实例URL、用户名)

sf org display --json
sf org display --json

Display info about a specific org

显示指定组织的信息

sf org display --target-org my-org --json
sf org display --target-org my-org --json

Display with SFDX auth URL (sensitive - contains refresh token)

显示包含SFDX认证URL的信息(敏感信息 - 包含刷新令牌)

sf org display --target-org my-org --verbose --json
sf org display --target-org my-org --verbose --json

Open org in browser

在浏览器中打开组织

sf org open sf org open --target-org my-org
sf org open sf org open --target-org my-org

Log out

登出

sf org logout --target-org my-org
undefined
sf org logout --target-org my-org
undefined

Configuration and aliases

配置与别名

bash
undefined
bash
undefined

Set default target org

设置默认目标组织

sf config set target-org my-org
sf config set target-org my-org

List all config variables

列出所有配置变量

sf config list
sf config list

Get a specific config value

获取指定配置项的值

sf config get target-org
sf config get target-org

Set an alias

设置别名

sf alias set prod=user@example.com
sf alias set prod=user@example.com

List aliases

列出所有别名

sf alias list
undefined
sf alias list
undefined

Querying Data (SOQL)

数据查询(SOQL)

Standard SOQL queries via the default API:
bash
undefined
通过默认API执行标准SOQL查询:
bash
undefined

Basic query

基础查询

sf data query --query "SELECT Id, Name, Email FROM Contact LIMIT 10" --json
sf data query --query "SELECT Id, Name, Email FROM Contact LIMIT 10" --json

WHERE clause

WHERE条件查询

sf data query --query "SELECT Id, Name, Amount, StageName FROM Opportunity WHERE StageName = 'Closed Won'" --json
sf data query --query "SELECT Id, Name, Amount, StageName FROM Opportunity WHERE StageName = 'Closed Won'" --json

Relationship queries (parent-to-child)

关联查询(父到子)

sf data query --query "SELECT Id, Name, (SELECT LastName, Email FROM Contacts) FROM Account LIMIT 5" --json
sf data query --query "SELECT Id, Name, (SELECT LastName, Email FROM Contacts) FROM Account LIMIT 5" --json

Relationship queries (child-to-parent)

关联查询(子到父)

sf data query --query "SELECT Id, Name, Account.Name FROM Contact" --json
sf data query --query "SELECT Id, Name, Account.Name FROM Contact" --json

LIKE for text search

文本搜索LIKE用法

sf data query --query "SELECT Id, Name FROM Account WHERE Name LIKE '%Acme%'" --json
sf data query --query "SELECT Id, Name FROM Account WHERE Name LIKE '%Acme%'" --json

Date filtering

日期过滤

sf data query --query "SELECT Id, Name, CreatedDate FROM Lead WHERE CreatedDate = TODAY" --json
sf data query --query "SELECT Id, Name, CreatedDate FROM Lead WHERE CreatedDate = TODAY" --json

ORDER BY + LIMIT

排序+限制返回条数

sf data query --query "SELECT Id, Name, Amount FROM Opportunity ORDER BY Amount DESC LIMIT 20" --json
sf data query --query "SELECT Id, Name, Amount FROM Opportunity ORDER BY Amount DESC LIMIT 20" --json

Include deleted/archived records

包含已删除/归档的记录

sf data query --query "SELECT Id, Name FROM Account" --all-rows --json
sf data query --query "SELECT Id, Name FROM Account" --all-rows --json

Query from a file

从文件读取查询语句执行

sf data query --file query.soql --json
sf data query --file query.soql --json

Tooling API queries (metadata objects like ApexClass, ApexTrigger)

Tooling API查询(针对ApexClass、ApexTrigger等元数据对象)

sf data query --query "SELECT Id, Name, Status FROM ApexClass" --use-tooling-api --json
sf data query --query "SELECT Id, Name, Status FROM ApexClass" --use-tooling-api --json

Output to CSV file

输出到CSV文件

sf data query --query "SELECT Id, Name, Email FROM Contact" --result-format csv --output-file contacts.csv
sf data query --query "SELECT Id, Name, Email FROM Contact" --result-format csv --output-file contacts.csv

Target a specific org

指定目标组织查询

sf data query --query "SELECT Id, Name FROM Account" --target-org my-org --json

For queries returning more than 10,000 records, use Bulk API instead:
```bash
sf data export bulk --query "SELECT Id, Name, Email FROM Contact" --output-file contacts.csv --result-format csv --wait 10
sf data export bulk --query "SELECT Id, Name FROM Account" --output-file accounts.json --result-format json --wait 10
sf data query --query "SELECT Id, Name FROM Account" --target-org my-org --json

对于返回超过10000条记录的查询,请改用Bulk API:
```bash
sf data export bulk --query "SELECT Id, Name, Email FROM Contact" --output-file contacts.csv --result-format csv --wait 10
sf data export bulk --query "SELECT Id, Name FROM Account" --output-file accounts.json --result-format json --wait 10

Text Search (SOSL)

文本搜索(SOSL)

SOSL searches across multiple objects at once:
bash
undefined
SOSL支持同时跨多个对象搜索:
bash
undefined

Search for text across objects

跨对象全文搜索

sf data search --query "FIND {John Smith} IN ALL FIELDS RETURNING Contact(Name, Email), Lead(Name, Email)" --json
sf data search --query "FIND {John Smith} IN ALL FIELDS RETURNING Contact(Name, Email), Lead(Name, Email)" --json

Search in name fields only

仅在名称字段搜索

sf data search --query "FIND {Acme} IN NAME FIELDS RETURNING Account(Name, Industry), Contact(Name)" --json
sf data search --query "FIND {Acme} IN NAME FIELDS RETURNING Account(Name, Industry), Contact(Name)" --json

Search from a file

从文件读取搜索语句执行

sf data search --file search.sosl --json
sf data search --file search.sosl --json

Output to CSV

输出到CSV

sf data search --query "FIND {test} RETURNING Contact(Name)" --result-format csv
undefined
sf data search --query "FIND {test} RETURNING Contact(Name)" --result-format csv
undefined

Single Record Operations

单记录操作

Get a record

获取记录

bash
undefined
bash
undefined

By record ID

通过记录ID获取

sf data get record --sobject Contact --record-id 003XXXXXXXXXXXX --json
sf data get record --sobject Contact --record-id 003XXXXXXXXXXXX --json

By field match (WHERE-like)

通过字段匹配获取(类似WHERE)

sf data get record --sobject Account --where "Name=Acme" --json
sf data get record --sobject Account --where "Name=Acme" --json

By multiple fields (values with spaces need single quotes)

通过多个字段匹配获取(值包含空格需要用单引号包裹)

sf data get record --sobject Account --where "Name='Universal Containers' Phone='(123) 456-7890'" --json
undefined
sf data get record --sobject Account --where "Name='Universal Containers' Phone='(123) 456-7890'" --json
undefined

Create a record (confirm with user first)

创建记录(需先征得用户确认)

bash
sf data create record --sobject Contact --values "FirstName='Jane' LastName='Doe' Email='jane@example.com'" --json

sf data create record --sobject Account --values "Name='New Company' Website=www.example.com Industry='Technology'" --json
bash
sf data create record --sobject Contact --values "FirstName='Jane' LastName='Doe' Email='jane@example.com'" --json

sf data create record --sobject Account --values "Name='New Company' Website=www.example.com Industry='Technology'" --json

Tooling API object

Tooling API对象创建

sf data create record --sobject TraceFlag --use-tooling-api --values "DebugLevelId=7dl... LogType=CLASS_TRACING" --json
undefined
sf data create record --sobject TraceFlag --use-tooling-api --values "DebugLevelId=7dl... LogType=CLASS_TRACING" --json
undefined

Update a record (confirm with user first)

更新记录(需先征得用户确认)

bash
undefined
bash
undefined

By ID

通过ID更新

sf data update record --sobject Contact --record-id 003XXXXXXXXXXXX --values "Email='updated@example.com'" --json
sf data update record --sobject Contact --record-id 003XXXXXXXXXXXX --values "Email='updated@example.com'" --json

By field match

通过字段匹配更新

sf data update record --sobject Account --where "Name='Old Acme'" --values "Name='New Acme'" --json
sf data update record --sobject Account --where "Name='Old Acme'" --values "Name='New Acme'" --json

Multiple fields

多字段更新

sf data update record --sobject Account --record-id 001XXXXXXXXXXXX --values "Name='Acme III' Website=www.example.com" --json
undefined
sf data update record --sobject Account --record-id 001XXXXXXXXXXXX --values "Name='Acme III' Website=www.example.com" --json
undefined

Delete a record (require explicit user confirmation)

删除记录(需要用户明确确认)

bash
undefined
bash
undefined

By ID

通过ID删除

sf data delete record --sobject Account --record-id 001XXXXXXXXXXXX --json
sf data delete record --sobject Account --record-id 001XXXXXXXXXXXX --json

By field match

通过字段匹配删除

sf data delete record --sobject Account --where "Name=Acme" --json
undefined
sf data delete record --sobject Account --where "Name=Acme" --json
undefined

Bulk Data Operations (Bulk API 2.0)

批量数据操作(Bulk API 2.0)

For large datasets (thousands to millions of records):
适用于大型数据集(数千到数百万条记录):

Bulk export

批量导出

bash
undefined
bash
undefined

Export to CSV

导出为CSV

sf data export bulk --query "SELECT Id, Name, Email FROM Contact" --output-file contacts.csv --result-format csv --wait 10
sf data export bulk --query "SELECT Id, Name, Email FROM Contact" --output-file contacts.csv --result-format csv --wait 10

Export to JSON

导出为JSON

sf data export bulk --query "SELECT Id, Name FROM Account" --output-file accounts.json --result-format json --wait 10
sf data export bulk --query "SELECT Id, Name FROM Account" --output-file accounts.json --result-format json --wait 10

Include soft-deleted records

包含软删除的记录

sf data export bulk --query "SELECT Id, Name FROM Account" --output-file accounts.csv --result-format csv --all-rows --wait 10
sf data export bulk --query "SELECT Id, Name FROM Account" --output-file accounts.csv --result-format csv --all-rows --wait 10

Resume a timed-out export

恢复超时的导出任务

sf data export resume --job-id 750XXXXXXXXXXXX --json
undefined
sf data export resume --job-id 750XXXXXXXXXXXX --json
undefined

Bulk import

批量导入

bash
undefined
bash
undefined

Import from CSV

从CSV导入

sf data import bulk --file accounts.csv --sobject Account --wait 10
sf data import bulk --file accounts.csv --sobject Account --wait 10

Resume a timed-out import

恢复超时的导入任务

sf data import resume --job-id 750XXXXXXXXXXXX --json
undefined
sf data import resume --job-id 750XXXXXXXXXXXX --json
undefined

Bulk upsert

批量Upsert(更新插入)

bash
sf data upsert bulk --file contacts.csv --sobject Contact --external-id Email --wait 10
bash
sf data upsert bulk --file contacts.csv --sobject Contact --external-id Email --wait 10

Bulk delete

批量删除

bash
undefined
bash
undefined

Delete records listed in CSV (CSV must have an Id column)

删除CSV中列出的记录(CSV必须包含Id列)

sf data delete bulk --file records-to-delete.csv --sobject Contact --wait 10
undefined
sf data delete bulk --file records-to-delete.csv --sobject Contact --wait 10
undefined

Tree export/import (for related records)

树形结构导出/导入(适用于关联记录)

bash
undefined
bash
undefined

Export with relationships into JSON tree format

导出为带关联关系的JSON树形格式

sf data export tree --query "SELECT Id, Name, (SELECT Name, Email FROM Contacts) FROM Account" --json
sf data export tree --query "SELECT Id, Name, (SELECT Name, Email FROM Contacts) FROM Account" --json

Export with a plan file (for multiple objects)

导出包含计划文件(适用于多对象)

sf data export tree --query "SELECT Id, Name FROM Account" --plan --output-dir export-data
sf data export tree --query "SELECT Id, Name FROM Account" --plan --output-dir export-data

Import from tree JSON files

从树形JSON文件导入

sf data import tree --files Account.json,Contact.json
sf data import tree --files Account.json,Contact.json

Import using a plan definition file

通过计划定义文件导入

sf data import tree --plan Account-Contact-plan.json
undefined
sf data import tree --plan Account-Contact-plan.json
undefined

Schema Inspection

Schema查看

bash
undefined
bash
undefined

Describe an object (fields, relationships, picklist values)

描述对象(字段、关联关系、下拉列表值)

sf sobject describe --sobject Account --json
sf sobject describe --sobject Account --json

Describe a custom object

描述自定义对象

sf sobject describe --sobject MyCustomObject__c --json
sf sobject describe --sobject MyCustomObject__c --json

Describe a Tooling API object

描述Tooling API对象

sf sobject describe --sobject ApexClass --use-tooling-api --json
sf sobject describe --sobject ApexClass --use-tooling-api --json

List all objects

列出所有对象

sf sobject list --json
sf sobject list --json

List only custom objects

仅列出自定义对象

sf sobject list --sobject custom --json
sf sobject list --sobject custom --json

List only standard objects

仅列出标准对象

sf sobject list --sobject standard --json
undefined
sf sobject list --sobject standard --json
undefined

Execute Apex Code

执行Apex代码

bash
undefined
bash
undefined

Execute Apex from a file

从文件执行Apex

sf apex run --file script.apex --json
sf apex run --file script.apex --json

Run interactively (type code, press Ctrl+D to execute)

交互式执行(输入代码,按Ctrl+D执行)

sf apex run
sf apex run

Run Apex tests

运行Apex测试

sf apex run test --test-names MyTestClass --json
sf apex run test --test-names MyTestClass --json

Get test results

获取测试结果

sf apex get test --test-run-id 707XXXXXXXXXXXX --json
sf apex get test --test-run-id 707XXXXXXXXXXXX --json

View Apex logs

查看Apex日志

sf apex list log --json sf apex get log --log-id 07LXXXXXXXXXXXX
undefined
sf apex list log --json sf apex get log --log-id 07LXXXXXXXXXXXX
undefined

REST API (Advanced)

REST API(高级用法)

Make arbitrary authenticated REST API calls:
bash
undefined
发起任意已认证的REST API调用:
bash
undefined

GET request

GET请求

sf api request rest 'services/data/v62.0/limits' --json
sf api request rest 'services/data/v62.0/limits' --json

List API versions

列出API版本

sf api request rest '/services/data/' --json
sf api request rest '/services/data/' --json

Create a record via REST

通过REST创建记录

sf api request rest '/services/data/v62.0/sobjects/Account' --method POST --body '{"Name":"REST Account","Industry":"Technology"}' --json
sf api request rest '/services/data/v62.0/sobjects/Account' --method POST --body '{"Name":"REST Account","Industry":"Technology"}' --json

Update a record via REST (PATCH)

通过REST更新记录(PATCH)

sf api request rest '/services/data/v62.0/sobjects/Account/001XXXXXXXXXXXX' --method PATCH --body '{"BillingCity":"San Francisco"}' --json
sf api request rest '/services/data/v62.0/sobjects/Account/001XXXXXXXXXXXX' --method PATCH --body '{"BillingCity":"San Francisco"}' --json

GraphQL query

GraphQL查询

sf api request graphql --body '{"query":"{ uiapi { query { Account { edges { node { Name { value } } } } } } }"}' --json
sf api request graphql --body '{"query":"{ uiapi { query { Account { edges { node { Name { value } } } } } }"}' --json

Custom headers

自定义请求头

sf api request rest '/services/data/v62.0/limits' --header 'Accept: application/xml'
sf api request rest '/services/data/v62.0/limits' --header 'Accept: application/xml'

Save response to file

保存响应到文件

sf api request rest '/services/data/v62.0/limits' --stream-to-file limits.json
undefined
sf api request rest '/services/data/v62.0/limits' --stream-to-file limits.json
undefined

Metadata Deployment and Retrieval

元数据部署与拉取

bash
undefined
bash
undefined

Deploy metadata to an org

部署元数据到组织

sf project deploy start --source-dir force-app --json
sf project deploy start --source-dir force-app --json

Deploy specific metadata components

部署指定元数据组件

sf project deploy start --metadata ApexClass:MyClass --json
sf project deploy start --metadata ApexClass:MyClass --json

Retrieve metadata from an org

从组织拉取元数据

sf project retrieve start --metadata ApexClass --json
sf project retrieve start --metadata ApexClass --json

Check deploy status

检查部署状态

sf project deploy report --job-id 0AfXXXXXXXXXXXX --json
sf project deploy report --job-id 0AfXXXXXXXXXXXX --json

Generate a new Salesforce DX project

生成新的Salesforce DX项目

sf project generate --name my-project
sf project generate --name my-project

List metadata components in the org

列出组织中的元数据组件

sf project list ignored --json
undefined
sf project list ignored --json
undefined

Diagnostics

诊断

bash
undefined
bash
undefined

Run CLI diagnostics

运行CLI诊断

sf doctor
sf doctor

Check CLI version

查看CLI版本

sf version
sf version

See what is new

查看新功能

sf whatsnew
undefined
sf whatsnew
undefined

Common SOQL Patterns

常用SOQL模式

sql
-- Count records
SELECT COUNT() FROM Contact WHERE AccountId = '001XXXXXXXXXXXX'

-- Aggregate query
SELECT StageName, COUNT(Id), SUM(Amount) FROM Opportunity GROUP BY StageName

-- Date literals
SELECT Id, Name FROM Lead WHERE CreatedDate = LAST_N_DAYS:30

-- Subquery (semi-join)
SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact WHERE Email LIKE '%@acme.com')

-- Polymorphic lookup
SELECT Id, Who.Name, Who.Type FROM Task WHERE Who.Type = 'Contact'

-- Multiple WHERE conditions
SELECT Id, Name, Amount FROM Opportunity WHERE Amount > 10000 AND StageName != 'Closed Lost' AND CloseDate = THIS_QUARTER
sql
-- 统计记录数
SELECT COUNT() FROM Contact WHERE AccountId = '001XXXXXXXXXXXX'

-- 聚合查询
SELECT StageName, COUNT(Id), SUM(Amount) FROM Opportunity GROUP BY StageName

-- 日期字面量
SELECT Id, Name FROM Lead WHERE CreatedDate = LAST_N_DAYS:30

-- 子查询(半连接)
SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact WHERE Email LIKE '%@acme.com')

-- 多态查找
SELECT Id, Who.Name, Who.Type FROM Task WHERE Who.Type = 'Contact'

-- 多WHERE条件
SELECT Id, Name, Amount FROM Opportunity WHERE Amount > 10000 AND StageName != 'Closed Lost' AND CloseDate = THIS_QUARTER

Guardrails

使用规范

  • Always use
    --json
    for structured, parseable output.
  • Never create, update, or delete records without explicit user confirmation. Describe the operation and ask before executing.
  • Never delete records unless the user explicitly requests it and confirms the specific record(s).
  • Never bulk delete or bulk import without user reviewing the file/query and confirming.
  • Use
    LIMIT
    on queries to avoid excessive data. Start with
    LIMIT 10
    and increase if the user needs more.
  • For queries over 10,000 records, use
    sf data export bulk
    instead of
    sf data query
    .
  • When the user asks to "find" or "search" a single object, use SOQL
    WHERE ... LIKE '%term%'
    . When searching across multiple objects, use SOSL via
    sf data search
    .
  • Use
    --target-org <alias>
    when the user has multiple orgs; ask which org if ambiguous.
  • If authentication fails or a session expires, guide the user through
    sf org login web
    .
  • Bulk API 2.0 has SOQL limitations (no aggregate functions like
    COUNT()
    ). Use standard
    sf data query
    for those.
  • When describing objects (
    sf sobject describe
    ), the JSON output can be very large. Summarize the key fields, required fields, and relationships for the user rather than dumping the raw output.
  • **始终使用
    --json
    **获取结构化、可解析的输出。
  • 没有用户明确确认的情况下,切勿创建、更新或删除记录。执行操作前请先描述操作内容并征得用户同意。
  • 除非用户明确请求并确认具体记录,否则切勿删除记录
  • 没有用户审核文件/查询并确认的情况下,切勿执行批量删除或批量导入
  • 查询时使用
    LIMIT
    避免返回过多数据。默认先使用
    LIMIT 10
    ,如果用户需要更多数据再调整。
  • 对于返回超过10000条记录的查询,使用
    sf data export bulk
    代替
    sf data query
  • 当用户要求“查找”或“搜索”单个对象时,使用SOQL的
    WHERE ... LIKE '%term%'
    。当跨多个对象搜索时,通过
    sf data search
    使用SOSL。
  • 当用户有多个组织时,使用
    --target-org <alias>
    ;如果存在歧义,询问用户要操作的组织。
  • 如果身份验证失败或会话过期,引导用户通过
    sf org login web
    重新登录。
  • Bulk API 2.0存在SOQL限制(不支持
    COUNT()
    等聚合函数),此类查询请使用标准的
    sf data query
  • 当描述对象(
    sf sobject describe
    )时,JSON输出可能非常大。请为用户总结关键字段、必填字段和关联关系,而不是直接输出原始内容。