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
JayaJayaJayaJaya 

Query on Dynamic Apex

Hi,

I have a custom object say CusObj__c and a custom checkbox field for it say CusFld__c.
If in Apex, I have a String which contains the API Name of the above custom field, i.e. say String x='CusFld__c' then how can I save the record of CusObj__c with data for that field.

For ex:
CusObj__c c = new CusObj__c();
String x = 'CusFld__c';
c.x=false;// This is not allowed, how to fix it??
insert c;

Thanks.
Best Answer chosen by JayaJaya
hitesh90hitesh90
Hello Jaya,

You have to use Following code as per your requirement.
because your custom field is not unique it's dynamic. try to use this.

CusObj__c c = new CusObj__c();
String x = 'CusFld__c';
c.Put(x,true);
insert c;

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

All Answers

Ashish_SFDCAshish_SFDC
Hi Jaya,


Are you getting any error while compiling this code?

This is shown to be working, see below, 

SObject fields can be accessed or changed with simple dot notation. For example:
1 Account a = new Account();
2 Contact c = new Contact();
3 System.debug(a.name);
4 a.name = 'xxx';
5 System.debug(c.account.name);

http://wiki.developerforce.com/page/Apex_Code:_The_Basics

Regards,
Ashish
hitesh90hitesh90
Hello Jaya,

You have to use Following code as per your requirement.
because your custom field is not unique it's dynamic. try to use this.

CusObj__c c = new CusObj__c();
String x = 'CusFld__c';
c.Put(x,true);
insert c;

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/
This was selected as the best answer