You need to sign in to do that
Don't have an account?

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.
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.
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
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
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/