You cannot call cross-object get
. Take a look at the FieldReference
class. It's open source, so you can just copy it into your org. Then you could do:
for (String invoiceField : invoiceFields)
system.debug(FieldReference.build(invoiceField).getFrom(opportunity);
What you want probably cannot be accomplished in a clean way without recursive logic. If you want to do so without a helper class, you could do something along the lines of:
public with sharing class CrossObject
{
public static Object multiLevelGetget(ObjectSObject record, String field)
{
if (record == null) return null;
if (!field.contains('.')) return ((SObject)record).get(field);
record = ((SObject)record).getSObject(field.substringBefore('.'));
field = field.substringAfter('.');
return multiLevelGetget(record, field);
}
}