【Salesforce开发工具篇】Visual Studio Code安装配置,使用,快捷键及常见问题一览

前言】:
1. VS Code需要.NET Framework 4.5.2或更高的版本,如果你使用win7,请按如下操作查看.NET Framework版本号:
Win + R -> 输入cmd -> 输入:reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\full" /v version,如下图:

环境安装】:
1. 安装Visual Studio Code
2. 安装Command Line Interface (CLI);macOS | Windows 64-bit
3. 测试CLI是否安装成功:打开命令行并输入sfdx,如下即为成功:

4. 打开Visual Studio Code安装Salesforce Extension Pack插件;

工具使用】:以Windows为例
1. 创建Std项目模板:
a. 使用Ctrl + Shift + P快捷键,输入Create Project with Manifest
b. 按Enter键选择标准项目模板填写Project名称
c. 最后选择文件本地存储路径即可生成标准文件模板,如下:

2. 连接Salesforce Org:
a. 使用Ctrl + Shift + P快捷键,输入Authorize an Org
b. 选择Sandbox / Production环境;
c. 选填Org Alias,并按Enter,即可跳转web登录页;
d. 一旦登录成功,Org连接就完成了:下图将显示Org Alias(未填写默认为vscodeOrg) + 左侧边栏多出2个icon按钮;

3. 从服务器拉取项目代码:
a. 使用Ctrl + P快捷键,输入package.xml并打开该文件;

b 订阅所需的元数据对象,如ApexClass,ApexPage等,更多订阅请参阅Sublime中安装HaoIDE Salesforce开发工具 + 配置Lightning Q1
c. 在package.xml处右键点选SFDX: Retrieve Source in Manifest from Org即可下载所需元数据对象文件:

4. 从本地上传代码到服务器:
a. 随便打开一个文件,如下:

b. 右键点选SFDX: Deploy This Source to Org,成功后出现如上console信息。

常用快捷键】:sfdx-cheatsheet.sh | dx-commands
VS Code Hacks / Commands / Configurations
--------------------------------------------------------
1. Execute Commands -> Ctrl + Shift + P
2. Search for Files -> Ctrl + P
3. Set theme -> Ctrl + K +T / Ctrl + Shift + P, enter theme, then set it.
4. Auto - Deploy on Save -> 
5. Auto - Generate Comments ->
6. Auto - Generate Getter and Setter
7. Multi-Select -> Go to the first occurence, Ctrl + D again for each occurence
8. Move selected text with arrow keys -> Alt (pressed) + Arrow Keys for selected text
9. Auto - Generate package.xml
10. Better Comments ->
11. Search in Project -> Ctrl + Shift + F

关于部署】:force:source:deploy | Develop Against Any Org with Visual Studio Code Tasks

即便是带Profile部署字段到PRO,使用sfdx CLI也不能绕过testLevel,此时比起运行本地测试,我更倾向指定一个简短的测试类。

sfdx force:source:deploy -x 'manifest\package.xml' -l RunSpecifiedTests -r FloorTriggerTest -u ap1_pro -w 33 --verbose --loglevel fatal

CLI命令行参考】:
1. 查看sfdx的相关信息:

2. 列出所有sfdx commands:

3. 使用sfdx force查看版本号,和相关参考文档:

4. 使用sfdx help force查看有哪些支持项:

5. 比如我们想查看force:mdapi (元数据API)在项目部署方面有哪些用法:sfdx help force:mdapi

6. 再进一步,如果我们想使用force:mdapi:deploy来部署项目,想通过sfdx help force:mdapi:deploy来查看用法:

7. 批量删除字段实践

sfdx force:mdapi:deploy -d destroy -u jll_dev -w -1

常见问题及解决方案】:
Q1、SFDX: Diff File Against Org failed to run? SFDX: diff file against org #1584
A1、使用Saleforce Diff插件解決本地文件和服务器文件差分

