You need to sign in to do that
Don't have an account?
Loop through all Elements (Fields)
Hi,
I want to loop through all field names and compare the values of a lineitem object...
Heres my not working code:
trigger HistoryOpportunityLineItemV001_test on OpportunityLineItem (after insert, after update, before delete) {
// Get the sObject describe result for the Opportunity object
Schema.DescribeSObjectResult r = OpportunityLineItem.sObjectType.getDescribe();
//Generate a Map of the fields
Map<String, Schema.SObjectField> M = r.fields.getMap();
//Now loop through the list of Fields
for (Object fieldName : M.keySet()){
//system.debug('fieldName: ' + fieldName);
if(Trigger.old[0].fieldName.value != Trigger.new[0].FieldName.value) {
System.debug('CHANGED fieldName/fieldValue: ' + fieldName + ' / ' + FieldName.value);
}
}
}
Any ideas?
Thx,
jup
if(Trigger.old[0].fieldName.value != Trigger.new[0].FieldName.value) {
You cannot use a substitution for the fieldname here. Apex parser thinks that "fieldName" is an actual field on the Triggering object.
Okay. Could you give a solution to loop through all field values an compare them?
I want compare the old trigger values with the new ones.
Like this:
for ( i=0 to <countTriggersFields> ){ if(Trigger.old[0].<fieldName.value[i]> != Trigger.new[0].<FieldName.value[i]>) { // do anything } }
But I want to loop ALL fields. Even if I will create custom fields in future.
- I need the value for <countTriggerFields>.
- I need the value for <fieldName.value[i]>
Thx,
jup
Dynamic Apex does not help you here. Use real field names in comparision. If you add a new field and you want to compare the values, change the trigger then.
If you want to track who is changing what, then eable history tracking for those fields.
You can query field history objects to find out what field is changed by who, old value and new value for the tracked fields.
Hope this helps.