Showing posts with label Salesforce DX. Show all posts
Showing posts with label Salesforce DX. Show all posts

Friday, 8 November 2019

Import and Export Data using SalesforceDX (SFDX)

Import and Export Data using SalesforceDX (SFDX)


In this post we will talk about Data migration using SalesforceDX. We have a vary simple use case to Import and Export data from one Salesforce Org to another Salesforce Org. We know we can do the same with dataloader or some other custom tool. But in this post we will talk about how to import and export with SalesforceDX (SFDX). If you are new to SalesforceDx please check this ApexHours Session Recording by Jitender Zaa.


How to export records with SFDX


 Step 1) First Login to your Salesforce Org from where you want to export data

sfdx force:auth:web:login --setalias myDev1

Step 2) Use below command to export the data

To Export the data we need to use "sfdx force:data:tree:export" command and we need to pass one Soql query. You can also pass the path of SOQL query file with the help of -q. Lets export the Account and its all related contacts records.

sfdx force:data:tree:export -q "SELECT Id,Name,(Select FirstName,LastName from Contacts) FROM Account limit 2" -d ./data -p -u myDev1

Here
-q : SOQL query or file path of soql query.
-d : directory, where exported json file can be saved
-u : Salesforce user to perform operation
-p : generate multiple sobject tree files and a plan definition file for aggregated import


Once you execute above command it will create three file for you in Data Folder.
  • Accounts.json
  • Contacts.json
  • Account-Contact-plan.json

How to Import data using Salesforce DX


First login to salesforce org in which you want to import the data. Then use below command to Import records in Salesforce instance, which we just exported in previous step

sfdx force:data:tree:import -p data/Account-Contact-plan.json -u myDev1

Where
-f : JSON file to be imported
-p : plan file
-u : Salesforce user to perform operation

The query for export can return a maximum of 2,000 records. For more information, see the REST API Developer Guide:
 

NOTE: For above use case you don't need any scratch org. You can simply login in any sandbox and migrate your data between two org's.


There is another Use case where we can use SalesforceDx for Import and Export Data.

Use Case: If you are implementing Continuous Integration (CI/CD) solution built for Salesforce, Some time you need to upload sample data into Salesforce.

Solution: For that you need to use command line dataloader or custom tool built to import export data automatically. We can use SalesforceDX for data import and export as well for this requirement.


Related Post :



Further Reading:

Thanks,
Amit Chaudhary

Saturday, 8 December 2018

SalesforceDX for Non-Scratch Org | SalesforceDX with Sandbox, Developer Orgs | SFDX


SalesforceDX for Non-Scratch Org


Want to Setup SalesforceDX (SFDX) for Sandbox, Developer and Production Orgs ?


Step 1) Install Salesforce CLI (SFDX).


Step 2) Install VSCode and Salesforce Extension for VSCode. Check our last post for same.

Step 3) Add "Salesforce CLI Integration" Extension in VSCode.


Step 3) Create SFDX Project.

Click Ctrl +Shift + P and then type SFDX . Then select "SFDX: Create Project with Manifest" option


After that Enter the project Name and select Folder where you want to save your project.


Step 4) Authenticate your Sandbox/Developer org.
  • 4.1 ) If you want to authenticate your sandbox then please change URL in below file "sfdx-project.json"


For Sandbox :-
"sfdcLoginUrl": "https://test.salesforce.com",

For production or Developer org
"sfdcLoginUrl": "https://login.salesforce.com",

  • 4.2 ) Then Authorize your org
 Click "CTRL+SHIFT+P" and then select "SFDX: Authorize an Org" and provide Org Alise Name



or you can execute below command for same if you are using CMD.

sfdx force:auth:web:login --setalias MyDevOrg --setdefaultusername

NOTE:- VSCode will also execute the above command automatically in Salesforce CLI Terminal

Step 5) Update your package.xml file.
 
Open Package.xml file from Manifest folder. Then Modify your Package.xml file to add and remove component.


Step 6) Retrieve Component  

Click on Package.xml and select "SFDX: Retrieve This Source from Org" option.


or you can execute below command for same if you are using CMD.
sfdx force:source:retrieve --manifest d:\Amit\MyDevOrg\manifest\package.xml

NOTE:- VSCode will also execute the above command automatically in Salesforce CLI Terminal like below

Step 7) Deploy changes in Org.

Now do your changes in VSCode and then select the file which you want to deploy in your org. Then Right click on VsCode and select "SFDC: Deploy This Source to Org" option for deployment.


or you can execute below command for same if you are using CMD.
sfdx force:source:deploy --json --loglevel fatal --sourcepath 
d:\Amit\MyDevOrg\force-app\main\default\classes\AccContExtTest.cls


