SFDX 常用命令以及相关资料

这里会学习到SFDX 常用命令,同时需要具备Git的相关知识。

SFDX

安装

1
$ npm install sfdx-cli --global

更新:

sfdx update

登入Salesforce Org

1
sfdx force:auth:web:login -d -a DevHub

登入 Sandbox:

1
sfdx force:auth:web:login -r https://test.salesforce.com

or
添加新的Sandbox Login

1
$ sfdx force:auth:web:login -r https://test.salesforce.com  -d -a DevHub

新建DX项目

Create a Salesforce DX Project

1
sfdx force:project:create --projectname mywork

查看你所拥有的Org

1
2
3
4
5
6
7
8
9
10
11
12
sfdx force:org:list
=== Orgs
ALIAS USERNAME ORG ID CONNECTED STATUS
----------- -------------------- -------- ----------------
DD-ORG jdoe@dd-204.com 00D...OEA Connected
(D) devhuborg jdoe@mydevhub.com 00D...MAC Connected


ALIAS SCRATCH ORG NAME USERNAME ORG ID EXPIRATION DATE
---------- ------------ -------------------------- --------- ----------
my-scratch Your Company test-wvkm5z113@example.com 00D...UAI 2017-06-13
(U) scratch208 Your Company test-wvkm5z113@example.com 00D...UAY 2017-06-13

删除non-active 的Scratch Org:

1
sfdx force:org:list --clean

设置默认的 Dev Hub

1
sfdx force:config:set defaultdevhubusername=org@username.com  --global

新建一个Scratch Org

1
2
3
sfdx force:org:create -s -f config/project-scratch-def.json -a dreamhouse-org
or
sfdx force:org:create -f config/project-scratch-def.json

成功的结果:

1
Successfully created scratch org: 00D0l0000009PENEA2, username: test-im44gkueld8k@example.com

删除一个Scratch Org

1
$ sfdx force:org:delete -u  test-s6r2le1cgpnd@example.com

设置defaultusername

1
2
3
4
5
$ sfdx force:config:set defaultusername=test-phvqjvms7dsj@example.com
=== Set Config
NAME VALUE
─────────────── ─────────────────────────────
defaultusername test-phvqjvms7dsj@example.com

打开Scratch Org

1
2
$ sfdx force:org:open
Access org 00D0l0000009PENEA2 as user test-im44gkueld8k@example.com with the following URL: https://force-data-1839-dev-ed.cs58.my.sxxxx.com alesforce.com//secur/frontdoor.jsp?sid=00D0l0000009PEN!ARoAQCCFPl.pwPYTXXNszi9rR0Q6CMrJc77771yynXH.jbnJbweH5b19bc_6KXo9BkMtMY1PLz_nY BO9SSEOyQnPnCWAU3gP

用username打开Scratch Org

1
sfdx force:org:open -u  test-phvqjvms7dsj@example.com

推送本地修改至Scratch Org

1
sfdx force:source:push

拉取所有metadata from Scratch Org

1
sfdx force:source:pull

添加Permission

1
2
3
4
5
$ sfdx force:user:permset:assign -n Dreamhouse
=== Permsets Assigned
USERNAME PERMISSION SET ASSIGNMENT
───────────────────────────── ─────────────────────────
test-im44gkueld8k@example.com Dreamhouse

导入数据

1
2
Administrator@EC2AMAZ-IOQ3I22 MINGW64 ~/my_sfdx_project/dreamhouse-sfdx (my_branch)
$ sfdx force:data:tree:import --plan data/sample-data-plan.json

其中,数据格式如下:

sample-data-plan.json:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[
{
"sobject": "Broker__c",
"saveRefs": true,
"files": [
"brokers-data.json"
]
},
{
"sobject": "Property__c",
"resolveRefs": true,
"files": [
"properties-data.json"
]
}
]

