Debugging with JSON.serializePretty

Here’s a quick tip that I thought I’d share. Today I was trying to troubleshoot a trigger and needed to know what the Trigger.newMap and Trigger.oldMap looked like. Rather than put a lot of System.Debug messages for each field I was interested in, I put in a single debug statement to show me the entire object. I used JSON.serializePretty to make it look nice.

Here’s a quick example:

1
2
Account a = new Account(name = 'Test Account');
System.debug(JSON.serializePretty(a));

This gives you a nice looking entry in the debug log:

 20:43:48.070 (70043000)|USER_DEBUG|[2]|DEBUG|{
 'attributes' : {
 'type' : 'Account'
 },
 'Name' : 'Test Account'
 }