NOTE:- VSCode will also execute the above command automatically in Terminal

 
Recently we did session in "Apex Hours" on "SalesforceDX for Non-Scratch Org" with Mohith Shrivastava. Thanks Mohith for a great session. Here is recording for same session




Please check this recording if you want to learn how to setup VsCode for Salesforce.

Thanks,
Amit Chaudhary

Capture.JPG  @amit_sfdc    @ApexHours
  Salesforce Apex Hours 
    #SalesforceApexHours 

Thursday, 4 January 2018

Salesforce DX | Change a Password for a Scratch Org in Salesforce

Change a Password for a Scratch Org User




In our last post we learn about how to install Salesforce DX and how to create Scratch org with salesforce DX,

What is Scratch Org ?

A Scratch org is a dedicated , configurable and short term Salesforce org that you can quickly spin - up when starting a new project , new feature branch and feature Test.


Create Password for Scratch org

When we create a scratch org using command line it does not show password. But if you need  password for your scratch org then try below command.

Step 1) Generate password

sfdx force:user:password:generate -u <Alise_Org_Name>


sfdx force:user:password:generate -u MyFirstScratch






Step 2) To see the password again.

sfdx force:org:display -u MyFirstScratch




Please share your feedback and comment


Thanks
Amit Chaudhary
@amit_sfdc


Tuesday, 2 January 2018

Salesforce DX | How to Setup Salesforce DX | Creating Scratch Org

Get Started With Salesforce DX
In this post we will talk about how to setup Salesforce DX, How to enable the Dev Hub and create Scratch org. Lets understand what is scratch org ? A scratch org is a dedicated short-term Salesforce environment for development and configuration. Scratch orgs drive developer productivity and collaboration during the development process, and facilitate continuous integration. If you want to learn about SalesforceDx for non-scratch org then please check this post.

Lets get Started with SalesforceDX (SFDX.)

Step 1) Enable Dev Hub


Enable Dev Hub in your org so you can create and manage scratch orgs. You can enable the Dev Hub in any Developer Org, Production and developer Hub Trail Account. Follow below setup to enable dev hub.
  1. Login to your Developer Edition, production or trail org. Then click on Setup.
  2. Search Dev hub in Quick Find and click on Dev Hub.
  3. Then Click on enable button.


Step 2) Install Command Line Interface

    1) Download the CLI from below link and install that.
    2)  In a command window, enter sfdx and validate SFDX is insalled properly.


Step 3) Setup Dev Hub and Set Alias


    1) Login on Dev Hub :  Execute below command in command prompt to authorize the Dev hub with web login flow 

    sfdx force:auth:web:login -d -a DEVHUB
       -d to set Default org
       -a to set alias for the org
 
DEVHUB login

    Then it will take you to wed browser to login. Then login with your salesforce Dx Account. After login you will get below screen.


        2) Open your DevHub Org: After that you can login with alise Name DEVHUB any time with below command.

    sfdx force:org:open -u DEVHUB

        3) Login in Sandbox: If you want to login in sandbox you can try below command as well

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

        4) View All org: if you want to see all org then try below command
    sfdx force:org:list


Step 4) Create Scratch Org:-



    1) Create Project : Before creating s scratch org we need to create one SFDX base project. Please execute below command to create DX project.

   sfdx force:project:create -n c:\Amit_Salesforce_DX\MyProject

above command will create a folder like below

SFDX Folder Structure

Lets understand the use of each files :-

File
Detail
sfdx-project.json
Configures the source that this local project manages
config/project-scratch-def.json
Determines the configuration of a scratch org, including which features and preferences define its org shape
force-app
This folder will contain project


    2) Create Scratch Org : Go to project location then create scratch org with below command

    sfdx force:org:create -s -f config/project-scratch-def.json -a MyFirstScratch

    -s used for scratch org
    -f path the project scratch org confirgutaion File.
    -a alias

    Once Scratch org will ready then you can also see same org in salesforce as well.


   Now your Scratch org is ready. Start your Development in scratch org.

    3) View Scratch Org Configuration: Exeucte below command to see newly created scratch org.

     sfdx force:org:display -u MyFirstScratch

    4) Open Scratch Org Configuration: Exeucte below command to see newly created scratch org.

     sfdx force:org:open -u MyFirstScratch
    
     5) Change password for Scratch org: if you want to change the password for scratch org. Please check this post.
  
   


If you want learn about how to upload the data in Scratch org with Salesforce DX. Please check this post.


If you you looking for Video and PPT. Check here in Salesforce Apex Hours.

Please share your feedback and comment


Thanks,
Amit Chaudhary
@amit_sfdc