Salesforce DX Absolute Beginner ( Part 7 Of 7 )

In this seventh part of the salesforce DX series, I am going to show how to use salesforce DX data commands to migrate the sample data from the scratch org to sandbox for testing or you can create data into the scratch org from the CLI.
Use force:data:soql:query command to Executes a SOQL query as shoe below.This Command will help if you wanted to retrieve data into CLI from scratch org without login. Use -t for tooling API 
sfdx force:data:soql:query -q "Select id , Name ,Passenger_Code__c,Reg_Number__c ,Vehicle_Id__c from Vehicle__c"

sfdx force:data:soql:query -q "SELECT Name FROM ApexTrigger" -t

 

Export Command 
 Exports data from an org into the sObject tree format  as shown below
sfdx force:data:tree:export -q "select Id,Name, Passenger_Code__c, Reg_Number__c, Vehicle_Id__c,(Select Id,Name,Office_Id__c,Violations_Id__c from Violations__r) from Vehicle__c " -x export-demo -d /force-app/data
 "records": [
        {
            "attributes": {
                "type": "Vehicle__c",
                "referenceId": "Vehicle__cRef1"
            },
            "Name": "V1",
            "Violations__r": {
                "records": [
                    {
                        "attributes": {
                            "type": "Violations__c",
                            "referenceId": "Violations__cRef1"
                        },
                        "Name": "VO0",
                        "Office_Id__c": "3278640",
                        "Violations_Id__c": "2387627836120"
                    },
                    {
                        "attributes": {
                            "type": "Violations__c",
                            "referenceId": "Violations__cRef2"
                        },
                        "Name": "V1"
                    }
                ]
            }
        },
......
]

 

Import Command 
 Imports data into an org using the SObject Tree Save API. This data can include master-detail relationships. The following command imports the data into Salesforce objects by using SObject Tree.
sfdx force:data:tree:import -f Vehicle__c-Violations__c.json

Create/Update/Delete Commands 
 You can use CRUD commands to manipulate the data the CLI as shown below.
Below example showing the how to inserts account record from the CLI.
sfdx force:data:record:create -s Account -v "Name='Universal Containers' 
Website=www.example.com"
Below example showing the how to delete account record from the CLI.
sfdx force:data:record:delete -s Account -i 001D000000Kv3dl
Below example showing the how to update account record from the CLI.
sfdx force:data:record:update -s Account -i 001D000000Kv3dl -v "Name=NewAcme"
Bulk Commands 

 You can use Bulk commands to delete or upsert the data into Bulk API Style.

The Following commands Create a job and one or more batches for inserting new rows and updating existing rows by accessing the Bulk API.
sfdx force:data:bulk:upsert -s MyObject__c -f ./path/to/file.csv -i MyField__c

Above Command Inserts or updates records from a CSV file with one or more batch based on the CSV, file size and Returns a job ID and a batch ID. Use these IDs to check job status with data:bulk: status as shown below.

sfdx force:data:bulk:status -i 750xx000000005sAAA

 

Below are some of the useful reference about salesforce DX

  1.  Salesforce DX Trailhead
  2. First Impressions with Salesforce DX by Peter Chittum
  3. Getting Started With Salesforce DX

Salesforce DX Absolute Beginner ( Part 6 Of 7 )

In this sixth part of the salesforce DX series, In this post, I am going to explain how to migrate changes from the scratch org to salesforce sandbox for testing.  When you’re ready to validate your source, convert your Salesforce DX source to Metadata API source format. You can then deploy to a sandbox.You can deploy or retrieve up to 10,000 files at once. The maximum size of the deployed or retrieved .zip file is 400 MB (39 MB compressed). If either limit is exceeded, the operation fails.You can increase the efficiency of your sandbox and production deployments by using tests you’ve already done in the scratch org. Run only the tests that are required, such as tests for Apex classes and triggers that change for the deployment. To run only specified tests when you deploy, set -l to RunSpecifiedTests and use -r to specify a comma-separated list of tests for deployment-specific changes to your Apex code.
  1.Pull Changes from Scratch Org 

To ensure that your project is up to date, synchronize the source in your local file system with your development scratch org by running the pull command.

sfdx force:source:pull

2.Login to the sandbox

Login into the sandbox by running the following command. Set alias name so that you can refer it easily for further.

sfdx force:auth:web:login --setalias my-dev-sbx --instanceurl https://test.salesforce.com

3.Convert Salesforce DX project into metadata project

create the directory for your source and convert the source to Metadata API format as shown below

mkdir mdapi_output_dir
sfdx force:source:convert -d mdapi_output_dir/ --packagename relase1.2.1

4.Deploy Metadata into Sandbox 

Deploy the metadata from the directory to the sandbox, specifying deployment-specific tests as needed by running this command.

 sfdx force:mdapi:deploy -d mdapi_output_dir/ -u my-dev-sbx -l NoTestRun

If your deployment exceeds the wait time before it completes, use force:mdapi:deploy:report to check the deployment status. The default wait time is 0 minutes. Use the –wait parameter to specify a longer wait time.

For example, to check a deployment job and add 5 more minutes to the wait time
sfdx force:mdapi:deploy:report --jobid 0Af2F00000HVyCbSAL --targetusername my-dev-sbx

Quick Recap of part 6 before going to part 7

  • Pull Changes from scratch org
  • Login to sandbox
  • Convert DX project to salesforce metadata.
  • Deploy changes to the sandbox.

Salesforce DX Absolute Beginner ( Part 5 Of 7 )

In this fifth part of the salesforce DX series, I am going to show how to set up the Salesforce DX from the existing source base without creating the package.In this post, I am extracting the metadata from the salesforce into the eclipse project. We will use this project metadata to convert into the salesforce DX specific format.

