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:

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'
 }
Advertisements

2 thoughts on “Debugging with JSON.serializePretty

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s