How to check if a Salesforce object has changed

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;

return changed;

by Vladimir Martinov


%d bloggers like this: