+ Start a Discussion
JerryHJerryH 

Find *AND* Update Fields At Runtime

Is there any way to retrieve a WRITEABLE reference to an SObject field at runtime?

 

Yes, I've seen all of the Schema.DescribeFieldResult stuff, and it's dandy if you want a READ-ONLY reference to the object metadata, but I was hoping to find a way to remap data from an existing Salesforce standard / default field into a set of user-defined custom fields.

 

What I'd like to do is pass in two field names at runtime and have the code-behind read the value from the first field and write it to the second field -- for purposes of illustration, something like:

 

    String data = Account.[field1];

    Account.[field2] = data;

 

Does anyone have a clue about how to do this?  I could hard-code a bunch of select statements into a bunch of methods, but I'd prefer to have a general-purpose reusable solution.

 

Jerry H.

 

Best Answer chosen by Admin (Salesforce Developers) 
mikefmikef

If you want to have a piece of code that can be depolyed into any org that takes a field and puts it into another field then you need a mapping setup to go with your trigger.

 

so I would create a custom object called Field_Map__c and have three fields.

One field for Sobject name, one for Source_Field__c and one for Destiation_Field__c, all text.

 

your code would be something like this.

 

 

trigger AccountTrigger on Account(before update){

   List<Field_Map__c> fieldMaps = [select Source_Field__c, Destiation_Field__c from Field_Map__c where Sobject_Name__c = 'Account'];

    for(Account a : trigger.new){
       for(Field_Map__c fm : fieldMaps){
          a.put(fm.Destiation_Field__c, a.get(fm.Source_Field__c));
       }
    }
}

 

Please keep in mind I did not test this code, and there is a lot of things you need to do to make this production ready. But it's a start.

 

All Answers

mikefmikef

So from your post, and this is just me, but I can't tell if you want to change the metadata or the field value?

 

If you want to change the metadata then the answer is no, you can't create fields, or change schema through Apex.

 

But you can support your simple code sample like this.

 

Account acc = [select Id, Name, Field_1__c from account where ...]
String data = acc.[field1];
acc.[field2] = data;

update acc;

//or
Account acc = [select Id, Name, Field_1__c from account where ...]
String data = acc.get('field_1__c');
acc.put('field_2__c',  data);

update acc;

 

Is that what your asking?

 

JerryHJerryH

> So from your post, and this is just me

 

Nope.  You're good.  I was spectacularly unclear. :-D

 

> but I can't tell if you want to change the metadata or the field value?

 

The field value (again, sorry about that).  I'd like to be able (as generically as possible, hence the "two string" approach) to take a value out of one field and put it into another field in the same SObject.

 

My end goal is to have a small and very-easy-to-grok piece of Trigger code that I can hand to anyone with Salesforce SysAdmin priviledges and a minimal amount of experience and say "My fieldname is here, and your change this other one to your custom fieldname".

 

Maybe I'm wishing for the moon here, but I won't know for sure until I get past the "use a string as a fieldname" hurdle!

 

Jerry H.

 

mikefmikef

If you want to have a piece of code that can be depolyed into any org that takes a field and puts it into another field then you need a mapping setup to go with your trigger.

 

so I would create a custom object called Field_Map__c and have three fields.

One field for Sobject name, one for Source_Field__c and one for Destiation_Field__c, all text.

 

your code would be something like this.

 

 

trigger AccountTrigger on Account(before update){

   List<Field_Map__c> fieldMaps = [select Source_Field__c, Destiation_Field__c from Field_Map__c where Sobject_Name__c = 'Account'];

    for(Account a : trigger.new){
       for(Field_Map__c fm : fieldMaps){
          a.put(fm.Destiation_Field__c, a.get(fm.Source_Field__c));
       }
    }
}

 

Please keep in mind I did not test this code, and there is a lot of things you need to do to make this production ready. But it's a start.

 

This was selected as the best answer
JerryHJerryH

Most excellent, and thank you *very* much!

 

Jerry H.