分类 Salesforce 下的文章

Salesforce Advice

Reduce class level variables data storage

Don't use class level variables to store a large amounts of data.

User SOQL for Loops

Utilize SOQL For Loops to iterate and process data from large queries.

for(Conatct ctt : [Select Id,Name,Account.Name FROM Contact]) {
    // do your work

Shorten variables lifetime

Construct methods and loops that allow variables to go out of scope as soon as they are no longer needed.

Other Advice

Using Efficient Algorithms

Being the heart of any code, algorithms can make or break the running of your application. A good space-efficient algorithm can save a lot of your heap memory while simultaneously performing smoothly. Before developing any logic or writing any code snippet, sit, and think through the algorithms to use and choose the one that serves your purpose and makes use of the heap memory efficiently. Try not to split the parts of your algorithms into different functions; rather try to keep them in line when used only within that code.

Built-in Apex libraries

Try to leverage and make use of the inbuilt Apex libraries that Salesforce provides other than writing your custom logic for everything. For example, use the Math class for performing mathematical operations, use JSONGenerator class to create JSON structures, and many more. One line of method calling code is way better than rewriting the logic on your own. This not only saves you time but also the heap memory is managed efficiently.

Avoid using temporary variables

Creating temporary variables is a favorite practice of many developers. We unknowingly create such variables in our code without realizing that we can do without them. These variables take up unnecessary space and add up to the heap memory.

Shorten variable names & declarations

You can spare characters when naming temporary variables like loop counter variables as i instead of myCounter. You should keep meaningful names according to the business.
You can also combine variable declarations like:

Integer i = 0, j = 10;

Shorten Field API Name

We all love to give meaningful names to the custom fields in Salesforce objects. Little do we know that a long API name impacts the code heap size. For example, it is counted in SOQL query size, code that references the field, and each time it is accessed. Maximum length of SOQL statements is 100K.

Remove unnecessary debug statements

Since all the debug statements are counted against the code length, it is necessary to keep a cap on them in order to limit the consumed heap size of the Apex code. It is fine to use them while doing the apex development but should be removed or minimized in the final production version of the code.




如果我们文件为Attachment,在Salesforce Internal (即使用Salesforce账户登录的,非Digital Experience, Site.com),则可以使用

 <a href="{!URLFOR($Action.Attachment.Download, attachmentId)}">File Name</a>



<img src="{!URLFOR($Action.Attachment.Download, attachmentId)}" />


如果是File,则可以使用Url.getFileFieldURL(entityId, fieldName)


Site.com上的则不能用上面的方法,使用的话会直接报错。这时可以通过Salesforce提供的文件下载api /servlet/servlet.FileDownload?file=xxxx~18,不过要注意,一定要使用Salesorce Internal Url,使用Site.com的相关url肯定也会报错。

  1. 首先在后台获取 Salesforce Instance Url 或 Org Domain Url。例如:
    String orgUrl = Url.getOrgDomainUrl().toExternalForm()
  2. 将其传到前台,拼接成下载地址:
    <a href="{!orgUrl + '/servlet/servlet.FileDownload?file=' + attachment.Id}">File Name</a>


  1. Visualforce Developer Guide: $Action
  2. Apex Reference Guide: URLClass
  3. stackexchange: How to get salesforce instance url
  4. Salesforce Developer Forum: Download Attachment from Sites??
  5. Salesforce Developer Forum: How to get downloadable link for attachment?

Salesforce 未锁定包(Salesforce Unlocked Package)是Salesforce最新推广的基于包的开发最佳实践。通过Salesforce CLI,我们可以很方便的制作和部署未锁定包。本文只是简略的将核心步骤展示出来:

  1. 创建一个 SFDX 项目
sfdx force:project:create -d demo-workspace -n expenser-app -p force-app
  1. 授权Dev Hub环境,该环境必须启用Dev Hub功能和未锁定包和第二代管控包功能
sfdx auth:web:login -d -a devHub
  1. 创建一个草稿环境(scratch org)并在其中开发包
sfdx force:org:create -f config/project-scratch-def.json -u scratchOrg1
  1. 保证所有要打包的组件都已经在当前的项目文件夹内
  2. 从 SFDX 项目文件夹,直接创建未锁定包:
sfdx force:package:create -n "Demo App" -r force-app -t Unlocked -o [email protected]
  1. 检查项目文件夹中的sfdx-project.json文件,CLI 会自动更新项目文件,使其包含上一步创建的包的信息:
   "packageDirectories": [
         "path": "force-app",
         "default": true,
         "package": "Demo App",
         "versionName": "ver 0.1",
         "versionNumber": "0.1.0.NEXT"
   "namespace": "",
   "sfdcLoginUrl": "https://login.salesforce.com",
   "sourceApiVersion": "50.0",
   "packageAliases": {
      "Expense Manager": "0Hoxxx"
  1. 创建一个包的版本,Salesforce CLI会自动处理包的版本号等
sfdx force:package:version:create --package "Demo App" -k lushang.me --wait 10
  1. 在另一个创建好的草稿环境中,安装并测试这个版本的包:
sfdx force:package:install -p "Demo [email protected]" -y testOrg1 -k lushang.me --wait 10 --publishwait 10
  1. 如果上一步骤没问题,则可以发布这个版本的包:
sfdx force:package:version:promote -p "Demo [email protected]"


  1. Salesforce DX Developer Guide
  2. Workflow for Unlocked Packages
  3. Release an Unlocked Package


  1. statista上面有一个Size of the public cloud platform as a service (PaaS) market worldwide from 2015 to 2022 (更新时间 by Arne Holst, May 4, 2020):
  2. businesswire上面Global Platform as a service (PaaS) Market Report 2020 - ResearchAndMarkets.com (December 04, 2019 09:53 AM Eastern Standard Time):

The global platform as a service (PaaS) market was valued at about $29.58 billion in 2018 and is expected to grow to $52.4 billion at a CAGR of 15.4% through 2022.

  1. Gartner的[Gartner Says Nearly 50 Percent of PaaS Offerings Are Now Cloud-Only]4:

The total PaaS market revenue is forecast to reach $20 billion in 2019, and to exceed $34 billion in 2022, according to the latest forecast from Gartner.

  1. Planet Market Reports的[Platform-as-a-service PaaS Market 2019 | Global Market Size, Share, Supply, Demand, Segments and Forecast 2024]5:

The global platform as a service market size will grow by USD 31.4 billion during 2024.



Semi-Join | 半连接



  1. 获取某些具有特定阶段或者截至日期Opportunity的Account下面所有Contact信息
  2. 获取具有激活的Contract的Account下面所有的Opportunity信息


  1. 使用Id字段的半连接例子:
SELECT Id, Name 
FROM Account 
  ( SELECT AccountId
    FROM Opportunity
    WHERE StageName = 'Closed Lost' 
  1. 使用外键的半连接例子:
FROM Task 
    FROM Contact
    WHERE MailingCity = 'Twin Falls'

Anti-Join | 反连接

反连接(Anti-Join)与半连接非常类似,只是不同于半连接使用的比较关键字为IN, 反连接使用的比较关键字为NOT IN。其余部分包括写法、限制等是一致的,不再赘述。


  1. 使用Id字段的半连接例子:
ELECT Id, (Select Id, Amount, CloseDate FROM Opportunities)
FROM Account
    SELECT AccountId
    FROM Contact
    WHERE LastName LIKE 'Trump%'
  1. 使用外键的半连接例子:
SELECT Id, Name, Phone, Email
FROM Contact
    SELECT AccountId
    FROM Opportunity
    WHERE CloseDate < Last_Week


  1. 在主语句的WHERE分句中,不能超过2INNOT IN语句
  2. 不能在半连接或者反连接前使用NOT操作符。若有这种情况,可以考虑把半连接转换为反连接,或者反连接转换为全连接
  3. 必须是Id字段或者外键,诸如... Where Account.Id IN (...)是非法的
  4. 子查询的查询字段必须与主查询对应的字段SObject一致,是且必须是对应的外键
  5. 子查询的字段条数不受限制,但是主查询条数仍收到限制
  6. 半连接或者反连接不能嵌套使用,即不能在半连接/反连接中再使用半连接或者反连接
  7. 子查询不能使用OR
  9. 下列对象不支持:

    • ActivityHistory
    • Attachments
    • Event
    • EventAttendee
    • Note
    • OpenActivity
    • Tags (AccountTag, ContactTag, and all other tag objects)
    • Task

详情可以查看SOQL and SOSL Reference以下章节:
Semi-Joins with IN and Anti-Joins with NOT IN