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.
Authentication and Org Management
Log in (opens browser)
CODEBLOCK0
Other login methods:
CODEBLOCK1
Manage orgs
CODEBLOCK2
Configuration and aliases
CODEBLOCK3
Querying Data (SOQL)
Standard SOQL queries via the default API:
CODEBLOCK4
For queries returning more than 10,000 records, use Bulk API instead:
CODEBLOCK5
Text Search (SOSL)
SOSL searches across multiple objects at once:
CODEBLOCK6
Single Record Operations
Get a record
CODEBLOCK7
Create a record (confirm with user first)
CODEBLOCK8
Update a record (confirm with user first)
CODEBLOCK9
Delete a record (require explicit user confirmation)
CODEBLOCK10
Bulk Data Operations (Bulk API 2.0)
For large datasets (thousands to millions of records):
Bulk export
CODEBLOCK11
Bulk import
CODEBLOCK12
Bulk upsert
CODEBLOCK13
Bulk delete
CODEBLOCK14
Tree export/import (for related records)
CODEBLOCK15
Schema Inspection
CODEBLOCK16
Execute Apex Code
CODEBLOCK17
REST API (Advanced)
Make arbitrary authenticated REST API calls:
CODEBLOCK18
Metadata Deployment and Retrieval
CODEBLOCK19
Diagnostics
CODEBLOCK20
Common SOQL Patterns
CODEBLOCK21
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.
Salesforce 技能
使用 Salesforce CLI (sf) 与 Salesforce 组织交互。使用前必须进行 CLI 认证。始终添加 --json 以获取结构化输出。
如果 sf 二进制文件不可用,可通过 npm (npm install -g @salesforce/cli) 安装,或从 https://developer.salesforce.com/tools/salesforcecli 下载。安装后,立即使用 sf org login web 进行认证以连接到 Salesforce 组织。
认证与组织管理
登录(打开浏览器)
bash
sf org login web --alias my-org
其他登录方式:
bash
基于 JWT 的登录(CI/自动化)
sf org login jwt --client-id
--jwt-key-file server.key --username user@example.com --alias my-org
使用现有访问令牌登录
sf org login access-token --instance-url https://mycompany.my.salesforce.com
通过 SFDX 认证 URL 登录(从文件)
sf org login sfdx-url --sfdx-url-file authUrl.txt --alias my-org
管理组织
bash
列出所有已认证的组织
sf org list --json
显示默认组织信息(访问令牌、实例 URL、用户名)
sf org display --json
显示特定组织信息
sf org display --target-org my-org --json
显示包含 SFDX 认证 URL 的信息(敏感信息 - 包含刷新令牌)
sf org display --target-org my-org --verbose --json
在浏览器中打开组织
sf org open
sf org open --target-org my-org
注销
sf org logout --target-org my-org
配置与别名
bash
设置默认目标组织
sf config set target-org my-org
列出所有配置变量
sf config list
获取特定配置值
sf config get target-org
设置别名
sf alias set prod=user@example.com
列出别名
sf alias list
数据查询(SOQL)
通过默认 API 进行标准 SOQL 查询:
bash
基本查询
sf data query --query SELECT Id, Name, Email FROM Contact LIMIT 10 --json
WHERE 子句
sf data query --query SELECT Id, Name, Amount, StageName FROM Opportunity WHERE StageName = Closed Won --json
关系查询(父到子)
sf data query --query SELECT Id, Name, (SELECT LastName, Email FROM Contacts) FROM Account LIMIT 5 --json
关系查询(子到父)
sf data query --query SELECT Id, Name, Account.Name FROM Contact --json
LIKE 文本搜索
sf data query --query SELECT Id, Name FROM Account WHERE Name LIKE %Acme% --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 FROM Account --all-rows --json
从文件查询
sf data query --file query.soql --json
Tooling API 查询(元数据对象,如 ApexClass、ApexTrigger)
sf data query --query SELECT Id, Name, Status FROM ApexClass --use-tooling-api --json
输出到 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 FROM Account --target-org my-org --json
对于返回超过 10,000 条记录的查询,请改用 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
文本搜索(SOSL)
SOSL 可同时跨多个对象进行搜索:
bash
跨对象搜索文本
sf data search --query FIND {John Smith} IN ALL FIELDS RETURNING Contact(Name, Email), Lead(Name, Email) --json
仅在名称字段中搜索
sf data search --query FIND {Acme} IN NAME FIELDS RETURNING Account(Name, Industry), Contact(Name) --json
从文件搜索
sf data search --file search.sosl --json
输出到 CSV
sf data search --query FIND {test} RETURNING Contact(Name) --result-format csv
单条记录操作
获取记录
bash
按记录 ID
sf data get record --sobject Contact --record-id 003XXXXXXXXXXXX --json
按字段匹配(类似 WHERE)
sf data get record --sobject Account --where Name=Acme --json
按多个字段(带空格的字段值需使用单引号)
sf data get record --sobject Account --where Name=Universal Containers Phone=(123) 456-7890 --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 对象
sf data create record --sobject TraceFlag --use-tooling-api --values DebugLevelId=7dl... LogType=CLASS_TRACING --json
更新记录(需先经用户确认)
bash
按 ID
sf data update record --sobject Contact --record-id 003XXXXXXXXXXXX --values Email=updated@example.com --json
按字段匹配
sf data update record --sobject Account --where Name=Old Acme --values Name=New Acme --json
多个字段
sf data update record --sobject Account --record-id 001XXXXXXXXXXXX --values Name=Acme III Website=www.example.com --json
删除记录(需明确用户确认)
bash
按 ID
sf data delete record --sobject Account --record-id 001XXXXXXXXXXXX --json
按字段匹配
sf data delete record --sobject Account --where Name=Acme --json
批量数据操作(Bulk API 2.0)
适用于大数据集(数千到数百万条记录):
批量导出
bash
导出到 CSV
sf data export bulk --query SELECT Id, Name, Email FROM Contact --output-file contacts.csv --result-format csv --wait 10
导出到 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.csv --result-format csv --all-rows --wait 10
恢复超时的导出
sf data export resume --job-id 750XXXXXXXXXXXX --json
批量导入
bash
从 CSV 导入
sf data import bulk --file accounts.csv --sobject Account --wait 10
恢复超时的导入
sf data import resume --job-id 750XXXXXXXXXXXX --json
批量更新插入
bash
sf data upsert bulk --file contacts.csv --sobject Contact --external-id Email --wait 10
批量删除
bash
删除 CSV 中列出的记录(CSV 必须包含 Id 列)
sf data delete bulk --file records-to-delete.csv --sobject Contact --wait 10
树形导出/导入(适用于关联记录)
bash
将关联关系导出为 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 FROM Account --plan --output-dir export-data
从树形 JSON 文件导入
sf data import tree --files Account.json,Contact.json
使用计划定义文件导入
sf data import tree --plan Account-Contact-plan.json
架构检查
bash
描述对象(字段、关系、选项列表值)
sf sobject describe --sobject Account --json
描述自定义对象
sf sobject describe --sobject MyCustomObjectc --json
描述 Tooling API 对象
sf sobject describe --sobject Apex