Tag Archives: Force.com

How to Use Debug Logs in Salesforce

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. We can monitor specific users in Debug log by adding them to list of Monitored Users.

Add Users to the debug log:

Step1: Go to Setup, click Monitoring | Debug Logs.

1

Step2: Click on New button to add User.

2

Step3: Add User and Save.

3

4

Debug log can contain information about:

  • Database changes
  • HTTP callouts
  • Apex errors
  • Resources used by Apex
  • Automated workflow process, such as :
    • Workflow rules
    • Assignment rules
    • Approval processes
    • Validation rules

Debug Log Categories:
We 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, flows, and processes, 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 Log Levels:
We can specify the following log levels.

  • ERROR
  • WARN
  • INFO
  • DEBUG
  • FINE
  • FINER
  • FINEST

Note: The following are the limits for debug logs:

  • Once a user is added, that user can record up to 20 debug logs. After a user reaches this limit, debug logs stop being recorded for that user. Click Reset on the Monitoring Debug logs page to reset the number of logs for that user back to 20. Any existing logs are not overwritten.
  • Each debug log can only be 2 MB. Debug logs that are larger than 2 MB are reduced in size by removing older log lines, such as log lines for earlier System.debug statements. The log lines can be removed from any location, not just the start of the debug log.
  • Each organization can retain up to 50 MB of debug logs. Once your organization has reached 50 MB of debug logs, the oldest debug logs start being overwritten.

Example:
Here is a sample debug log I’ve written it in an Apex class, which is the controller of a visualforce page.

 System.debug('This is a test debug log'); 

Now execute that visualforce page on which the code is written.

After execution again go to the Debug Logs page, where the user is added. You will see the screen like below:

5

As you can see now that new row is added in “Debug Logs” section. click on the “view” button.

6

Displaying records based on the picklist values

Here is the example of Opportunity object, based on Stage Picklist values.

Apex Class:

public class SearchOpportunity {
    public Opportunity opp  {get;set;}
    public List<Opportunity> OpportunityList  {get;set;}
    
    public SearchOpportunity () {
        opp = new Opportunity();
    }
    
    public PageReference doSearch() {
        OpportunityList =[Select Id, Name, StageName FROM Opportunity WHERE StageName =: Opp.StageName];
        return null;
    }
}

Visualforce Page:

<apex:page controller="SearchOpportunity">
    <apex:form>
        <apex:pageBlock Title="Search Opportunities">
            <b>Stage :</b>
            <apex:inputField value="{!opp.StageName}" />
            <apex:commandButton value="Search" action="{!doSearch}"/>
        </apex:pageBlock>
        
        <apex:pageBlock title="List of Opportunities">
            <apex:pageBlockSection columns="1">
                <apex:pageBlockTable value="{!OpportunityList}" var="item">
                    <apex:column value="{!item.Name}" headerValue="Name"/>
                    <apex:column value="{!item.StageName}" headerValue="Stage"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output:

SearchOpportunities

Primitive Data Types in Salesforce Apex

Apex uses the same primitive data types as the SOAP API. All primitive data types are passed by value, not by reference.

All Apex variables, whether they’re class member variables or method variables, are initialized to null. Make sure that you initialize your variables to appropriate values before using them. For example, initialize a Boolean variable to false.

  • Integer: A 32-bit number that does not include decimal point. Integers have a minimum value of -2, 147,483648 and maximum value of 2,147,483648.
    For example:

     Integer i = 1; 
  • Long: A 64 bit number that doesn’t includes a decimal point. Long has a minimum value of -2^63 and a maximum value of 2^63-1.
    For example:

    Long l = 2147483648L;
  • Double: A 64 bit number that doesn’t includes a decimal point. Long has a minimum value of -2^63 and a maximum value of 2^63-1.
    For example:

     Double d = 3.14159; 
  • Decimal: A number that includes a decimal point. Decimal is an arbitrary precision number. Currency fields are automatically assigned the type decimal.
    For example:

     Double d = 256.32; 
  • String: Strings are set of characters and are enclosed in single quotes. They store the text values such as name or an address.
    For example:

     String str = 'Biswajeet Samal'; 
  • Date: A value that indicates a particular day. Date values contain no information about time. Date values must always be created with system static method.
    For example:

     Date myDate = Date.newinstance(2015, 09, 14);
    Output: 2015-09-14       00:00:00
    
  • Time: A value that indicates a particular time. Time values must always be created with a system static method.
    For example:

    Time tm = newInstance(10,12,5,11);
    Output: 10:12:05
    
  • Date Time: These are data types associated with dates and times along with Date data type. The time data type stores times (hours, minutes, second and milliseconds). The Date data types stores dates (Year month and day). The time data type stores both dates and times.
    Each of these classes has a newInstance method with which we can construct particular date time values.
    For example:

    Date dt = Datetime.now();
    
  • Id: Any valid 18-character Force.com record identifier.
    For example:

    ID id='00910000004T2AGAA0';
    

    If you set ID to a 15-character value, Apex converts the value to its 18-character representation. All invalid ID values are rejected with a runtime exception.

  • Boolean: A value that can only be assigned true, false, or null.
    For example:

    Boolean isValid = true;
    
  • Blob: A value that can only be assigned true, false, or null.
    For example:

    It stores files data in binary format.