Friday, June 1, 2012

Custom Setting


Custom Setting
 Custom Settings is an essentially custom object that is exposed in the applications cache and accessible via their own API. Custom setting will give us to use in SOQL and there is no limit of fields and also use custom settings in formula fields, validation rules, Apex code and the Web Services API.
     * Provide advanced application personalization and customization
    * Deliver efficient cached data access by user.

How to Create Custom Setting?
Go to SetupàApp Set upàDevelopàCustom SettingàNew
Custom Setting Definition Page will open enter the required fields value.
Like Label: OpportunityAction
       Object: Opportunity.
      Setting Type: List
       Visibility: Public
     Description: Created for which action.
Setting Type (A List type defines application-level data, such as country codes, that is stored in the application cache. A Hierarchy type defines personalization settings, such as default field values, that can be the overridden at the organization, profile, or user level).

Add New Two Custom Fields this declaration of Oppty fields and related list: Click on New Button
1) Label: API Name
        Data Type: Text
        Length: 200
2) Label: Object
        Data Type: Text
        Length: 200
Etc.

Click on Manage Button and enter all fields which you want to in SOQL.
1) Label: Account Name
    API Name: AccountId
    Object: Opportunity
2) Label: Agents
   API Name: Agents__c
   Object: Operations_Service__c
Etc.

Apex class Code:
Object Following example is for the fetching the fields from Custom Setting and insert the record.

List<OpportunityAction__c> FieldList =[Select API_Name__c,Object__c fromOpportunityAction__c];
Map<String,Set<String>> objectFieldsList= newMap<String,Set<String>>();
for(OpportunityAction__c field:FieldList){
            Set<String> temp= objectFieldsList.get(field.Object__c);
            if(temp!=null){
                 temp.add(field.API_Name__c);
            }
            else{
                temp = new Set<String>();
                temp.add(field.API_Name__c);
            }
            objectFieldsList.put(field.Object__c,temp);
      }

//this for loop picks up the API names of all fields against the object OpportunityAction__c in custom setings
      for(String OpptyField:objectFieldsList.get(OpportunityAction__c')){
        if(OppQuery!=''){
            OppQuery=OppQuery+' , ';
        }
        OppQuery = OppQuery + OpptyField;
      }

OppQuery ='Select ' + OppQuery + ' from OpportunityAction__c WHERE Id =\'' + ids + '\' ';

OpportunityAction__c objOppty=Database.query(OppQuery);       
      insertOpportunity = objOppty.clone(false);   

try{
insert insertOpportunity;
}catch(Exception ex){
           msg='';
           System.debug('Exception 1 : '  +ex);
           Database.rollback(sp);
      }     

Conclusion:  As above example we can fetch number of fields in our code and made dynamic SOQL. This feature is very important for our dynamic SOQL and client flexible reqiurment.

No comments:

Post a Comment