Thursday, January 31, 2019

How to ignore any source components to be pulled or push from or to Scratch Org?



If you check your local SFDX Project Directory, then you can see that there is a file named as ".forceignore". This file is basically used for ignore source component(s) tracking.

  • IGNORE SOURCE COMPONENT(S) FOR PULL OPERATION.
  1. If you want to ignore any file(s) to be pull from Scratch Org to your local project, then just copy paste your file name (with extension) in your ".forceignore" file. For example say you have create a class "TestIgnoreClass.cls" in your Org for any R&D purpose and you don't want to pull it in your local project, so just write the name "TestIgnoreClass.cls" under your ".forceignore" file.

  • IGNORE SOURCE COMPONENT(S) FOR PUSH OPERATION.
  1. If you want to ignore any file(s) to push from local project to Scratch Org, then you have to give absolute path of that file/folder [Path will be started from your root directory]. For example say you have a class "TestIgnoreClass.cls" in your local project and you don't want to push it to your scratch Org, then just write the path of that class "force-app\main\default\classes\TestIgnoreClass.cls" under your ".forceignore" file.

Note: If you have multiple classes to be ignore then just write down the file names (pull operation) / file or folder path (push operation) with extension and separate the file/folder by placing them in new line.

Tuesday, January 29, 2019

SFDX Setup Guide.

Goal of this post is to setup your SFDX environment and perform below activities,
  • DevHub setup,
  • Local workspace/project creation,
  • Scratch Org creation,
  • Pereform PUSH/PULL operation (deploy/retrieve) etc.

Step by step perform the below action items.


1. First you have to install sfdxcli. Download it from https://developer.salesforce.com/tools/sfdxcli (I have download here). Then install it in your machine and set your environment variable for your install sfdxcli.

2. Now open command prompt and run below command to check is it working properly.

sfdx -v

3. Now create a folder where you want to setup your local repository for sfdx operation. For my case say it under"D:\Assignments\Salesforce DX".

4. Now open CLI and change the directory location to above location using below command,

cd D:\Assignments\Salesforce DX

5. Now setup your DevHub org from where you want to create scratch Orgs. To setup an Org as DevHub run the below command in your CLI.

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

6. It takes few seconds and browser window will open. Put your user name and password and after successful login in browser it will show below successful message in CLI.




7. If you already login in your DevHub org, then go to setup and search for "Dev Hub" and then       enabled Dev Hub. Otherwise you will get error while try to create scratch org (explain below).



8. Now your dev hub is ready and you are ready to create your scratch org. But before create a    scratch org first create local workspace/project. And it is the location where you push or pull            your source code to your scratch org or from your scratch org respectively.
Now run the below command to create a wrokspace/project in your local directory,

sfdx force:project:create --projectname MyFirstSFDXProject


     So, a new project will be create named as "MyFirstSFDXProject" under "D:\Assignments\Salesforce DX" directory.


9. Now open the folder "D:\Assignments\Salesforce DX\MyFirstSFDXProject\config” there is a file named as "project-scratch-def.json". This json file contains scratch org information. So, you can edit this file to setup your scratch org name, edition etc.

{

    "orgName": "ArunSFDX Demo",

    "edition": "Developer",

    "orgPreferences" : {

        "enabled": ["S1DesktopEnabled"]

    }

}

10. Run the command in CLI to change the directory to your project directory which is created in point 8.

cd MyFirstSFDXProject


11. Now check your Dev Hub connection and list of scratch orgs related to this dev hub. Currently we don't have any scratch org. Run the below command for checking,
sfdx force:org:list



12. Create Scratch Org (Use the above JSON as mention in point 9) and run the below command. Here I have given my scratch org alias name as "MyScratchOrg1".
sfdx force:org:create --setdefaultusername -f config/project-scratch-def.json --setalias MyScratchOrg1

Note: If you miss to enable Dev Hub as mentioned in point 7, then you will get the below error.  So, enable your dev hub as mentioned point 7 and run the command again.




13. If the above command run successfully then CLI will show your scratch org name.


Response message:
Successfully created scratch org: 00DN0000000REujMAG, username: test-f2bwflahpmax@example.com

14. Now like point 11, you can check the dev hub connection and it's scratch org list. Currently we have one scratch org. Run the same command as mentioned in point 11.
sfdx force:org:list



15. Open your scratch org by below command (No password is here).
sfdx force:org:open
or
sfdx force:org:open -u org_alias

 (for my case it is sfdx force:org:open -u ArunSFDX Demo)



System will automatically open the scratch Org in your default browser or you can open the ORG by copy paste the displaying URL in CLI.


16. You can generate password for your scratch org.
sfdx force:user:password:generate




17. You can see the password / org details again by running below command.
sfdx force:user:display -u scratch_org_user_name
For me it like,

sfdx force:user:display -u test-f2bwflahpmax@example.com



18. Check Company Information and it is same as you mentioned in your JSON


19. As this scratch org is developer org so the storage limit is low and you can check it from “System Overview”.


20. Now try to deploy some source code in the scratch org.
  • Open your project folder (D:\Assignments\Salesforce DX\MyFirstSFDXProject) and here you get a json file named as “sfdx-project.json”.
  • Open the json file and there you get the source directory folder path, where you put your source code to deploy.

{
  "packageDirectories": [

    {

      "path": "force-app",

      "default": true

    }

  ],

   "namespace": "",

"sfdcLoginUrl": "https://login.salesforce.com",

  "sourceApiVersion": "44.0"

}
  • Suppose I want to deploy some class. So create “Classes” folder and here put your classes and their metadata. Folder and file structure is same as used on the time of ant deployment and only difference is here being no package.xml is required.
  • For our case say, I want to deploy “ToolBean.cls”.


  • Currently there is no class under scratch Org.

  • Now use below push command for deployment.
sfdx force:source:push


  • Check Org for this deployment,


  • Now suppose I have created another class (ArunSFDXDemoClass.cls) and a lightning component (ArunSFDXDemoComponent.cmp) and also modify existing class (ToolBean.cls) in scratch org and finally I want to pull it from scratch Org.



  • Now run the below command to pull the above new and modified source code.
  • sfdx force:source:pull
  • Before pull

  • After pull

  • Under classes folder


  • Under aura folder



21. Once you complete your work then logout from org. Below are the logout command,

sfdx force:auth:logout -u test-f2bwflahpmax@example.com

LWC to LWC Communication using Lightning Messaging Service (Part - 2)

In my previous post ( previous post link ) we have learn how to create a Lightning Messaging Service with 6 steps and today we will use the ...