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
klabklab 

Bind Field API name to sObject instance with string

I'm looking for a way to dynamically bind a string's value, which contains a Field API Name, with an sObject instance variable and set the value of the sobject field to yet another string.  Here is what I mean.

 

Here is what I can do.

 

My_Custom_Object__c cRecord = new My_Custom_Object__c (); 
cRecord.field1__c = 'field value'; 
String X = cRecord.field1__c;

 So String X would be equal to 'field value'.

 

Here is what I'd like to do.

 

My_Custom_Object__c cRecord = new My_Custom_Object__c ();
cRecord.field1__c = 'field value';
String Y = 'field1__c';
String X = cRecord.Y;

 

Clearly the error I get is that the Y field does not exist, so is there a special notation for doing this?  I've seen similar things done in dynamic SOQL, but not in APEX.  http://salesforce.stackexchange.com/questions/8408/bind-variable-with-dotted-name-not-working-in-dynamic-soql.
 
Any ideas?  Is there some method like, value(Y),  for cRecord.value(Y)? Just some method that passes on the value of the string instead of the dot notation looking for a field named 'Y'?
 
Thank you!
 
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Your below code shows error because its not possible to access a string Y, that is not a field in the object.

 

My_Custom_Object__c cRecord = new My_Custom_Object__c ();
cRecord.field1__c = 'field value';
String Y = 'field1__c';
String X = cRecord.Y;

Eventhough, you give the field API name for the String Y, it will not be considered as an API name, instead it will be a string alone. So, when you try to bind the string Y to the object instance, it refers the object for the field name Y, it will not be there, So it shows you the error doesn't exists.

 

So, you first need to understand that an instance of the object can only access the field API names exists for that object.

 

You can modify your code as follows if you want to give string value of Y to X,

 

My_Custom_Object__c cRecord = new My_Custom_Object__c ();
cRecord.field1__c = 'field value';
String Y = cRecord.field1__c;
String X = Y;

 

Please give kudos and mark this as a solution, If you found this answer as useful to others as well.

klabklab

Hi,

 

Thank you for your reply.

 

I am not trying to set String Y equal to String X.  What I'm actually trying to do is use the value of String Y appended in dot notation to cRecord to grab the field in the custom object and set its field value to String X.

 

I'm just not sure how or if this is possible.

 

Thank you again

 

 

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi klab,

That's why, I mentioned this is not possible.

Because, it will consider the Y as a field (will not consider Y as field1__c) and check in that object.

 

Please give kudos and mark this as a solution, If you found this answer as useful to others as well.

klabklab

Thank you.

 

Too bad there isn't a notation that makes this work, because similiar binding works with dynamic SOQL queries as mentioned in the link above.

 

This would be useful for looping through a list of strings which hold api field names.  Then one could bind the api name stored in the string to an sObject record for dynamic referencing of a field's value.   In one case this would be nice, woudl be grabbing all fields api names of a fieldset and looping through them to find the values of the sObject record and doing some calculations with them. One could build a dynamic query to do it, but if one can get away without querying, that would be a preferred method.

 

Thank you

klabklab

Here's a good post on the subject http://salesforce.stackexchange.com/questions/4978/not-getting-how-fieldset-works-in-salesforce.

 

I just can't get the obj.get(fieldName) to work outside of system.debug();  I get an illegal assignment trying to set it to anything to actually store the value of the object field.

 

 

klabklab

It should be the following to work

 

My_Custom_Object__c cRecord = new My_Custom_Object__c ();
cRecord.field1__c = 'field value';
String Y = 'field1__c';
String X = {String)cRecord.get(Y);

 

 

 This post touches on it a bit. http://boards.developerforce.com/t5/Apex-Code-Development/Ilegal-assignment-from-Schema-SObjectField-to-String/td-p/180541

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Klab,

 

Actually, the url you mentioned is saying about the Fieldset usage. Field set is used to hold a set of fields in an object, that can be customized to include or exclude from the VF page display & reference in controller without affecting any functionality or changing the code.

 

You can try the following, but it just says that fieldname dynamically referred in the controller to assign the field value to a string.

 

Map<String, Schema.SObjectField> M = Schema.SObjectType.Account.fields.getMap();
List<String> allFieldNames = new List<String>();
allFieldNames.addAll(m.keyset());


Account obj = Database.query('SELECT ' + String.join(allFieldNames,',') + ' FROM Account LIMIT 1');
String Y;
for(String fieldName : allFieldNames){
   Y = obj.get(fieldname);
   String X = Y;
}

 

If the above explaination is not enough to solve your problem, Please mention your requirement in brief.

 

Hope this helps you...!

Please give kudos and mark this as a solution, If you found this answer as useful to others as well.

 

 

 

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Klab,

I tried your code and it give me success.


Try the following,
VF page:
<apex:page controller="DynamicFieldCon">
     <apex:form>
         <apex:outputtext value="{!X}"/>
     </apex:form>
</apex:page>

Controller:
public class DynamicFieldCon{


    public Date X{get;set;}

   
    public DynamicFieldCon(){
        First__c obj = new First__c();
        obj.DateField__c = Date.Today();
        String Y = 'DateField__c';
        X = (Date)obj.get(Y);
    }
}

 


Am having an object First__c. In the controller i give the value for a field DateField__c in the First__c object. Now, i gave the API name to string and assigned it to X as you needed. It works now...!

 

Hope this helps you...!

Please give kudos and mark this as a solution, If you found this answer as useful to others as well.

 

klabklab

Thank you.

 

The requirement does use fieldSets, but the crux of the problem was was the same. You start with a map to get fields or fieldSets. The post used a map of fields,

 

Schema.SObjectType.Account.fields.getMap()

where with fieldSets, you use a map like

 

Schema.SObjectType.Account.fieldSets.getMap()

 In the end, if you want to use the results in the map to assign a value of an sobject field to any variable, the (String)object.get(stringVariable) syntax will have to be used.

 

I may not be speaking your language, but the application didn't matter, it was the syntax that was the question.

 

Thank you again for all your replies!

 

 

Bnt SoftBnt Soft
Hello,
Use  ObjectInstance.put(stringVariable, value);
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm
Hope this helps you...!
Thanks