+ 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