Where and how can we use reflection in Salesforce, well here's a good example
Say you have 3 classes that map values from 1 object to another.
Class A matches 3 fields from Object X to ObjectY
Class B matches 5 different fields from Object X to ObjectY
Class C matches 12 fields, same fields in Class A and B + 4 other fields from Object X to ObjectY
Instead of hard coding this mapping in the code provide it as a lookup to a custom object, which will allow you to change any mappings without having to rewrite and deploy new code. Also, will allow the code to be re-used in future projects.
Custom object Mapping__c
Name
FieldMappingGroup__c
Custom object FieldMappingGroup__c
Name
Custom object FieldMappings__c
Fields
FieldMappingGroup__c
Name - not unique
SourceObject__c
SourceField__c
DestinationObject__c
DestinationField__c
Mapping__c[] listofFieldToCopy = [Select (Select id From FieldMappingGroup__c ) From Mapping__c where Name='XXX'];
set<id> mapIDs = new set<id>();
for (Mapping__c eachmap: listofFieldToCop){
mapIDs.add(eachmap.FieldMappingGroup__.id);
}
FieldMappings__c[] fieldMappings = [Select FieldMappingGroup__c , Name , SourceObject__c, SourceField__c, DestinationObject__c, DestinationField__c From FieldMappings__c where Id In: mapIDs];
For(String fieldName : fieldMappings )
Object1.put(fieldname, Object2.get(fieldname));
After writing all of this I've heard that Salesforce maybe introducing Reflection so this may not be necessary arrrgggghhh
No comments:
Post a Comment
Note: only a member of this blog may post a comment.