SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Monday, 25 December 2017

Celebrate DXmas with Salesforce DX

Merry Christmas...!!

In this post I am going to talk about Salesforce DX. So, after the celebration of Christmas, let's celebrate DXmas together by learning what Salesforce DX is and how can we use it to increase our productivity.

Well, the first question that comes in your mind is What is DX in Salesforce ? 
DX stands for Developer Experience. Salesforce focuses on enhancing the developer experience through this new feature. There are some specific terms that you will hear every time you learn about Salesforce DX, whether it's during a live demo session or reading about it in a blog post like mine. Let's learn about those terms first so that we'll be aware of these whenever we'll hear about them in future.

1. Source of Truth

A source of truth is simply a source for accessing and modification of a data element. Till now we have worked on Org Based Development Model in which every data related to a project resides in an Org. So, earlier our development org was our Source of Truth but the concept of Salesforce DX focuses on the point that your VCS (Version Control System) repository in which your code resides is your source of truth. That can be a repository on Github, Bitbucket  or any other VCS that you use to prefer Salesforce supports all VCS. Salesforce DX focuses on Artifact-Based development in which we divide the whole project into different applications/features and then we develop and release each feature individually as a single artifact.

2. Artifact 

An artifact is simply a group of related code and customizations. It can be developed, tested and released independently as well. The point to be noted in that while talking about a salesforce project, the metadata components that reside in one artifact cannot reside in any other artifact at the same time. This means that we have to make relationships between artifacts if other artifact components require metadata from a different artifact. It's similar to as we make a base test class that can be used by other test classes for the creation of data.

3. Scratch Org

You'll hear about scratch org in each and every article related to Salesforce DX and even at every session you attend. So the question here is - What is a Scratch Org ? Scratch Orgs are simply Salesforce Development Environment in which you do the development. As salesforce mainly focuses on artifact based development, so it is preffered to keep all your code and customizations for a particular artifact in a single scratch org. and make regular commits to your repository. The important point to note about a Scratch Org is it's life span is only for 7 days. Salesforce has make such decision because it wants you to store your code and customizations into your VCS instead of org as your VCS Repo is not your Source of  Truth. However, if you have such requirement, like you are working on any big feature, you can extend your scratch org life span to 30 days at max.

4. Developer Hub Org

A developer hub org is the place where you can create and manage your scratch orgs. You can signup for a 30 day trial of your developer hub org here. It is similar to a Salesforce Org but has the capability to create and manage all your scratch orgs. If you have a production org or a business org, you can enable devHub from there just follow these steps:- 
  •  Go to Setup
  • Search for Dev Hub
  • Click Enable
So, in this way you can enable a Dev Hub for your production or business environment. Once you enable the dev hub, you can't disable it. Here is how your DevHub Org looks like:- 


As you can see above, you can easily view and manage all your scratch orgs from here. But we are developer and we love to work on CLI right ? Don't worry, Salesforce has that option for you too.

5. Salesforce CLI

Salesforce CLI is a command line interface using which you can use Salesforce DX and all it's command to interact with your scratch org. You can download it here. and install it on your system.

6. Extensions

Salesforce has provided a number of extensions for various IDE so that they can be DX compatible. What I personally recommend is to use Visual Studio Code for the same. Here you can easily work on your apex code as well as you can use DX commands to push or pull your metadata from your org and your VCS too through general VCS commands. You can download the Visual Studio Code here.
After installing, you can add an extension to your Visual Studio Code from here. This extension will make writing apex code easy and also you can interact with your scratch orgs in a much flexible way.

So, now you are perfectly ready for making a move towards Salesforce DX. So, I recommend you to go through the document in this link:- Hands-on Workshop: Using Salesforce DX for Lightning Development. Its the step by step approach on how you can use Salesforce DX for developement by Shashank Srivatsavaya (Developer Relations Manager/Evangelist, India). After that you can go through some other important links I am mentioning below:- 

  1. App Development with Salesforce DX - Trailhead
  2. Continuous Integration Using Salesforce DX - Trailhead
  3. Salesforce DX Setup Guide - Salesforce Docs
Thanks for reading. You can suggest more topics that you want to learn and I'll try my best to make you understand those in the easiest possible manner. You can reach out to me by messaging here directly or onto my twitter handle @rahulcoder

Have a great Christmas and a Happy New Year 2018

Saturday, 16 December 2017

Analyze your Apex code using PMD Source Code Analyzer

As a salesforce developer, we all write apex code or even if you are an admin, there are some conditions when you have to write code or even some triggers. So, isn't it awesome if you can write code that's perfect and follow the industry standards ?

