Monday, May 28, 2012

Working with the Developer Console - Salesforce

How to Play with Developer Console and Debug logs

Developer Console
The Developer Console is a collection of tools you can use to analyze and troubleshoot applications in your Salesforce organization. It’s a separate window composed of a set of related tools that allow you to access your source code and review how it executes. It can also be used to monitor database events, workflow, callouts, validation logic, cumulative resources used versus system limits, and other events that are recorded in debug logs. It’s a context-sensitive execution viewer, showing the source of an operation, what triggered that operation, and what occurred afterward. Access the Developer Console by clicking Your Name | Developer Console.
The Developer Console System Log
The System Log console

Debug Log Categories

You can specify the following log categories. The amount of information logged for each category depends on the log level:
Log Category Description
Database Includes information about database activity, including every data manipulation language (DML) statement or inline SOQL or SOSL query.
Workflow Includes information for workflow rules, such as the rule name, the actions taken, and so on.
Validation Includes information about validation rules, such as the name of the rule, whether the rule evaluated true or false, and so on.
Callout Includes the request-response XML that the server is sending and receiving from an external Web service. This is useful when debugging issues related to using Force.com Web services API calls.
Apex Code Includes information about Apex code and can include information such as log messages generated by DML statements, inline SOQL or SOSL queries, the start and completion of any triggers, and the start and completion of any test method, and so on.
Apex Profiling Includes cumulative profiling information, such as the limits for your namespace, the number of emails sent, and so on.
Visualforce Includes information about Visualforce events including serialization and deserialization of the view state or the evaluation of a formula field in a Visualforce page.
System Includes information about calls to all system methods such as the System.debug method.

Debug Logs
A debug log records database operations, system processes, and errors that occur when executing a transaction or while running unit tests. The system generates a debug log for a user every time that user executes a transaction that is included in the filter criteria.
You can retain and manage the debug logs for specific users.
Log Lines
Included inside the units of code. These indicate what code or rules are being executed, or messages being specifically written to the debug log. For example:
Debug Log Line Example
Debug Log Line Example
Log lines are made up of a set of fields, delimited by a pipe (|). The format is:
  • timestamp: consists of the time when the event occurred and a value between parentheses. The time is in the user's time zone and in the format HH:mm:ss.SSS. The value represents the time elapsed in nanoseconds since the start of the request. The elapsed time value is excluded from logs reviewed in the Developer Console.
  • event identifier: consists of the specific event that triggered the debug log being written to, such as SAVEPOINT_RESET or VALIDATION_RULE, and any additional information logged with that event, such as the method name or the line and character number where the code was executed.
Now we start playing with System Logs:-

System Log Containing Output of Formula Build: --- (Developer Console)

   
1. Integer remainder = math.mod(12, 2);
system.debug('######### value of remainder'+remainder);

output :-- 11:47:31:042 USER_DEBUG [2]|DEBUG|######### value of remainder0

2. system.Debug(Math.mod(3,10));

Output :-- 11:55:56:053 USER_DEBUG [1]|DEBUG|3

3. system.debug(math.min(12.3, 156.6));

Output:-- 12:04:09:097 USER_DEBUG [1]|DEBUG|12.3

4. system.debug(math.max(12.3, 156.6));
output:--12:07:21:067 USER_DEBUG [1]|DEBUG|156.6

5. system.debug(math.abs(-42));

output:-- 12:11:52:043 USER_DEBUG [1]|DEBUG|42

6. Integer numberDays = date.daysInMonth(1960, 2);
   system.debug('###### value of days'+numberDays);

output:--12:22:05:046 USER_DEBUG [2]|DEBUG|###### value of days29

7.  system.debug(date.daysInMonth(1960, 2));

output:-- 12:23:39:054 USER_DEBUG [1]|DEBUG|29

8. system.debug(date.newinstance(1960, 2, 17));

output:-- 12:26:09:042 USER_DEBUG [1]|DEBUG|1960-02-17 00:00:00

Note:-- newInstance Constructs a Date from Integer representations of the year,
month (1=Jan), and day. The following example creates the date February 17th, 1960.


9. system.debug(date.isLeapYear(1960));

output:-- 12:28:31:038 USER_DEBUG [1]|DEBUG|true

10. system.debug(date.today());

output:--12:33:41:047 USER_DEBUG [1]|DEBUG|2012-05-15 00:00:00

11. date mydate = date.today();
      system.debug(mydate.day());

output:-- 12:38:55:038 USER_DEBUG [2]|DEBUG|15

12. date mydate = date.today();
system.debug(mydate.year());

output :-- 12:37:36:057 USER_DEBUG [2]|DEBUG|2012

13. system.debug((date.today()+365));

output:-- 13:15:38:030 USER_DEBUG [1]|DEBUG|2013-05-15 00:00:00

14. system.debug((date.today()-365));

Output:-- 13:17:17:035 USER_DEBUG [1]|DEBUG|2011-05-16 00:00:00

15. Date myDate = Date.Today();
String sDate = String.valueOf(myDate);
system.debug('###### value of sDate'+sDate);

output:--- 13:25:28:042 USER_DEBUG [3]|DEBUG|###### value of sDate2012-05-15

16. Double myDouble = 12.34;
String myString = String.valueOf(myDouble);
System.Debug('##### value of myDouble'+myString);

output:--- 13:27:57:036 USER_DEBUG [4]|DEBUG|##### value of myDouble12.34

17. date startDate = date.newInstance(2003, 1, 1);
date dueDate = date.newInstance(2008, 1, 30);
integer numberDaysDue =  startDate.daysBetween(dueDate);
system.debug('##### value of numberDaysDue'+numberDaysDue);
   
output:-- 13:53:35:041 VARIABLE_ASSIGNMENT [5]|numberDaysDue|1855

18.date startDate = date.newInstance(2003, 1, 1);
date dueDate = date.newInstance(2008, 1, 30);
integer numberDaysDue = startDate.daysBetween(dueDate);
system.debug('##### value of numberDaysDue'+numberDaysDue);
system.debug(math.mod(numberDaysDue,7));

output:-- 13:55:26:049 USER_DEBUG [8]|DEBUG|0

Keep Playing with Debug Logs and Developer Console because they are extremely useful. Enjoy Coding.

                                         =======  Abhinav Sharma  =========



1 comment: