Friday, September 14, 2018

Custom settings in salesforce


Custom setting are similar to custom object in salesforce, We can store some custom data inside this and can access this inside apex class avoiding use of multiple soql queries. 


Custom settings in salesforce


1)List custom settings 
2)Hierarchy custom settings

 List custom settings:



List custom settings


List custom settings have user-independent values.

Methods available with list custom settings are: 

1)getAll(); 
2)getValues(); 

Syntax: 


1) Map<Data_set_name, CustomSettingName__c> cus = CustomSettingName__c.getAll(); 

The getAll method returns values from all fields associated with list custom setting. 

2)CustomSettingName__c cus = CustomSettingName__c.getValues(Data_set_name); 

The getValues method returns values associated with specific data set. 

This method can be used with both list and hierarchy custom settings. 


Hierarchy custom settings: 


Hierarchy custom settings


Hierarchy custom settings let's you personalize setting for specific role and profile. 



Methods available with Hierarchy custom settings are:

1)getOrgDefaults();
2)getInstance();

Syntax: 



1)CustomSettingName__c cus = CustomSettingName__c.getOrgDefaults();

The above method returns values from data set at organization level.

2)CustomSettingName__c cus = CustomSettingName__c.getInstance(Profile_ID or UserID);

The above method returns data set values related to specific profiles.


SAMPLE EXAMPLE:


Let say we have List Custom Setting called employee where we are storing employee roll number. 

Let say we have some employees information stored in custom setting.

employee1     rollNo1 
employee2    rollNo2 

Syntax: 

To get rollNo for employee 1,


Employee__c obj=new Employee__c.getvalues('employee1'); 
String rolNo=obj.rollNo1__c; 

No comments:

Post a Comment