function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Scott0987Scott0987 

get sobject field from string

What I am trying to do is convert a string like opportunity.name from the string opportunity.name into the field so that I can then assign a value to that field and update it.  What I don't know how to do is take my string and make it the value for the field.  

sfdcfoxsfdcfox
String someName = opportunityRecord.Name;
// do something
opportunityRecord.Name = someName;

If using a before trigger, update will happen automatically. In an after trigger or Visualforce page, make sure you save it with an insert or update function.

Scott0987Scott0987

Not sure if I don't understand your answer or if I was not specific enough.  What I want to do is have the user put in a number of date fields in string format.  Then I want to validate those fields to make sure they are in the correct format and then plug them into the opportunity.  So I build a method that does custom validation for the strings.  I want to pass to it 2 strings string 1 being the input from the text field.  String 2 being the name of the field on the opportunity.  So the method would get 2 strings one being '12/25/2015' and the other a string that is 'opportunity.datefield'.  I would then need to convert the second date string into the field so that I could update it.

Bhawani SharmaBhawani Sharma
You can use Date.valueOf(stringValue) and then do like
opportunity.put(stringfield, value);