You need to sign in to do that
Don't have an account?

Access saved data for field name stored in a variable
Hi,
I have made a trigger and inside that trigger, I need to access saved data value, for field name stored in a string variable.
e.g. Account myAccount = trigger.new[0];
String fieldName = 'Fax';
myAccount.Fax works;
myAccount.fieldName gives error.
trigger dsAccountTrigger on Account (after insert) {
Account myAccount = trigger.new[0];
String query = 'select FieldName__c,MatchType__c from FieldMatchTypeRules__c where ObjectName = \'account\'';
matches = Database.query(query);
for(integer i = 0; i < matches.size(); i++)
{
String matchType = matches[i].MatchType__c;
String myFieldScore = '';
myFieldScore = myAccount.Fax;//This works
//This does not work
myFieldScore = myAccount.matches[i].FieldName__c;
//How to access field value saved in variable?
}
}
Do I need to have a different approach.
Please guide
Regards
Hector...
You have to use the get/put notation if you want to access a field based on another variable.
Something like the following should do the trick:
You can use the get and put methods on an object to set or retrieve values for fields using either the API name of the field expressed as a String, or the field's token. In the following example, the API name of the field AccountNumber is used:
myFieldScore = matches[i].get('fieldname');
Refer the apex-guide to know more about the dynamic apex concepts.
Hope this helps.
Thanks bob and pradeep,