Showing posts with label Apex Variable. Show all posts
Showing posts with label Apex Variable. Show all posts

Thursday, 8 December 2016

SQOL to know Code coverage in Salesforce


Salesforce provides developers a way to find out Codecoverage details. 
Write a SQOL by using  “ApexCodeCoverageAggregate”  to know Code coverage for all Apex Classes and Apex Triggers withing fraction of seconds. 

* SOQL :

SELECT ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverageAggregate


Tips : 

* Use of "ORDER BY" clause will let you have your result more presentable.

e.g. 

SELECT ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverageAggregate ORDER BY  ApexClassOrTrigger.Name ASC




* Make sure that you have enabled "Use Tooling API" check box in Developer Console to execute following query.  (see following image - Red circle)




Monday, 5 October 2015

Avoid repeated execution of Trigger

Many times we came across the situation where because of our Workflow Rules or some other cases our same trigger goes into an Infinite Loop or get executed twice - once before workflows and once after workflows, you can find proof here.

“The before and after triggers fire one more time only if something needs to be updated.”

So Lets see one simple example of How to restrict any Trigger to fire only once i.e. avoid repeated or second execution of Trigger in same context -

Solution : 

You need to add one Static Boolean variable to a your utility/helper class (any apex class), and check its value within affected triggers, where you do think this trigger will get fired more than once.


* "MyUtilityClass" is your helper  / utility apex class  where we will have our static boolean variable - e.g. declared as "runOnceFlag= true"

public class MyUtilityClass {
   public static boolean  runOnceFlag= true;
}


* Sample example Trigger - where you will check for the value of static boolean variable "runOnceFlag"- For first time its value will be equal to TRUE, and we will set its value to FALSE in our trigger to avoid second run.

trigger myTriggerName on Account (before delete, after delete) {
   
 
     if(Trigger.isBefore && Trigger.isDelete){
   
         if(MyUtilityClass.runOnceFlag){
                // **************** Your all business logic code goes here  *********                
                               //** and in the end we will update our static boolean variable
                MyUtilityClass.runOnceFlag=false;           }
    }
}



Friday, 17 July 2015

Declare Apex Variable

Declaring Variables in Apex, Saelsforce (SFDC)

You can declare variables in Apex like any other programming language in simple way - like String and Integer as follows : 

    //String variable declaration with value = 'my string'
    String str = 'my string'; 
    
    // Integer variable declaration with value = 9 
         Integer i = 9;  

* Remember Apex variables are Case-Insensitive