Wednesday, March 4, 2015

Keep alive your debug logs without manual refresh in Salesforce

Hello Everyone,

I felt this urge to write such thing because I have to monitor log in salesforce for performance testing being a lazy person J I don’t want to do it by keeping my eye on developer console. So I found this trick it might be helpful to you as well

Here goes the code !!!

Apex Class

/**
* This class will create debug log instances for users 
**/ 
public class LogResetController {
    public List<String> userIds {get;set;} //will hold the user ids 
    public List<String> userNames {get;set;} // will hold the user names used in soql
    Public Integer counter {get;set;} {counter = 0;}
    public Boolean isFstLoad {get;set;} { isFstLoad = true;}
    /**
* controller of the class initialize your user name list here  
**/
    public LogResetController(){    
        //initialise list of users here and call initpage();
        userNames = new List<String>();        
       //addition of users for those you want log to be enabled 
       userNames.add('test@test.com'); 
        initPage();
    }
    public void initPage(){
        // getting the users 
        List<User> users = [SELECT ID FROM USER WHERE USERNAME IN:userNames AND isActive = true];
        // creating list of ids this can be omitted i did it for ease of use on the page 
        userIds = new List<String>();
        for(User u : users){
            userIds.add(u.Id) ;
        }
    }
    public void dummyAction(){
        // this will be called from vf page action poller to re-initiate the debug log 
        counter++;
        isFstLoad = false;
    }
    public void ResetAction(){
        isFstLoad = true;
    }
}

Visualforce Page

<apex:page sidebar="false" showHeader="false" controller="LogResetController">
    <style>
    #loadingMessage {
   width: 100%;
   height: 100%;
   z-index: 1000;
   top: 50%;
   left:50%;
   position: absolute;
   background-repeat: no-repeat !important;

    </style>
    <script>
    var openWindows= new Array();
    </script>
    <apex:form id="mainform">
        <apex:actionFunction name="resetPage" action="{!ResetAction}" reRender="mainform" />
        <apex:repeat value="{!userIds}" var="u" rendered="{!isFstLoad}">
            <script>
                var user = '{!u}' ;
                var url = '/setup/ui/listApexTraces.apexp?user_id='+ '{!u}'+ '&user_logging=true';               
                var win = window.open(url);
                openWindows.push(win);
                console.log("************* Pushing ***************");
            </script>
        </apex:repeat>
        <apex:actionPoller action="{!dummyAction}" interval="20" reRender="mainform"> 
            <apex:outputPanel id="counterPanel">
            <div id="loadingMessage">
                    <img src="/img/loading32.gif" title="/img/loading32.gif"/>
                    <br/>
                    <div style="font-size:20px;margin-left:-60px;color:red">
                        Counter Value is : {!counter}
                    </div>
                </div>
            </apex:outputPanel>
        </apex:actionPoller>
        <apex:outputPanel id="refreshPanel">
            <apex:outputPanel rendered="{!NOT(isFstLoad)}">
                <apex:outputLabel > Refreshing .....</apex:outputLabel>
                <script>
                try{
                    for(var index = 0 ; index < openWindows.length ; index++){
                        console.log(" **************** CLOSING *********************** " + index );
                        openWindows[index].close();                           
                    }
                    openWindows= new Array();
                    resetPage();
                }
                catch(err){
                    console.log(err);    
                }
                </script>
            </apex:outputPanel>
        </apex:outputPanel>
    </apex:form>
</apex:page>


Test Class




@istest
public class LogResetControllerTest {
    private static testMethod void unitTest(){
        User U = [Select Id,UserName From User WHERE Id =: userInfo.getUserId()];
        Test.startTest();
        LogResetController con = new LogResetController();
        con.userNames = new List<String>();
        con.userNames.add(U.UserName);
        con.initPage();
        con.dummyAction();
        Test.stopTest();
        System.assertEquals(con.userIds.size(), 1);
    }

}

Tips on passing Salesforce AI Associate Certification

  🌟 Motivation to Pursue the Salesforce AI Associate Certification 🌟 The world of technology is in a state of perpetual evolution, and on...