Consider a situation when you have to solve a problem to get a particular task done and you start writing your apex code for that. After some time, you finally completed your code. You analyzed it using a free tool and it is telling you that here are the points where your code is not following the standards or it is not properly optimized. Now what ? Your next step will be asking your friends or take the help of Trailblazer Community to make your code perfect and optimized. As a result, it not only improved your code performance but also improved your skills. So, that in future if somebody looks at your code, He/She will be impressed by looking at your perfect style of writing code. Great !

What is PMD ?

PMD basically stands for Programming Mistake Detector. It is a source code analyzer that finds common mistakes in your code like:- unused variables, if-else statements without braces, naming conventions of methods, soql in loops and many more. It also includes CPD which stands for Copy/Paste Detector which finds duplicate code in your files and make you aware so that you can improve to make your code reusable.


Setting up PMD

You can download the latest version of PMD as a zip file from the PMD Homepage by clicking the download link or you can scroll down to the downloads section and download any other version from there. Currently, the latest version is 6.0.0 which was released yesterday. You can download it directly here.

After downloading the zip file you have to extract it and it will give you two folders namely bin and lib apart from a LICENSE file. I am currently using a windows operating system so going to tell you the commands according to that. If you are using a Linux/Unix OS you can read from here and then follow the similar commands according to your OS from the getting started page.

In the bin folder, we get a number of batch files out of which - pmd.bat and cpd.bat files are the one which we'll be mainly using for using pmd. As PMD is a command line tool, so if you want to use it like from anywhere on your system, I suggest you to add pmd bin to your system path in the environment variables so that it can be accessed from anywhere on your system. That's all for setting up let's learn how to use PMD.

Analyzing code using PMD

Now you have installed PMD and you want to analyze your code. PMD allows you to analyze code written in a number of languages like:- java, javascript, xml, apex etc. We'll mainly focus on the apex code here. So here is the general command syntax to analyze the apex class or a directory consisting of a number of classes as follows:- 


The release 6.0.0 is the latest and is a major release so even if you are already using PMD, this release consists of a number of changes that you must be aware of. Even the apex rules categorization is updated and the old rule-sets like:- Braces, Complexity is deprecated now. You can know about the latest apex rules in the current release of PMD here. The table of contents present on this page have the same name as the xml files present in the pmd library implementing the rules under that name. Below is the example command using the default rules already present in pmd library for apex. It is using the design.xml rule file under the apex category following the name of the rule that you want to check.You can run the command without any rule name to use all the rules present in that xml file.

This command is checking that in all the apex classes present in the classes folder. If there is any class which is using methods with excessive parameters. The excessive parameter rule is present in the standard library and you can view it here. So, when I run this command as I have set the format as html and the output file is named output.html, I get the file named output.html in the current directory where the command is executed as I have not given any path for output.html but you can keep a separate path for output report files.
The report created by pmd for me is an html file which looks like this:-

The report gives you the file name with path, the line number that consists of error as well as the problem which is the direct link to the original doc where the sample error code is there to which your code resemble leading to error. When I opened my test.cls then this was the code snippet giving me error:-

As you can see this method consists of a number of parameters whereas it is preffered that you use less parameters in your method. So this is how you can use pmd with some general standard rules defined in the library to check whether your code quality is good or not.

Apart from people working as a developer or admin, if you are a technical architect or even the owner of your company. Then obviously you want people working under you to write perfect code and maybe follow only a bunch of standard rules that you think are most important. Don't worry PMD has taken care of that too.

You can make your own rule file in which you can define a bunch of standard rules and then distribute it to other developers so that they must follow the standards while writing code.
You can see in the file below that I am using three common ways by which you can add rules to your rule-set file.
  1. By using a single rule from a rule-set.
  2. By using full rule-set present in the library.
  3. By using full rule-set but excluding some of the rules from it.
In this way, you can make your own rule-set file and you can use that file as follows:-


Try the above command by copying the rule-set file given above or making your own rule-set file and start analyzing your code.

CPD - Copy/Paste Detector

CPD is another feature of PMD that you can use to analyze the re-usability implemented in your code. It scans your files and detects duplicate code for you. The general syntax for the CPD command is as follows:-

Below is the example command for the above syntax that I used to find duplicate code in my files:-

I have set the minimum tokens to 5 and specifically used my test.cls as I have added a lot of duplicate code in that. So, you can see the output from cpd below:-


As you can see above, I have duplicated System.log('function with excessive parameters'); and also the doWork() by writing it as doMoreWork() with multiple integers, as the number of minimum tokens I specified in cpd command are very less i.e. 5 so cpd is reporting it as duplicate code.