有两个文件,一个是properties-data.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{
"records": [
{
"attributes": {
"type": "Property__c",
"referenceId": "18HenryStRef"
},
"Name": "Stunning Victorian",
"Address__c": "18 Henry St",
"City__c": "Cambridge",
"State__c": "MA",
"Zip__c": "01742",
"Price__c": 975000,
"Title__c": "Stunning Victorian",
"Beds__c": 4,
"Baths__c": 3,
"Location__Longitude__s": -71.11095,
"Location__Latitude__s": 42.35663,
"Picture__c": "https://s3-us-west-1.amazonaws.com/sfdc-demo/realty/house01.jpg",
"Thumbnail__c": "https://s3-us-west-1.amazonaws.com/sfdc-demo/realty/house01sq.jpg",
"Description__c": "Lorem ipsum dolor sit amet",
"Broker__c": "@CarolineBrookerRef"
},
{
"attributes": {
"type": "Property__c",
"referenceId": "24PearlStRef"
},
"Name": "Ultimate Sophistication",
"Address__c": "24 Pearl St",
"City__c": "Cambridge",
"State__c": "MA",
"Zip__c": "02420",
"Price__c": 1200000,
"Title__c": "Ultimate Sophistication",
"Beds__c": 5,
"Baths__c": 4,
"Location__Longitude__s": -71.10869,
"Location__Latitude__s": 42.359103,
"Picture__c": "https://s3-us-west-1.amazonaws.com/sfdc-demo/realty/house02.jpg",
"Thumbnail__c": "https://s3-us-west-1.amazonaws.com/sfdc-demo/realty/house02sq.jpg",
"Description__c": "Lorem ipsum dolor sit amet",
"Broker__c": "@MichaelJonesRef"
}
]
}
```


另外一个brokers-data.json,格式如下:
```json
{
"records": [
{
"attributes": {
"type": "Broker__c",
"referenceId": "CarolineBrookerRef"
},
"name": "Caroline Kingsley",
"Title__c": "Senior Broker",
"Phone__c": "617-244-3672",
"Mobile_Phone__c": "617-244-3672",
"Email__c": "caroline@dreamhouse.demo",
"Picture__c": "https://s3-us-west-1.amazonaws.com/sfdc-demo/people/caroline_kingsley.jpg"
},
{
"attributes": {
"type": "Broker__c",
"referenceId": "MichaelJonesRef"
},
"name": "Michael Jones",
"Title__c": "Senior Broker",
"Phone__c": "617-244-3672",
"Mobile_Phone__c": "617-244-3672",
"Email__c": "michael@dreamhouse.demo",
"Picture__c": "https://s3-us-west-1.amazonaws.com/sfdc-demo/people/michael_jones.jpg"
}
]
}

新建metadata package

首先新建文件夹mdapi_output_dir:

1
$ mkdir mdapi_output_dir

创建package

1
2
$ sfdx force:source:convert -d mdapi_output_dir/ --packagename package_name
Source was successfully converted to Metadata API format and written to the location: C:\Users\Administrator\my_sfdx_project\dreamhouse-sfdx\mdapi_output_dir

查看改变

1
$ ls -lR mdapi_output_dir/

部署

部署目录至沙盒环境可以定义需要跑的测试类

1
sfdx force:mdapi:deploy -d mdapioutput_dir/ -u "sandbox_username" -l RunSpecifiedTests -r test1,test2,test3,test4

或者利用 sfdx force:mdapi:deploy -d <same directory as step 1> -u 直接部署

1
2
3
4
5
6
7
8
9
10
 sfdx force:mdapi:deploy --deploydir mdapi_output_dir --targetusername username@email.com
495048 bytes written to C:\Users\ADMINI~1\AppData\Local\Temp\2\mdapi_output_dir.zip using 1693.299ms
Deploying C:\Users\ADMINI~1\AppData\Local\Temp\2\mdapi_output_dir.zip...

=== Status
Status: Queued
jobid: 0Af6F00000wgOnhSAE

The deploy request did not complete within the specified wait time [0 minutes].
To check the status of this deployment, run "sfdx force:mdapi:deploy:report"

如下代码查看部署结果

1
sfdx force:mdapi:deploy:report

转换

mdapi:convert

Converts metadata retrieved via Metadata API into the source format used in Salesforce DX projects.
转换由package.ml拉取的Metadata到DX项目文件的格式:

1
sfdx force:mdapi:convert -r mdapipackage/

获取沙盒的metadata

1
sfdx force:mdapi:retrieve --retrievetargetdir force-app  -k package.xml

或者如下代码,将生成的metadata放在force-app下面:

1
sfdx force:mdapi:retrieve --retrievetargetdir force-app  -k package.xml  --targetusername tony.ren@scmp.com.itdev

参考: