function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lena ChristyLena Christy 

Override the save button for custom setting

Hi all,
I have an issue, I think ut is simple but I could not do it, I hope you could help me,
Here is the issue, 
I made a vf page to display the value fields of a list custom setting "csObject__c" and editing its fields.

I want to make only one record which name="param" where i will modify its fields from the vf page, it's like in the beginning I put only the name 'param' in custom settings, then I can modify the other fields, the important it should be one only record which is Name='param'

<apex:page standardController="csObject__c" recordSetVar="CSparam" extensions="Ctrl">

<apex:form >

<apex:pageBlock >

<apex:pageBlockButtons >
 <apex:commandButton value="Save" action="{!save}" "/>
</apex:pageBlockButtons>


<apex:pageBlockSection  >
<apex:pageBlockTable value="{!CSparam}" var="a" > 
  <apex:column headerValue="Field 1 ">
  <apex:inputField value="{!a.Field1__c}" />
  </apex:column>
  </apex:pageBlockTable>
  </apex:pageBlockSection>

//here I have other field of the custom setting

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

And for the save button I  want to override it to redirect to another vf page:

public with sharing class Ctrl {

    public Ctrl (ApexPages.StandardSetController controller) {

    }
    
    public PageReference  save() {
       csObject__c  cs = csObject__c.getValues('param');
       update cs;
       PageReference pageRef = new PageReference('/apex/VF');
       return pageRef;
   }

    
}

But it doesn't work I still have the old values of the cs fields...

If anyone could help me with this, it would be great...

Thanks!

Best Answer chosen by Lena Christy
Emmanuel Cruz BEmmanuel Cruz B
Since you are going to use your own save method, you don't need to use a standard controller. Also, with your save method you are calling a new set of values and that won't save the values that you already entered in the vfp. Another important thing is that you are not going to save only one csObject__c, you need to save a list of csObject__c because you are getting a list of custom settings ("recordSetVar=CSparam").

What I would do is modify the vfp where your extension class becomes the controller:
<apex:page controller="Ctrl">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons>
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
            
            <apex:pageBlockSection>
                <apex:pageBlockTable value="{!CsObjectList}" var="a" > 
                    <apex:column headerValue="Field 1 ">
                        <apex:inputField value="{!a.Field1__c}" />
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form> 
</apex:page>

Then in the controller class, get the CsObjectList that your table will use as data source and remove the getValues line from your save method.
public with sharing class Ctrl
{
    public List<csObject__c> CsObjectList {get;set;}
    
    public Ctrl (){
        CsObjectList = csObject__c.getall().values();
    }
    
    public PageReference save() {
       update CsObjectList;
       PageReference pageRef = new PageReference('/apex/VF');
       return pageRef;
   }
}
Now, you said that you want to modify only the csObject llamado "param", for that you will need to override the getall().values() with a SOQL
public csObjectCtrl (){
    CsObjectList = [SELECT Field1__c, Fieldn__c... FROM csObject__c WHERE name = 'param' LIMIT 1];
}
With this your list of custom settings will have only one record.

Hope this helps you
 

All Answers

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Lena,

You can use the inline edit. Try the following snippet -
Apex Controller -
public with sharing class Ctrl {
    public List<csObject__c> CSList;
    public Ctrl(ApexPages.StandardSetController controller) {
        CSList = (List<csObject__c>) controller.getRecords();
    }

    public PageReference  save() {
        Integer counter = 0;
        System.debug(CSList);
        for(csObject__c cs : CSList) {
            counter = cs.name.equalsIgnoreCase('param') ? ++counter : counter;
            if(counter > 1) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Param value cannot be more than one'));
                return null;
            }
        }  
        update CSList;    
        PageReference pageRef = new PageReference('/apex/VF');
        return pageRef;
    }   
}
VF - 
<apex:page standardController="csObject__c" recordSetVar="CSparam" extensions="Ctrl" >
<apex:pageMessages ></apex:pageMessages>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection >
            <apex:pageBlockTable value="{!CSparam}" var="i">
                     <apex:column headerValue="Name" > 
                         <apex:outputField value="{!i.name}">
                             <apex:inlineEditSupport event="ondblClick" /> 
                         </apex:outputField>
                     </apex:column>
                     <apex:column headerValue="Name" > 
                         <apex:outputField value="{!i.value__c}">
                             <apex:inlineEditSupport event="ondblClick" /> 
                         </apex:outputField>
                     </apex:column>
                </apex:pageBlockTable>   
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form> 
</apex:page>
Hope, it will help you.

Thanks, 
Sumit Kuamr Singh
Emmanuel Cruz BEmmanuel Cruz B
Since you are going to use your own save method, you don't need to use a standard controller. Also, with your save method you are calling a new set of values and that won't save the values that you already entered in the vfp. Another important thing is that you are not going to save only one csObject__c, you need to save a list of csObject__c because you are getting a list of custom settings ("recordSetVar=CSparam").

What I would do is modify the vfp where your extension class becomes the controller:
<apex:page controller="Ctrl">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons>
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
            
            <apex:pageBlockSection>
                <apex:pageBlockTable value="{!CsObjectList}" var="a" > 
                    <apex:column headerValue="Field 1 ">
                        <apex:inputField value="{!a.Field1__c}" />
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form> 
</apex:page>

Then in the controller class, get the CsObjectList that your table will use as data source and remove the getValues line from your save method.
public with sharing class Ctrl
{
    public List<csObject__c> CsObjectList {get;set;}
    
    public Ctrl (){
        CsObjectList = csObject__c.getall().values();
    }
    
    public PageReference save() {
       update CsObjectList;
       PageReference pageRef = new PageReference('/apex/VF');
       return pageRef;
   }
}
Now, you said that you want to modify only the csObject llamado "param", for that you will need to override the getall().values() with a SOQL
public csObjectCtrl (){
    CsObjectList = [SELECT Field1__c, Fieldn__c... FROM csObject__c WHERE name = 'param' LIMIT 1];
}
With this your list of custom settings will have only one record.

Hope this helps you
 
This was selected as the best answer
Lena ChristyLena Christy
Thank you Sumit Kuamr Singh and Emmanuel so much for your replies !!
Respect.
Lena ChristyLena Christy

Please I have a problem, for example if i don't have this 'Param' record in cs I want that it will be created with the same name then edit the other fields, if it's already created we can modify directly its fields

Any help?

Emmanuel Cruz BEmmanuel Cruz B
Hi Lena,

What you can do is verify the list size, if the list size is empty you can create a new cs named 'param'. You will also need to modify the update statement in your save method for upsert, with this you will insert or update the record..
public csObjectCtrl (){
    CsObjectList = [SELECT Field1__c, Fieldn__c... FROM csObject__c WHERE name = 'param' LIMIT 1];
    if(CsObjectList.size() > 0){
        CsObjectList.add(new csObject__c(Name = 'param');
    }
}
Lena ChristyLena Christy
Thank you so much Emmanuel, you are the best!