function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Loop through all Elements (Fields)



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 !=[0].FieldName.value) {
System.debug('CHANGED fieldName/fieldValue: ' + fieldName + ' / ' + FieldName.value);




Any ideas?






Message Edited by juppinger on 03-24-2009 03:29 AM
Message Edited by juppinger on 03-24-2009 03:30 AM
Venkat PolisettVenkat Polisett

 if(Trigger.old[0].fieldName.value !=[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]> !=[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]>




Message Edited by juppinger on 03-24-2009 07:30 AM
Venkat PolisettVenkat Polisett

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.


Message Edited by Venkat Polisett on 03-24-2009 11:02 AM