Even if you don't like to use command line, PMD has made a cpdgui that you can use to find duplicate code in your directory and you can even simply export your report to text, xml or csv formats.
Just go to the command line into your bin folder and type cpdgui.bat and you will see an application like this:-


In the application, select the folder in which your classes are present and change value in report duplicate chunks larger than field the default value is 75. It is similar to minimum tokens you have in cmd. Select the language as apex and click on go. You have a report like the one shown below and you have the option to export that too.


If you want to learn more about CPD you can checkout the docs here. So, now you learned how you can analyze your code usind PMD Source Code Analyzer and also how you can detect duplicate code using CPD - Copy Paste Detector tool from PMD.

Let's start using PMD now and be sure that you always write quality code.

Sunday, 10 December 2017

Develop Lightning Components faster using Lightning Data Service

Lightning Data Service allows us to read, create and edit a record without requiring any apex code. There are a number of cool features of Lightning Data Service and I am going to go through some of those features right now:-
  • By using Lightning Data Service you can easily make lightning components that deal with a single record and embed that component to the detail page of that record. 
  • You can even make reusable lightning components that deal with similar data so that they can be reused at multiple record detail pages. 
  • One big advantage of Lightning Data Service is that you don't have to make any apex controller. 
  • Suppose you have two components that work on the same data - one is updating the record and other is displaying the record so as you update the record, the component that is displaying the record is updated automatically and you don't have to take care of any event handling in this case as you normally do in Lightning Components.
  • There are some considerations also that we have to take care while using LDS one of which is that you can only query a single record and that too by Id of that record only while using LDS.
During the starting of this month, I took a session on Lightning Data Service with Noida Salesforce Developer User group at Dreamforce Global Gathering 2017. I have made a presentation for the same and you can view it here and have a more clear idea about the LDS.


During my session, I made multiple lightning components, each for one operation in CRUD. Out of which I gave the demo of read, update and delete operation only but I have the whole code for each operation including create. So let's discuss about each component first and then I'll handover the code to you.

  • Read (LDSComp) - This component displayed the name of the contact.
  • Edit (LDSCompEdit) - This component allows you to edit the contact's first name and last name.
  • Delete (LDSCompDelete) - This component allows you to delete the contact.
  • Create (LDSCompNew) - This component allows you to create a new lead.
There is one more component i.e. LDSWrapperComp in which I wrapped all the components so that I can embed all components together as a single component. The final wrapper component look as follows when you embed it in the record detail page:- 


So, here you can see, I have made four components for each of CRUD operation. What I did is I made this wrapper component reusable. But how ?? As you know that in our Lead as well as Contact both consists of first name and last name fields, so I made a single component and embedded it in both the Lead and Contact detail pages. But as you can see in the last component, I have named it as Create New Lead. This is because, when you create a new record using LDS, you have to give the SObject Type as you load the component initially and I have given my SObject Type as Lead so, in this case whether you embed it in any detail page, it'll create a Lead only.

So let's jump on to the code. You can download the whole code of this LDS demo from here.
If you want to see whole code step by step, you can clone the github repo on your system and then move step by step as I have created 4 different branches in which in each branch one extra component is added to the wrapper component.
You can switch between all 4 branches in the following order:-
  1. read
  2. update
  3. delete
  4. create
More better approach can be if you have setup MavensMate with sublime text to connect with your org, as in that case you have to just create empty components in your actual org and you can go into your org's folder and can set the remote url to my github repo and then switch to particular branch do a git pull and you have the code. But to sync the code to your dev org, you have to make the Lightning Components with same name in your org and then refresh from the server to get those components and then switch to branch and save each file. You may have to delete the .lightning file from the config folder if you are not able to sync the code on saving the file.

If you want to learn how to setup MavensMate you can refer to my other blog post How to setup MavensMate with your favorite text editor for Salesforce.

So, make sure you have tried all the crud operations of LDS by using the code given in the repo. If you have any doubts or problems you can comment below or contact me directly on my twitter handle which is - @rahulcoder

Saturday, 9 December 2017

How to setup MavensMate with your favorite text editor for Salesforce


Sublime Text is my favorite text editor and maybe yours also. But even if you prefer any other text editor, no need to worry as I am going to tell you how to setup MavensMate IDE for salesforce with your favorite text editor. Primarily MavensMate supports:- Sublime Text, Atom or Visual Studio as of now so make sure you choose any one of these.
So, let's move forward step by step:-

Install Sublime Text

Go to https://www.sublimetext.com/ and click on download from the navigation menu at the top, it'll take you to the download page.


Download the file according to your OS and install that.

Setting Up MavensMate

1. Install MavensMate

