Tuesday 9 December 2014

How to Update custom setting without granting them Customize Application access


Requirement :-
Recently I was working on one requirement where i need to provide the user to update custom setting data without providing the customize application access because customize application is also depend on manager translation,view setup and configuration. But on that profile we dnt want to provide View Setup.
Then i decided to write a visual force page. But when we bind the custom setting with <apex:inputField> field was also coming in read only mode. For that I come up with below solution.

Solution :-
If we will bind custom setting directly with <apex:inputField without customize application access then field will come in read only mode. For that we need to mapped our custom setting data in Wrapper class or variable.

Page :-


<apex:page controller="CustomSettingController">
<apex:form >
<apex:pageBlock mode="edit" id="PB" title="Custom Settings">
<apex:pageMessages />

<apex:pageBlockButtons location="Top">
    <apex:commandButton action="{!Edit}" value="Edit" rendered="{!Not(isEdit)}" reRender="PB"/>
    <apex:commandButton action="{!Save}" value="Save" rendered="{!isEdit}" reRender="PB"/>
    <apex:commandButton action="{!Cancel}" value="Cancel" rendered="{!isEdit}" reRender="PB"/>
</apex:pageBlockButtons>

<apex:pageBlockSection rendered="{!Not(isEdit)}" id="PB1" >
 <apex:outputField value="{!custSetting.Number_Of_Days__c}"/>         
 <apex:outputField value="{!custSetting.User_name__c}"/>            
</apex:pageBlockSection>

<apex:pageBlockSection rendered="{!isEdit}" id="PB2" title="Update Commerce Settings Data">
   <apex:pageBlockSectionItem >
    <apex:outputLabel value="{!$ObjectType.Custome_Settings__c.fields.Number_Of_Days__c.Label}" />            
    <apex:inputText value="{!number_Of_Days}"/>            
 </apex:pageBlockSectionItem> 
 <apex:pageBlockSectionItem >
  <apex:outputLabel value="{!$ObjectType.Custome_Settings__c.fields.User_name__c.Label}" />            
  <apex:inputText value="{!strName}"/>            
</apex:pageBlockSectionItem>

</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>
</apex:page>


Class:-

public with sharing class CustomSettingController {

public Custome_Settings__c custSetting {get;set;}
public Boolean isEdit {get;set;}
public Decimal number_Of_Days{get;set;}
public String strName{get;set;}


    public CustomSettingController()
    {
        custSetting = [select id , Number_Of_Days__c,User_name__c from Custome_Settings__c limit 1];

        number_Of_Days= custSetting.Number_Of_Days__c;
        strName = custSetting.User_name__c ;
    
        isEdit = false;
    }
    
    public void Cancel()
    {
        isEdit = false;
    }

    public void Edit()
    {
        isEdit = true;
    }
    
    public void Save()
    {
       try
       { 
        custSetting.Number_Of_Days__c =number_Of_Days;
        custSetting.User_name__c =strName;

        update custSetting;
        isEdit = false;
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Data Saved Successfuly !!!' ) );
       }Catch(Exception ee){
           ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ee.getMessage()) );
       } 
    }
    

}






Thanks,
Amit Chaudhary

No comments:

Post a Comment