Skip to content

Instantly share code, notes, and snippets.

@use
Last active February 23, 2024 19:17
Show Gist options
  • Save use/5a10964ad92ee931f55ec7353fdc3217 to your computer and use it in GitHub Desktop.
Save use/5a10964ad92ee931f55ec7353fdc3217 to your computer and use it in GitHub Desktop.
// bark's prototype insert example 240220
List fclog = new List(); // the log, for the insert
SObjectType triggerType = trigger.new.getSObjectType(); // the object name
List idx = [SELECT FieldName__c, Default__c FROM FieldChangeIndex__c WHERE Object__c =
:triggerType.getDescribe().getLabel() ]; // for the WHERE clause.
if (idx.isEmpty()) {
return;
}
for ( sObject trigRecs : Trigger.new ) { // the records that changed
Map<String, Object> popFields = trigRecs.getPopulatedFieldsAsMap(); // recs’ fieldnames & vals
for(String field :popFields.keySet()){ // moving through the fieldnames
for(FieldChangeIndex__c field2 :idx ) { // fieldname index
if(field == field2.FieldName__c // do we care about the field?
&& '' <> popFields.get(field)
&& String.valueOf(popFields.get(field))
<> field2.Default__c ) { // has it changed?
fclog.add (new FieldChangeLog__c ( // add it to the log.
FieldName__c = field,
FieldRowId__c = trigRecs.Id,
NewValue__c = String.valueOf(popFields.get(field)),
Object__c = trigRecs.getSObjectType().getDescribe().getName(),
OldValue__c = '' ) // inserting
);
}
}
}
}
Database.insert(fclog);
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment