+ Start a Discussion
MrTheTylerMrTheTyler 

Debug Helper - sample code

I've found using system.debug to be a little cumbersome so I created this little bit of code today to help myself and hope that it will help others too.   The most challenging part about this was simply reading through the documenation to find how to create a generic sobject handler and then iterate through the fields without knowing the object type beforehand. 

 

First thing to do is create a new object called debug.  Add fields for text and timestamp.  Add the controller and vforce page listed below.  From there you can call DebugPlus.writeLog() from your code or ExecuteAnonymous.  You can write a string or a list<sObject>.

 

 

Cheers,

 

Tyler

---------------------------------------

The Nerd Whisperer LLC

www.thenerdwhisperer.com

 

 

 

Controller

 

public class DebugPlus {

    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [select name,text__c, timestamp__c from debug__c order by name desc]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records  
    
    public List<debug__c> getDebugEntries() {
         return (List<debug__c>) setCon.getRecords();
    }
    
    static public void writeLog(string s){
    	debug__c x = new debug__c();
    	x.Text__c = s;
    	x.timestamp__c = datetime.now();
    	insert x;
    }
    
    static public void writeLog (list<sObject> lo){
    	string output;
    	for (sobject o : lo){
    		output = '';
    		Schema.DescribeSObjectResult r = o.getSObjectType().getDescribe();
    		Map<String, Schema.SObjectField> M = r.fields.getMap();
    		
    		for (string fldname : M.keyset()){
    			try{
    				output = output + ' | ' + fldname + ':' + o.get(m.get(fldname));
    			}
    			catch(System.SObjectException e){}
    		}
    		
    		writelog(output);
    	}
    }
    
    static public PageReference clearLog(){
    	list<debug__c> lDbg = [select id from debug__c];
    	delete lDbg;
    	pagereference DebugPage= new pagereference(Page.DebugLogPlus.getURL());
		DebugPage.setRedirect(true);
		return Debugpage;
    }
}

 

 

vForce

 

<apex:page controller="DebugPlus">
    <apex:form >
    	<apex:commandButton action="{!ClearLog}" value="Clear Log" id="ClrLog"/>
    </apex:form>
    
    <apex:pageBlock >
    	<apex:pageBlockTable value="{!DebugEntries}" var="d">
			<apex:column value="{!d.name}"/>
            <apex:column value="{!d.Text__c}"/>
            <apex:column value="{!d.TimeStamp__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

 

 

bob_buzzardbob_buzzard

Just to give you a heads up on something you may hit in the future - we did something similar (though not as full featured) a while back, but had to return to system debug when attempting to figure out what was going on when errors were raised.  

This was because when the transaction was rolled back on error, so was all of our debug information.  We tried all sorts of things to get around this - future methods, http callouts but couldn't find a workaround.  Its a real bummer as the debug log is so busy now its difficult to sort wheat from chaff.

MrTheTylerMrTheTyler

You make a good point and after reading that you've tried all kinds of ways to circumvent the issue I'll forgoe the effort on my end.  >8 )   .  I just browsed around the dox looking for some method of commiting a transaction within a transaction like can be done in SQL stored procedures but it looks like that is not available at this time.

 

But I did notice that the word "commit" is listed as a reserved word in apex (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_reserved_words.htm) but I can't find anywhere in the apex language reference about its use.  Perhaps one day we can have nested commits and my little debug helper will become more useful.

 

 

It's Lookin' Like Spring!

 

 

Tyler