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
Sunny MohlaSunny Mohla 

Pick field names from custom setting

I have a custom setting which saves three field names of lets say contact object. I want to update value of all those fields to null which are store in my custom setting. following is the code that i'm writing.

List<Field_Name__c> mycustomsetting = Field_Name__c.getAll().values();
system.debug('List of custom setting'  + mycustomsetting.size());
List<contact> mycontacts = [select id, name,birthdate, Languages__c, Level__c from contact];
string fieldname = '';
for(contact Con : mycontacts)
{
    for(integer i=0; i<mycustomsetting.size();i++)
    {
        system.debug(i+ ' :custom setting : ' + mycustomsetting[i].Field__c);   
        fieldname = mycustomsetting[i].Field__c;
        Con. + fieldname = null; - This is what i'm trying to do but it does not works
        Con.birthdate = null; -- This is what needs to be done via custom setting
    }
    update Con;
}

I have created custom setting as "Field_Name__c" and this contains field as "Field__c" which contains contact field API names.

Any help will be appreciated
Best Answer chosen by Sunny Mohla
Arthur LockremArthur Lockrem
try using a put statement instead.
Con.put(fieldname, null);

All Answers

Arthur LockremArthur Lockrem
try using a put statement instead.
Con.put(fieldname, null);
This was selected as the best answer
Sunny MohlaSunny Mohla
Hi Arthur,

This does seems to work in developer console. Will apply in my batch class as well.