Thursday, April 7, 2016

Clip of the day : logging SObjects without nulls

// sobjects are difficult to read with all the "field=null" values, so
// return a string without them.
public string stringWithoutNullFields(string aString) { return aString.replaceAll('\\w+=null',''); }
public string toString(SObject anSobject) { return stringWithoutNullFields(anSobject + ''); }
public string toString(list<sobject> aList) { return stringWithoutNullFields(aList + ''); }
public string toString(map<string, sobject> aMap) { return stringWithoutNullFields(aMap + ''); }
public string toString(map<id, sobject> aMap) { return stringWithoutNullFields(aMap + ''); }

Whether you're using system.debug(someObject); or logging using a home-rolled log (I do), it'd difficult to read the output of some sobjects if too many of the fields look something like "a_field__c=null, another_field__c=null, yet_another_field__c=null..."

Get rid of all that nonsense using aString.replaceAll('\\w+=null','');.

No comments:

Post a Comment

Follow @TomGagne