Please mark it as best answer if the information is informative so that question is removed from an unanswered question and appear as a proper solution to similar kind of questions.
Detailed Introduction how to use custom setting and why
Lets see how we can accomplish it using Custom Settings.
Step 1: Create Custom Setting. Click Setup-->Develop --> Custom Settings -->New and then enter the fields as mentioned in the snippet below
Click on Save. This creates a custom object in the background. If you notice the API name is followed by __c same as in the case of a custom object.
Step 2: Create a custom checkbox field which would be used to indicate whether the output panel is enable for the profile. Click New in the custom fields section and choose checkbox and click next. Give the field label and name as 'Enabled' and click next followed by Save. It should look the same as shown in snapshot below
Step 3: Click on manage and create records as shown in the image. Create multiple records for all the profiles in your org . The Checkbox field indicates whether the output panel in the VFpage is visible.
Custom settings can be accessed using their own instance methods . getinstance() method can be used to get the record value. The records are accessed using the Name field instead of the id .
To get the list of all records of the custom setting use list<DisplayPanel__c> ListDisp = DisplayPanel__c.getAll().values();
Now create apex page and controller as shown below:
Visualforce Page:
<apex:page controller="CustomSettingsDemoExt">
<apex:outputPanel rendered="{!Enable}" >
This panel is displayed when allowed for the profile in Custom settigs
</apex:outputPanel>
</apex:page>
Controller
public with sharing class CustomSettingsDemoExt {
public boolean Enable{get;set;}
public CustomSettingsDemoExt () {
Id profileId = UserInfo.getProfileId();
String profileName = [Select Name from Profile where Id =:profileId].name; // Get the current user profile Name
Map<string,DisplayPanel__c> DisplayMap= DisplayPanel__c.getAll();
Enable= DisplayMap.get(profileName ).Enabled__c; // display the permission for current profile
}
}
Now you can easily toggle the display of this output panel from Custom settings for all profiles instead of altering the code.
I Hope This information is useful for you.please select my answer as best answer. Thanks varun
I would like to update custom setting field without using visualforce. I am writting following code to insert the manage field in custom setting but I am not sure how to execute this class to insert the values in Custom setting. Do I have to go for Batch Apex?
public class custsetting { List<samplelist__c> slist= new List<samplelist__c>(); public void updatesetting(){ samplelist__c cs=new samplelist__c(); cs.name='testing1'; slist.add(cs); insert slist; }
I forget To Insert Custom Setting in above controller ,Now i have modifief it.
public with sharing class CustomSettingsDemoExt {
public boolean Enable{get;set;}
List<DisplayPanel__C> CusList=New List<DisplayPanel__C> ();
public CustomSettingsDemoExt () {
//Method to insert custom Setting
insertCusSetting();
Id profileId = UserInfo.getProfileId();
String profileName = [Select Name from Profile where Id =:profileId].name; // Get the current user profile Name
Map<string,DisplayPanel__c> DisplayMap= DisplayPanel__c.getAll();
Enable= DisplayMap.get(profileName ).Enabled__c; // display the permission for current profile
}
public void insertCusSetting(){
Displaypanel__c cusfiled=new List<DisplayPanel__C> ();
cusfiled.name='Custom filed1';
CusList.add(cusfiled);
insert CusList.;
}
}
Thank you.. I am confused How should I execute Apex code to insert name in custom setting? (I do not want to go for visual force).. do I need write this code in Batch Apex to execute?
Using Apex for custom setting Insert, Update and delete
For insert
samplelist__c sList = new samplelist__c(name='xxxxxxxxxxxx');
insert sList;
For Update
You can do a query to get the id or you can use the below code to get the id
samplelist__c s = samplelist__c.getInstance('xxxxxxxxxxxx');
String sampleListId = s.id;
Currently I have hardcoded the id
samplelist__c sList = new samplelist__c(id='a0190000000Zrai',name='xxxxxxxxxxxx');
update sList;
For delete
samplelist__c s = [select id,name from samplelist__c where name='xxxxxxxxxxxx'];delete s;
or
List<samplelist__c> mcs = samplelist__c.getall().values();
delete mcs;
Please refer the below link for reference.
- http://blogatsalesforce.blogspot.sg/2015/03/use-custom-setting-efficiently-in-apex.html
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm
Hope it will be helpful.Please mark it as best answer if the information is informative so that question is removed from an unanswered question and appear as a proper solution to similar kind of questions.
Thanks
Rahul Kumar
Detailed Introduction how to use custom setting and why
Lets see how we can accomplish it using Custom Settings.
Step 1: Create Custom Setting. Click Setup-->Develop --> Custom Settings -->New and then enter the fields as mentioned in the snippet below
Click on Save. This creates a custom object in the background. If you notice the API name is followed by __c same as in the case of a custom object.
Step 2: Create a custom checkbox field which would be used to indicate whether the output panel is enable for the profile. Click New in the custom fields section and choose checkbox and click next. Give the field label and name as 'Enabled' and click next followed by Save.
It should look the same as shown in snapshot below
Step 3: Click on manage and create records as shown in the image. Create multiple records for all the profiles in your org . The Checkbox field indicates whether the output panel in the VFpage is visible.
Custom settings can be accessed using their own instance methods . getinstance() method can be used to get the record value. The records are accessed using the Name field instead of the id .
DislayPanel__c disp= DislayPanel__c .getInstance('System Administrator');
To get the list of all records of the custom setting use
list<DisplayPanel__c> ListDisp = DisplayPanel__c.getAll().values();
Now create apex page and controller as shown below:
Visualforce Page:
Controller
Now you can easily toggle the display of this output panel from Custom settings for all profiles instead of altering the code.
I Hope This information is useful for you.please select my answer as best answer.
Thanks
varun
I am writting following code to insert the manage field in custom setting but I am not sure how to execute this class to insert the values in Custom setting. Do I have to go for Batch Apex?
public class custsetting {
List<samplelist__c> slist= new List<samplelist__c>();
public void updatesetting(){
samplelist__c cs=new samplelist__c();
cs.name='testing1';
slist.add(cs);
insert slist;
}
}
I forget To Insert Custom Setting in above controller ,Now i have modifief it.
For All About Custome Setting See my above post.
(I do not want to go for visual force).. do I need write this code in Batch Apex to execute?
Test__c t = Test__c.getInstance('TT');
t.Size__c = 908;
update t;
Here Test__c is a Custom Settings.
Cheers!!!
I have written this code in Apex class, still I cann't see updated URL in custom setting
samplelist__c slist= samplelist__c.getInstance('Sample');
slist.URL__c='https://xyz.sfsdfsd.com';
update slist;
samplelist__c slist= samplelist__c.getInstance('Sample');
slist.URL__c='https://xyz.sfsdfsd.com';
update slist;