Custom settings are similar to custom objects and enable application developers to create custom sets of data, as well as create and associate custom data for an organization, profile, or specific user. All custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database. This data can then be used by formula fields, validation rules, Apex, and the SOAP API.
A custom setting like a custom object will have custom fields as per requirement. Once we have object and its fields, we can create records of that object, similarly we can create records of custom setting too(its just that we don't call it a record, otherwise it very well qualifies to be called a record).
We will call it a list of data for our reference.
If custom settings are similar to custom objects, then why do we need custom custom settings in first place?
Ans: Custom setting allows us to store set of data and access it without actually querying it in apex. So you save a select query here !
There are two types of custom settings:
Lets understand this by an example,
Say for ex. we have to store data of states and its capital cities. So a state should have its capital associated with it. For this we can create a custom setting and store data in two fields.
Custom setting default gives one field "Name" so this is our name of the state. So we need one more field to store the capital of the state "Capital".
After our fields are created its time to create the data (States and its capitals) We can create this by pressing "Manage" and then new button.
All the list of data created can be viewed by pressing manage button:
The above example was for List Type custom setting. Now let us see Hierarchical Custom settings.
Hierarchical Custom Settings are similar to List type but in this you can assign data depending on user or profile. You can also have default data.
For example,
You want to create custom setting for Incentive(you will store incentive amount). So your custom setting will have field Incentive amount. Any organisation will distribute its incentive based on profile/User.
A manager will definitely get more incentive than a Trainee.
So depending on the profile/user incentive be given.
In hierarchical custom setting you have a extra field "Location" here you have to specify the user or the profile.
Salesforce logically checks the profile/user settings for the current user and returns the specific data to that profile/User.
Now let us address the following question,
How to access custom setting in apex?
Let us access data from List Type custom setting,
In the above example for List type we had custom setting named "States and Capitals" and its api name "StatesandCapitals__c", it had field "Capital__c".
We had data set for this custom setting, so now let us access it in apex.
1. We will access all the states and their capital
2. We will access capital of a particular state
Our code as below,
Public CustomSettingMethod (){
//Get all the states and their capitals
for(StatesandCapitals__c saC: StatesAndCapitalsList ){
System.debug('State Name is #'+ saC.name +'and it s capital is'+ saC.Capital__c);
}
//Get capital of a specific state
StatesandCapitals__c CapitalOfGoa = StatesandCapitals__c.getValues('Goa');
System.debug('Capital of goa is -' + CapitalOfGoa.Capital__c );
}
Debug statements can be used to see the data.
Needless to say, please do read through the Salesforce documentation on Custom Settings : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_custom_settings.htm
A custom setting like a custom object will have custom fields as per requirement. Once we have object and its fields, we can create records of that object, similarly we can create records of custom setting too(its just that we don't call it a record, otherwise it very well qualifies to be called a record).
We will call it a list of data for our reference.
If custom settings are similar to custom objects, then why do we need custom custom settings in first place?
Ans: Custom setting allows us to store set of data and access it without actually querying it in apex. So you save a select query here !
There are two types of custom settings:
- List Custom Settings:
- A type of custom setting that provides a reusable set of static data that can be accessed across your organization. If you use a particular set of data frequently within your application, putting that data in a list custom setting streamlines access to it. Data in list settings does not vary with profile or user, but is available organization-wide. Examples of list data include two-letter state abbreviations, international dialing prefixes, and catalog numbers for products. Because the data is cached, access is low-cost and efficient: you don't have to use SOQL queries that count against your governor limits.
- Hierarchy Custom Settings:
- A type of custom setting that uses a built-in hierarchical logic that lets you “personalize” settings for specific profiles or users. The hierarchy logic checks the organization, profile, and user settings for the current user and returns the most specific, or “lowest,” value. In the hierarchy, settings for an organization are overridden by profile settings, which, in turn, are overridden by user settings.
- Limits in Custom Settings:
- 1. The total amount of cached data allowed for your organization is the lesser of these two values:
- i. 10 MB
- ii. 1 MB multiplied by the number of full-featured user licenses in your organization
- 2. Each Certified managed package gets its own separate limit in addition to your organization limit.
- 3. 300 fields per custom setting.
- 4. You can’t share a custom setting object or record.
- 5. No owner is assigned when a custom setting is created, so the owner can’t be changed.
- 6. Custom settings are a type of custom object. Each custom setting counts against the total number of custom objects available for your organization.
Lets understand this by an example,
Say for ex. we have to store data of states and its capital cities. So a state should have its capital associated with it. For this we can create a custom setting and store data in two fields.
Custom setting default gives one field "Name" so this is our name of the state. So we need one more field to store the capital of the state "Capital".
After our fields are created its time to create the data (States and its capitals) We can create this by pressing "Manage" and then new button.
All the list of data created can be viewed by pressing manage button:
The above example was for List Type custom setting. Now let us see Hierarchical Custom settings.
Hierarchical Custom Settings are similar to List type but in this you can assign data depending on user or profile. You can also have default data.
For example,
You want to create custom setting for Incentive(you will store incentive amount). So your custom setting will have field Incentive amount. Any organisation will distribute its incentive based on profile/User.
A manager will definitely get more incentive than a Trainee.
So depending on the profile/user incentive be given.
In hierarchical custom setting you have a extra field "Location" here you have to specify the user or the profile.
Salesforce logically checks the profile/user settings for the current user and returns the specific data to that profile/User.
Now let us address the following question,
How to access custom setting in apex?
Let us access data from List Type custom setting,
In the above example for List type we had custom setting named "States and Capitals" and its api name "StatesandCapitals__c", it had field "Capital__c".
We had data set for this custom setting, so now let us access it in apex.
1. We will access all the states and their capital
2. We will access capital of a particular state
Our code as below,
Public CustomSettingMethod (){
//Get all the states and their capitals
for(StatesandCapitals__c saC: StatesAndCapitalsList ){
System.debug('State Name is #'+ saC.name +'and it s capital is'+ saC.Capital__c);
}
//Get capital of a specific state
StatesandCapitals__c CapitalOfGoa = StatesandCapitals__c.getValues('Goa');
System.debug('Capital of goa is -' + CapitalOfGoa.Capital__c );
}
Debug statements can be used to see the data.
Needless to say, please do read through the Salesforce documentation on Custom Settings : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_custom_settings.htm
No comments:
Post a Comment