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
MohandaasMohandaas 

Update Custom Setting Field Value

I have a Hierarchy Custom setting with one field IsProcessed.

 

My batch apex will run only when the value of  IsProcessed is true. After processing I need to update the value to false. I doubt whether it is possible.

 

Is there a way I can update a custom setting field value using apex.

 

Appreciate your help.

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Yes you can update custom settings using Apex. Custom settings are just Custom Objects that have been specialised in certain specific ways. You can Retrieve custom setting records using either CustomSetting__c.get('value') or [SELECT id FROM CustomSetting__c WHERE name = 'value'];

 

Once retrieved you can update the setting by using the 'update' keyword as you would with any other object.

 

Cheers,

Wes

All Answers

NaishadhNaishadh

Custom setting is like global utiliity data and ideally they are not for updating. do you have any specific reason for using custom setting in place of using some class level variable?

WesNolte__cWesNolte__c

Yes you can update custom settings using Apex. Custom settings are just Custom Objects that have been specialised in certain specific ways. You can Retrieve custom setting records using either CustomSetting__c.get('value') or [SELECT id FROM CustomSetting__c WHERE name = 'value'];

 

Once retrieved you can update the setting by using the 'update' keyword as you would with any other object.

 

Cheers,

Wes

This was selected as the best answer
MohandaasMohandaas

Thanks Wes.

 

I do not know we can use an update statement. I was looking for a custom setting method.

Vaclav LukasekVaclav Lukasek
for example if you wanna store last update of cutom object without usage of Custom Object(for example if I sync sf with external system and i need to store last succes update time stamp)
Kapil BatraKapil Batra
Hello,

Please check below code to update custom settings from Apex.
public class updateCustomSettingsCtrl {
    public updateCustomSettingsCtrl(){
        mySettings__c obj = mySettings__c.getInstance('myCustomField');
        obj.fieldName__c = 'New Data';
        update obj;
    }
}

Reference : https://www.salesforcebolt.com/2020/05/update-value-of-custom-settings-from.html
Richard Fiekowsky 3Richard Fiekowsky 3
@Naishadh we have custom settings which toggle automation bypasses, which are useful to avoid governor limits in test classes. So in a test, we might turn off process builders and then turn them back on by updating the setting. I would guess that the changes would be rolled back automatically after the test ends, but I code the reversion to original value anyway.  
sanket mahajan 1sanket mahajan 1
Hi Mohandaas,

Please find the solution in the below link:
https://sfdc-concepts.blogspot.com/2021/12/update-custom-settings-in-apex.html?m=1