Tuesday, May 5, 2020

PMD Source code analyzer | Automated Apex Code Review

What is PMD?
PMD stands for Programming Mistake Detector is an open source code analyzer tool which is used to generate code quality report.
Here we will discuss about how PMD can be used to generate code quality report for standalone application.
Step 1:
Download PMD from here à Unzip the download file
Step 2:
Now Retrieve the code through package.xml from workbench-
Now unzip the source code after downloaded from workbench.
Step 3:
Run the below pmd command to generate the report

You can define the rule set as per your need or else you can use the built-in rule set. See the below table for build in rule set path
Rule Name
Rule Set Path
Best Practices
Rules which enforce generally accepted best practices.
Code Style
Rules which enforce a specific coding style.
Rules that help you discover design issues.
Rules that are related to code documentation
Error Prone
Rules to detect constructs that are either broken, extremely confusing or prone to runtime errors.
Rules that flag suboptimal code.
Rules that flag potential security flaws.

Sunday, May 3, 2020

SFDX Setup – Dev Hub | Scratch Org | VS Code IDE

Salesforce DX will provide you a series of new tools and features that will change the way developer build Salesforce application.
Salesforce DX introduce a new type of environment which is called as ‘Scratch Org’. To create this scratch org, you need to enable the org as Dev Hub. The scratch will be the copy of Dev Hub though you can configure which parts of your org copy over and it can be disposable over a specified period of time.

Here I will setup SFDX in my Org and will create a Scratch org to implement a lightning component using VS Code IDE and push the component from VS Code IDE to Target org.

Step 1:

Enable Deb Hub in your Salesforce Org. Go to Setup à Development à Dev Hub and click Enable. Once you enable Deb Hub, you cannot disable it again.

Step 2:

Download and Install VS Code IDE.
Download and Install Command Line Interface (CLI). Make sure you have checked the checkbox to set the Salesforce CLI bin path in PATH environment variable while installing CLI.
Open command prompt and run sfdx to check whether CLI installed properly or not

Step 3:

Open Visual Studio Code IDE and Install Salesforce Extension Pack extension.

Step 4:

To create a project in VS Studio Code IDE-
Use SFDX:Create Project, System will ask for SFDX Project Name and then choose the directory where you want to save the project and clicks on ‘Create Project’

Step 5:
Now we are going to integrate the VS Code project to Dev Hub.
Press CTRL+SHIFT+P à SFDX:Authorize a Dev Hub
VS Studio Code IDE will redirect to login Dev Hub and after successfully login, you will get success message in VS Studio for Authorizing a Dev Hub

Step 6:
Now you need to create a Scratch Org by using –
CRTL+SHIFT+P à SFDX:Create a default Scratch Org..

Choose default scratch Org definition JSON

Put a scratch Org Alias name-

Enter the no of day you want to work in the scratch Org. You can choose between 1-30 days. After that no. of days the scratch org will be expired.

Step 7:
Once you get Salesforce CLI message that scratch org has been created, you need to run the below command to open the scratch org-
sfdx force:org:open

So as of now we have created a scratch org from a Dev Hub. Now we are going to create a lightning component in VS Code IDE and move the component to scratch org.

Step 8:
To create a lightning component, CTRL+SHIFT+P à SFDX:Create Lightning Component and give a name to lightning component.

Step 9:
Now we will push the lightning component from VS Code IDE to scratch Org.
Press CTRL+SHIFT+P à SFDX:Push Source to Default Scratch Org
You can see the push request status in Output window. Once push request is successful the same component will be available in the scratch Org to use.

Saturday, May 2, 2020

Salesforce ANT Deployment – Retrieve |Validate | Deploy

Setup the ANT into your system properly before going to use it. Please check here the ANT setup guide if you haven’t done it so far

Understanding build.xml | build.properties | package.xml

build.properties – you must update this file to specify the salesforce org credential in order to complete the ANT task in build.xml

build.xml – you must modify the build.xml file to indicate the location of ant-salesforce.jar file. Modify the path the of the ant-salesforce.jar in location attribute of <pathelement> in build.xml.

Package.xml – you need to update the metadata component in this xml file. package.xml is a collection of metadata components which are identifies by the programming interface.
Here are the steps that need to follow to retrieve code from source org and deploy the code in Target org-

Update the build.properties file with source org credential and server url. For non-production org server url would be test.salesforce.com

Modify the package.xml with the necessary component which needs to be deployed from source org to Target org.
Here we are going to deploy one Apex class (OrderHelper) and one Apex Trigger (OrderTrigger) to understand the process-

Run the command ant retrieveCode in command prompt to retrieve the code from source org to local machine.

retrieveTarget=”codepkg” – This attribute indicates that the component mentioned in the package.xml will be retrieve to codepkg folder.

As of now we have retrieved the component from Source org to local machine. Now we are going to push the same components to production.
Update the credential in build.properties with server url. To deploy into the production change the server url to Login.salesforce.com
Make sure that you have included the necessary test class in the package.xml within the <runTest></runTest> tag under <target name=”deployCode”> element.
testLevel=RunSpecifiedTests indicates that the command will run only specific test classes that are mentioned within <runTest> tag.

Run the command ant deployCode .
To validate the component with test coverage use checkOnly attribute.
checkOnly=true indicates that the command will only validate with test class coverage (Similar to validate change set with run as specific test class)
checkOnly=false indicates that the command will validate and deploy the component to Target org.
You can also check the validation status in salesforce- Setup->Deploy->Deployment Status

Now its time to Quick Deploy!
Copy the Request Id from command prompt and paste it in build.properties file as recentValidationId.

Now Run the ant quickDeploy command to deploy the recently validated components.

Congratulations! You have completed deployment using ANT deployment tool

Friday, May 1, 2020

Quick Step by Step ANT Setup guide

Install JDK 1.7 and above version for better security and for the latest TLS security protocols. Download Link

Download apache ANT from here

Download Force.com ANT Migration Tool from here. Save the .zip file locally, and extract the contents to the directory of your choice.

Setup Environment variable ‘ANT_HOME’ and JAVA_HOME’

To setup environment variable go to Control Panel->System and Security->System->Advance System Setting->Environment variable

Set ANT_HOME variable path to parent folder of ‘bin’
i.e. apache-ant-1.10.7-bin\apache-ant-1.10.7

Similarly Set JAVA_HOME variable path to jdk directory.
i.e. C:\Program Files\Java\jdk1.8.0_241

Add Apache bin folder path to PATH variable i.e. apache-ant-1.10.7-bin\apache-ant-1.10.7\bin

Test whether ANT is installed properly or not by running ant -version command in command prompt. If you receive error like ‘unable to find tools.jar’, make sure you copy the tool.jar from jdk/lib folder to jre/lib folder.

Congratulations! You have successfully Setup ANT in your system.

For code retrieve/Validate/Deploy, please this article- Salesforce ANT Deployment – Retrieve | Validate | Deploy