+ Start a Discussion
XmanXman 

decides which field to be changed automaticly

For example, I have two sobjects as below:

SobjectA__c, which has three fields: column1__c, column2__c, column3__c

SobjectB__c, which has one field: column_name__c

 

What I want to do is,  I get the value of column_name__c from SobjectA__c and run a trigger for SobjectB__c.

If  column_name__c's value is "column1__c" then I set the field "column1__c" 's value to 1.

If the value is "column2__c" then I set the field "column2__c" 's value to 1, and so on...

The value decides which field to be changed.

 

Could anyone tell me Is this consideration possible? If it's possible, how to achieve it?

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can use dynamic DML for this - that allows you to get/set field values based on the field name.

 

E.g. 

 

Sobject  opp=[select id, Name from Opportunity limit 1];

String name=opp.get('Name');

name=name+' Test';

opp.put('Name', name);

 

There's more information at:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_dml.htm

All Answers

bob_buzzardbob_buzzard

You can use dynamic DML for this - that allows you to get/set field values based on the field name.

 

E.g. 

 

Sobject  opp=[select id, Name from Opportunity limit 1];

String name=opp.get('Name');

name=name+' Test';

opp.put('Name', name);

 

There's more information at:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_dml.htm

This was selected as the best answer
Damien_Damien_

I'm assuming that column_name__c is a lookup to SobjectA__c.  If this is the case, you can query for your SobjectA__c from your SobjectB__c query, and then do your work on it.

 

Trigger help:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_trigger.htm

XmanXman

Thank you, bob buzzard!

That's what I need!!