When working with Apex triggers, it will sometimes be useful to know whether your
object has changed. You can write a function where you check every field by hard-coding them, but there is a more elegant way to accomplish this:


private static boolean hasChanged(YourObjectClass oldObject, YourObjectClass newObject) {

boolean changed = false;
Map fieldsMap = Schema.SObjectType.YourObjectClass.fields.getMap();
for (String key : fieldsMap.keySet()) {
Schema.DescribeFieldResult result = fieldsMap.get(key).getDescribe();

if (oldObject.get(result.getName()) != newObject.get(result.getName())) {
changed = true;
break;
}

}
return changed;
}

by Vladimir Martinov

 

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