Friday, October 19, 2018

Automated Code review for Apex in Salesforce – Static code analysis


PMD is a source code analyzer. It finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth. It supports Java, JavaScript, Salesforce.com Apex and Visualforce, PLSQL, Apache Velocity, XML, XSL.

Installation Steps:

1. For MacOS:

Follow below steps to install PDM.

A. Open the terminal
B. Run cd $HOME
C. Run curl -OL https://github.com/pmd/pmd/releases/download/pmd_releases%2F6.8.0/pmd-bin-6.8.0.zip
D. unzip pmd-bin-6.8.0.zip
E. alias pmd="$HOME/pmd-bin-6.8.0/bin/run.sh pmd"

2. For Windows:

Follow below steps to install PDM.

A. Open the command prompt
B. Download pmd-bin-6.8.0.zip 
C. Extract the zip-archive, e.g. to C:\pmd-bin-6.8.0
D. Add folder C:\pmd-bin-6.8.0\bin to PATH, either
Permanently: Using System Properties dialog > Environment variables > Append to PATH variable
Temporarily, at command line: SET PATH=C:\pmd-bin-6.8.0;%PATH%
E. alias pmd="$HOME/pmd-bin-6.8.0/bin/run.sh pmd"

PMD Configuration for Eclipse :

To install the PMD plugin for Eclipse:

A. Start Eclipse and open a project
B. Select “Help”->“Install New Software”->“Find and Install”
C. Click “Add”
D. Enter “PMD” into the Name field and http://sourceforge.net/projects/pmd/files/pmd-eclipse/update-site/ into the URL field
E. Click “Ok”
F. Click through the rest of the dialog boxes to install the plugin


Running PMD on Eclipse :

A. To run PMD, right-click on a project node and select “PMD”->“Check code”.
B. To run the duplicate code detector, right-click on a project node and select “PMD”->“Find suspect cut and paste”. The report will be placed in a “reports” directory in a file called “cpd-report.txt”.

Wednesday, May 9, 2018

Build URL to display a case using case number instead of Salesforce CaseID

This can be implemented using below code.

Custom label:  
Name: Case_Number_Details

Value: Please enter the valid 8 character Case Number (e.g. 00123456) after the equal sign in the URL above to view the case details.


Visualforce Page:


<apex:page controller="GetCaseByNumber" title="Get Case By Number" showHeader="false">

  <script>

      if ("{!CaseQuery}".length > 0) {

          if ("{!CaseQuery}" != "<apex:outputText value="{!$Label.Case_Number_Details}"/>" && "{!CaseQuery}" != "You Must Supply a Case Number") window.location = "/{!CaseQuery}";

      }

  </script>

  <h1 style="display:block;margin:25px;font-size:16px;">{!CaseQuery}</h1>


</apex:page>


Apex Class:


public class GetCaseByNumber {

    public static String getCaseQuery () {
       String caseNumberDetails = Label.Case_Number_Details;

        string caseNumber = ApexPages.CurrentPage().getParameters().get('CaseNumber');
        List <Case> c = [SELECT Id FROM Case Where CaseNumber =: caseNumber];
        if (c.size()>0) {
            return string.valueOf(c[0].Id);
        }
        else return caseNumberDetails;

    }    

}

Apex test Class:



private class GetCaseByNumberTest {
    
    static testmethod void TestAccdata(){ 
        Profile prof = [select id from profile where name='System Administrator'];    
        User us      = new User(alias = 'tuser', email='TestUser@testingorg.com',    
                                emailencodingkey='UTF-8', lastname='TestingUser', languagelocalekey='en_US', 
                                localesidkey='en_US', profileid = prof.Id,   
                                timezonesidkey='America/Los_Angeles', username='TestUser@testingorg.com');
        insert us;  
        System.runAs (us) { 
            Account ac = new Account(Name    ='Test', OwnerId  =us.Id);            
            insert ac;
            
            
            Contact cn  = new Contact (FirstName  = 'Test', LastName = 'Test',AccountId = ac.Id,email='testcase@org.com');
            insert cn;
            
            Test.startTest();
            string RTID = [select id from RecordType where SobjectType = 'Case' and Name = 'Support Case'].Id;
            System.assertNotEquals(null,RTID);
            
            case c = new Case(AccountId=ac.Id ,ContactId =  cn.Id,Status='New',Origin='Phone',RecordTypeId = RTID,Subject = 'TestCase',Description ='Test',Product_Type__c = 'My Product);
            
            insert c;
            PageReference myVfPage = Page.getCaseByNumber;
            Test.setCurrentPage(myVfPage);
           ApexPages.currentPage().getParameters().put('CaseNumber',ApexPages.currentPage().getParameters().get(c.CaseNumber));
            
            GetCaseByNumber controller = new GetCaseByNumber();
            GetCaseByNumber.getCaseQuery();
            
            Test.stopTest();
            
        }

    }