পৃষ্ঠাসমূহ

.

Search Your Article

Total Pageviews

Wednesday, January 25, 2017

Apex - Debugging

Debugging is an important part in any programming development. In Apex, we do have certain tools to use for debugging. One of them is system.debug() method which prints the value and output of variable in debug logs.

There are two tools which you could you for debugging:
  • Developer Console
  • Debug Logs

Debugging via Developer Console

You could use the Developer console and execute anonymous functionality for debugging the Apex as below:
Example:
Consider our existing example of fetching the customer records which have been created today. We just want to know if the query is returning the results or not and if yes, then we would like to check the value of List.
Paste the below code in execute anonymous window and follow the steps which we have done for opening execute anonymous window.
Step 1: Open Developer console
Step 2: Open execute anonymous from 'Debug' as shown below:
open_developer_console_for_class_execution_Step_1 Step 3: Open the Execute Anonymous window and paste the below code and click on execute.
open_developer_console_for_class_execution_Step_2
//Debugging The Apex
List<apex_customer__c> customerList = new List<apex_customer__c>();
customerList = [SELECT Id, Name FROM APEX_Customer__c WHERE CreatedDate = today];//Our Query
System.debug('Records on List are '+customerList+' And Records are '+customerList);//Debug statement to check the value of List and Size
Step 4: Open the Logs as shown below
apex_debugging_devconsole_Step_1 Enter 'USER' in filter condition as shown below
apex_debugging_devconsole_Step_2 Step 5: Open the USER DEBUG Statement as shown below
apex_debugging_devconsole_Step_3 Step 6: Check for Error or variable values by which you could check and debug the code.

Debugging via Debug Logs

You could debug the same class via debug logs as well. Suppose, you have a trigger in Customer object and you would like to debug this Trigger for some variable values. Then you could do this via the debug logs as shown below:
This is the Trigger Code which updates the Description field if the modified customer is active and you want to check the values of variables and records currently in scope:
trigger CustomerTrigger on APEX_Customer__c (before update) {
    List<apex_customer__c> customerList = new List<apex_customer__c>();
    for (APEX_Customer__c objCust: Trigger.new) {
        System.debug('objCust current value is'+objCust);
        if (objCust.APEX_Active__c == true) {
            objCust.APEX_Customer_Description__c = 'updated';
            System.debug('The record which has satisfied the condition '+objCust);
        }
    }
}
Perform following steps to generate the Debug logs.
Step 1: Set the Debug logs for your user. Go to Setup and type 'Debug Log' in search setup window and then click on Link.
debugging_via_debug_console_Step_1 Step 2: Set the debug logs as following:
debugging_via_debug_console_Step_2 debugging_via_debug_console_Step_3 Step 3: Enter the name of User for which you would like to setup. Enter your name.
debugging_via_debug_console_Step_4 Step 4: Modify the customer records as event should occur to generate the debug log.
debugging_via_debug_console_Step_5 Step 5: Now go to the debug logs section again. Open debug logs and click on Reset.
debugging_via_debug_console_Step_6 Step 6: Click on the view link of the first debug log.
debugging_via_debug_console_Step_7 Step 7: Search for the string 'USER' by using browser search as shown below:
debugging_via_debug_console_Step_8 Debug statement is showing the value of the field at which we have set the point.

No comments:

Post a Comment