Friday, December 24, 2021

Update Custom Setting in Apex Salesforce

Many times we came across scenarios where we sometime need to insert/update/delete the custom setting in our apex logic. The below code will help you in that.

Syntax : 

<Custom Setting Name>   cs = new <Custom Setting Name>();
cs.<Custom Setting Field Name> = true;
insert cs;

Example 
Custom setting name is Test__c and one of the field name in custom setting is Active__c.

Test__c cs = new Test__c();
cs.Active__c = true;
insert cs;

Hierarchy Custom Settings
Now if you want to Insert a record in Hierarchy Custom Settings then,
Test__c cs = new Test__c();
cs.Active__c = true;
cs.SetupOwnerId = UserInfo.getUserId();
insert cs;

cs.SetupOwnerId-- You can add the user id or profile id.

Update Org Level Custom Settings

Test__c cs = Test__c.getOrgDefaults();
cs.Active__c = true;
update cs;

Please share the blog and leave a comment about your thoughts.
Click here for more blogs.










No comments:

Post a Comment