To install MavensMate, you have to go to it's github repository and in the releases, you can download any version you want to. Once you are on the releases page, you'll see something like this:- 

Click on this link:- https://github.com/joeferraro/MavensMate-Desktop/releases to go directly to the releases page and download the file according to your system. You have different extensions available like:- exe for Windows, dmg for Mac or deb for linux. Download and install it.

2. Create Project

Once you'll open MavensMate, you will see this screen:- 


  • Click on New Project.
  • Select the Environment Type. It can be production, developer, sandbox, prerelease or a custom URL.
  • Click on Connect Button.
  • Login with your Salesforce Credentials.
  • When you login, it may ask for permissions for making changes allow it to do so.
  • Click on the allow button and it'll take you to a new screen where you have to setup your project configuration.
  • Fill up the project details and then click on the advanced tab.
  • Here you'll see you have the information about the metadata that you are fetching from your org. By default some metadata is selected for you like:- ApexClass, ApexTrigger, ApexPages etc. You can add anything you want from Org Metadata to Project Metadata Subscription like here I have added AuraDefinitionBundle to this metadata so that I can fetch and work on Lightning Components as well.
  • Leave the Metadata tab as it is for now and click on Create Project.
  • It may take some time and then you'll end up to the home screen of your project.

  • Cick on Edit Project.
  • On the Edit Project screen, click on Project Metadata Tab, Click on Select All button and click Update Project.
  • It may take some time and then you have your project updated successfully with all the files from your org.
  • Once you have your project updated, you can click on the 2nd last button from the left menu and open the project in Sublime Text or any text editor you have installed.

  • If you are not getting your text editor in here, you can click on settings from the top right corner and you can check and update the installation path of your text editor.
  • You can edit the path here and you may have to restart MavensMate application or your system to see the effect.
As, you have already installed sublime text successfully, let's go on and configure it for MavensMate.

Configuring Sublime Text for MavensMate

  • Go to Tools. Click on Command Pallete.
  • If you have already Package Control installed then no problem, otherwise type package control in the command pallete and install that first.
  • Once you have installed package control, again open the command pallete and type install package and press enter when you see Package Control: Install Package highlighted.
  • It will browse the repositories and fetch it up for you.
  • Once you have the list, type MavensMate and press Enter.

Great...!! You have now MavensMate plugin installed for sublime text and you have configured your text editor for working with Salesforce. You must see MavensMate tab on the right side of help on the top when installed. Now you can code easily here and as you save your file it'll be automatically saved on the Salesforce as shown in the below image:- 


Be careful while using MavensMate as it may override your code on Salesforce when multiple users are working on the same org. It usually show a conflict if there is one but it's a better approach to refresh the files from server before start working. For this you can right click on the folder or file from the left side bar and go to MavensMate->Refresh from Server.




So, In this way you can setup MavensMate with your favorite text editor for Salesforce. Cheers..!!

Friday, 1 December 2017

Trailhead is more personalized and smart than ever before


New HomePage

Trailhead has always been a great source to learn about salesforce and now it's more awesome. Recently, it has got a new personalized homepage which looks amazing.


Vanity URL

Not only this, you can make some great customization in your profile just go to your trailhead profile and click on the edit button in the top right corner of the about me section. 


Now what ? Noticed something new ? Yes you have got few more fields to fill up and showcase your personal opinion about the trailhead. It can be your favorite badge, the tune you like the most while doing trailheads, even you can also share your favorite natural wonder. But wait..!! there is something special here. Did you noticed My URL ?  Yup. you guessed it right. You can now customize your trailhead profile URL to make it yours. Your own personalized URL. Here is mine.


You have some rules to follow while configuring your vanity url. Make sure to have a look at them here

Isn't it amazing ? But wait there is something more.

My Trailhead

Salesforce has announced My Trailhead that will allow your company to customize the learning environment. This is not available yet but will be generally available in the 2nd half of 2018. It allows your company to create customized trailmixes for you and also track your progress, means your company can now help you improving your performance by forming a customized trailhead path just for you.

There is one more amazing feature you must have a look at.

Merge your trailhead accounts

Yes..! You can finally merge your trailhead accounts yourself. Salesforce gives you this functionality too now, just go to settings and you have the option to merge your trailhead account. Need to know more ? have a look at this article


Seize the Trail

Salesforce has given a golden opportunity to all the trailblazers. Do more and more trailheads and earn batches between Nov 13 - Dec 31' 2017 and you can be the lucky winner to earn any of these goodies depending upon your rank.You can look at the description here and if you have seen this now for the first time. Don't worry, you have still 1 month left to showcase your skills. #SeizetheTrail


So. What are you waiting for ? Go ahead and start doing trailheads now...!!