1. Retrieve the Metadata

Create an eclipse project to extract the metadata from the salesforce.

 2. Login into Dev Hub from CLI 

Run the force:auth:web:login CLI command. If you are authorizing a Dev Hub org, use the –setdefaultdevhubusername parameter if you want the Dev Hub org to be the default for commands that accept the –targetdevhubusername parameter.

sfdx force:auth:web:login --setdefaultdevhubusername --setalias my-sfdchub-org-pkg

3. Create a DX Project

A Salesforce DX project has a specific structure and a configuration file that identifies the directory as a Salesforce DX project.Use the force:project:create command to create a skeleton project structure for your Salesforce DX project.

sfdx force:project:create --projectname DXEx
cd DXEx

4. Export The Package.xml

Export the package.xml file into the temporary directory. Type the following commands in the root folder of your Salesforce DX project:

sfdx force:mdapi:retrieve -r ./mdapipkg -u my-dev-org1 -k  C:\Users\rvakati\workspace\RajV\src\package.xml

Unzip the unpackaged.zip file  which is created from the above command  and optionally delete the unpackaged.zip file

 5. Convert Source code to Salesforce  DX 

Convert the source code to the Salesforce DX project structure by running the following commands

sfdx force:mdapi:convert --rootdir mdapipkg --outputdir force-app
6. Create a Scratch Org for Development

 You can create scratch org by Run the following commands.

sfdx force:org:create -f config/project-scratch-def.json --setdefaultusername --setalias my-dx-scratchorg-pck
7.Push Metadata into the scratch org
You can push the extracted metadata into the scratch org by running the below command into the scratch org.
sfdx force:source:push

8.Open scratch org

You can open scratch org by running following command.

	sfdx force:org:open

9. Implement the new features

Now if you wanted to make any changes you can do in the scratch org . In this example, you can create an apex class by running this command.

sfdx force:apex:class:create  --classname DemoClass --template DefaultApexClass --outputdir force-app/main/default/classes/
10.Push Metadata into the scratch org
You can push the changes into the scratch org by running the below command.
sfdx force:source:push

Quick Recap of part 5 before going to part 6

  • Create an Eclipse Project to extract metadata from the salesforce
  • Convert package.xml file into Salesforce DX format.

 

Salesforce DX Absolute Beginner ( Part 4 Of 7 )

In this fourth part of the salesforce DX series, I am going to show how to set up the Salesforce DX from the existing source base by creating the temporary package.  If you are already a Salesforce developer or ISV, you likely have the existing source in a managed package in your packaging org or some application source in your sandbox or production org. Before you begin using Salesforce DX, retrieve the existing source and convert it to the Salesforce DX project format.When you convert existing Metadata API source to Salesforce DX project format, we create an XML file for each bit. All files that contain XML markup now have an .xml extension. You can then look at your source files using an XML editor. To sync your local projects and scratch orgs, Salesforce DX projects use a particular directory structure for custom objects, custom object translations, Lightning components, and documents.

1. Retrieve the Metadata

Create an unmanaged package in your existing org, and include all the component you want to move to your Salesforce DX project. You can do it by login into the salesforce dev or sandbox.

 2. Login into Dev Hub from CLI 

Run the force:auth:web:login CLI command. If you are authorizing a Dev Hub org, use the –setdefaultdevhubusername parameter if you want the Dev Hub org to be the default for commands that accept the –targetdevhubusername parameter.

sfdx force:auth:web:login --setdefaultdevhubusername --setalias my-sfdchub-org

3. Create a DX Project

A Salesforce DX project has a specific structure and a configuration file that identifies the directory as a Salesforce DX project.Use the force:project:create command to create a skeleton project structure for your Salesforce DX project.

sfdx force:project:create --projectname DXMigration
cd DXMigration

4. Login into Salesforce Source Org 

Now you need to login into the salesforce source org from where you wanted to fetch metadata into the salesforce scratch org. Use below command to connect to the developer account or sandbox.

sfdx force:auth:web:login -a my-dev-org

5. Export The Package 

Export the unmanaged package metadata in a temporary directory. Type the following commands in the root folder of your Salesforce DX project:

mkDir temp 
sfdx force:mdapi:retrieve -s -r ./temp -u my-dev-org -p DXExport	

Unzip the unpackaged.zip file and optionally delete the unpackaged.zip file

 6. Convert Source code to Salesforce  DX 

Convert the source code to the Salesforce DX project structure by running the following commands.

cd ..
sfdx force:mdapi:convert -r ./temp

 

7. Create a Scratch Org for Development

       You can create scratch orgs for different functions, such as for feature development, for development of packages that contain a namespace, or for user acceptance testing.Run the following commands to create a scratch org .

sfdx force:org:create -f config/project-scratch-def.json --setdefaultusername --setalias my-dx-scratchorg
 8.Push Metadata into the scratch org
You can push the extracted metadata into the scratch org by running the below command.
sfdx force:source:push

9: Open scratch org

You can open scratch org by running following command.

sfdx force:org:open

10. Pull Changes from Scratch Org, if any needed 

sfdx force:source:pull

11. Implement the new features

Now if you wanted to make any changes you can do in the scratch org . In this example, you can create an apex class by running this command.

sfdx force:apex:class:create  --classname DemoClass --template DefaultApexClass --outputdir force-app/main/default/classes/
push changes to scratch org again after development by running the following command.
sfdx force:source:push

Quick Recap of part 4  going to part 5
  • Salesforce DX  Project setup
  • Converting metadata into DX specific format
  • Making changes and push to scratch org