Q2、SFDX: Authorize an Org failed to run? 
ERROR running force:auth:web:login:  Cannot start the OAuth redirect server on port PortInUseAction. 
A2、Kill the process running on port 1717 or use a custom connected app and update OauthLocalPort in the sfdx-project.json file.
Win + X -> 以管理员身份运行Shell -> 键入:netstat -ano | findstr :1717,获得port为1717程序的PID -> 前往任务管理器在详细信息tab对PID排序后找到目标程序,并结束。

示例:

代码片段:

netstat -ano | findstr :9856

taskkill /pid 14620 /f

Q3、运行测试类,如何查看代码覆盖率?
A3、使用【Ctrl + ,】调出Settings,输入"converage",勾选下图圈点处即可:

启用前后运行测试类log对比:
【前】:

【后】:

最后,vs code会保存运行过的测试结果,如下:

Q4、部署自定义对象时报错:Must specify a non-empty plural label for the CustomObject
A4、截至21年除夕,Error: Must specify a non-empty label for the CustomObject这个Known Issues目前处于In Review状态,不过我们可以在对象的原始数据文件中加入<pluralLabel>Courses</pluralLabel>

如下图:

参见:Metadata API Developer Guide - CustomObject

Q5、用Salesforce Package.xml Generator Extension for VS Code构建package.xml拉取元数据列表失败咋办?
A5、请在尝试卸载插件重新安装前,Delete a scratch org with alias,重新授权登入环境再试,可能是因为长期未登录造成的登录认证过期造成的。

Q6、写于2021-04-22,前不久更新了vs code,随后在pr_dev上传代码后通过GUI切换到sg_dev做代码部署时,出现Error: INVALID_SESSION_ID: Session expired or invalid,如何破?
A6、临时方案有2,1是重新跑sg_dev的web auth,之后再通过GUI切换环境:

sfdx force:auth:web:login --setalias sg_dev --instanceurl https://test.salesforce.com

2是继续通过GUI切换,切换后点击下图所示的浏览器图标登进org,随后再按常规部署

Q7、部署代码检查冲突如何设定?
A7、用户设定查找并勾选"Salesforcedx-vscode-core: Detect Conflicts At Sync",详见Detect Conflicts on Deploy,要点如下图:

 当然,如果你用git,还可以通过下面方式diff冲突:Simple way to diff a file in local SFDX project against Production version with SFDX

git checkout -b temp-branch
git add force-app
git commit -m "temp commit for compare"
sfdx force:source:retrieve -p force-app
git diff

Q8、部署时遇到"Error authenticating with the refresh token due to: expired access/refresh token"错误,重新Auth也未解决,咋整?
A8、重启电脑 -> 再部署一遍

VS Code优缺点】:
1. 优点:
a. 在使用sublime时,部署代码遇见:This schedulable class has jobs pending or in progress at line -1 column -1,可以通过VS Code直接部署;
解决方案:在Sublime上使用Deploy To This Server / Deploy To Server替代Save To Server即可正常部署

2. 缺点:
a. 修改完代码deploy前,一定要记得保存,然后deploy change才会成功;
解决方案:使用快捷键【Ctrl + ,】调出Settings,输入"enablePreview",禁用下图所示option即可。该配置除了能解决手动保存的问题,还能解决Ctrl + P搜索并预览多文件

Good to Know】:

1. 创建项目的方法:Git Bash Here...

参考资料】:Org Development Model with VS Code | Make Visual Studio Code Salesforce Ready | How to setup Visual Studio Code for Salesforce ?

系统学习合集】:
Your Complete Guide to Understanding Salesforce DX | Flosum
Getting Started with Salesforce DX (Part 1 of 5)
Getting Started With Salesforce DX (Part 2 of 5)
Getting Started with Salesforce DX (Part 3 of 5)
Getting Started With Salesforce DX (Part 4 of 5)
Getting Started with Salesforce DX (Part 5 of 5)

相关资源】:
Productive Development with Visual Studio Code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值