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
Deepak Tumkur ShivanandaiahDeepak Tumkur Shivanandaiah 

Save function is not setting the values Using StandardSetController

Hey,
Here is the requirement.
Basically I have to add a button to the Contact related list in the Account Layout. When that button is clicked it sould redirect to a Visualforce Page with all the related Contacts and the contact details for that Account and the details should be editable with a save button.
Its basically Mass Update for Related List Details.
I was able to get all related contacts and give inline edit support for them and wrote a save function for the same. I don't know what is wrong but you can edit but when you click save it wont save to salesforce DB.

Here is the controller,
public class stanset_EX_Controller1{
    
    private final Contact setCon;
    
    public stanset_EX_Controller1(ApexPages.StandardSetController controller)
    {
        this.setCon = (Contact) controller.getRecord();
    }

    List<Contact> Conts;

    public List<Contact> getConts(){
        return Conts;
    }

    public ApexPages.StandardSetController  doSomething{
    get {
        // do something with the selected records
        doSomething = new ApexPages.StandardSetController(
                        Database.getQueryLocator([
                                SELECT  Phone,
                                        Name,
                                        AccountId,
                                        Email,
                                        FirstName,
                                        Id,
                                        LastName,
                                        Title
                                FROM Contact
                                WHERE AccountId
                                        = :ApexPages.currentPage().getParameters().get('id')


                        ]));
     return doSomething;
 }
 private set;
}

 public List<Contact> getAccountContactsPagination() {
        //system.debug(doSomething.getRecords()+'ppp');
        return (List<Contact>) doSomething.getRecords();  
}
public PageReference save() {
        system.debug(getAccountContactsPagination());
        //update(getAccountContactsPagination());
        PageReference pageRef = ApexPages.currentPage();
        pageRef.setRedirect(true);
        return pageRef;

    }


   
    
}

Here is the Visualforce page,
 
<apex:page
  standardController="Contact"
  recordSetVar="contacts"
  extensions="stanset_EX_Controller1" showHeader="true" sidebar="false">
  <apex:form >
     <apex:pageBlock >
       <apex:pageBlockTable value="{!accountContactsPagination}" var="contact">
         <apex:column value="{!contact.name}"/>
         <apex:column value="{!contact.FirstName}"/>
         <apex:column value="{!contact.LastName}"/>
         <apex:column value="{!contact.Title}"/>
         <apex:column value="{!contact.Id}"/>
         <apex:column>
         <apex:outputField value=" {!contact.Email}">
         	<apex:inlineEditSupport disabled="false"/>
         </apex:outputField>
     	 </apex:column>
         <apex:column value="{!contact.Phone}"/>
         <apex:inlineEditSupport />
       </apex:pageBlockTable>
       <apex:pageBlockButtons location="bottom">
         <!-- <apex:commandButton value="Do Something" action="{!doSomething}"/> -->
         <apex:commandButton value="Save" action="{!save}" />
       </apex:pageBlockButtons>
     </apex:pageBlock>
  </apex:form>
</apex:page>
Tried many things dont know what is wrong with the code.
HELP!! Thanks In Advance! Cheers!
Akshay_DhimanAkshay_Dhiman
Hi, Deepak

Below is a code for the same. 
Visualforce Code :
<apex:page standardController="Account" extensions="massEditController">
    <apex:form>
        <apex:pageBlock title="My Content" mode="edit">
            <apex:pageBlockButtons>
                <apex:commandButton action="{!editMode}" value="Edit" />
                <apex:commandButton action="{!saveRecord}" value="Save"/>
            </apex:pageBlockButtons>
       		<apex:pageBlockTable value="{!cnt}" var="cn" >
                
                <apex:column rendered="{!IF(editModeInput != true, false, true)}">
                	<apex:inputField value="{!cn.id}"/>
                </apex:column>
              
                <apex:column rendered="{!IF(editModeInput != true, false, true)}">
                	<apex:inputField value="{!cn.LastName}"/>
                </apex:column>
              
                <apex:column rendered="{!IF(editModeInput != true, false, true)}">
                	<apex:inputField value="{!cn.Phone}"/>
                </apex:column>
                
                <apex:column rendered="{!IF(editModeInput != true, false, true)}">
                	<apex:inputField value="{!cn.Birthdate}"/>
                </apex:column>
                
                <apex:column rendered="{!IF(editModeOutput != true, false, true)}">
                    <apex:outputField value="{!cn.id}"/>
                </apex:column>
                <apex:column rendered="{!IF(editModeOutput != true, false, true)}">
                    <apex:outputField value="{!cn.LastName}"/>
                </apex:column>
                <apex:column rendered="{!IF(editModeOutput != true, false, true)}">
                    <apex:outputField value="{!cn.Phone}"/>
                </apex:column>
                <apex:column rendered="{!IF(editModeOutput != true, false, true)}">
                    <apex:outputField value="{!cn.Birthdate}"/>
                </apex:column>
                
            </apex:pageBlockTable>	
        </apex:pageBlock>
    </apex:form>
</apex:page>


Visualforce Controller Code :
public class massEditController {
	public Boolean editModeInput { get; set; }
    public Boolean editModeOutput { get; set; }
    public String id{get;set;}
	public List <Contact> cnt{get;set;}
    
    public massEditController(ApexPages.StandardController controller){
        editModeInput = false;
        editModeOutput = true;
        id=controller.getid();

        cnt = [select id , LastName, Phone, Birthdate from Contact where AccountId =: id ];
        
    }
    public void editMode(){
         editModeInput = true;
        editModeOutput = false;
    }
    public void saveRecord(){
        if(cnt.size()>0){
            update cnt;
            editModeOutput = true;
             editModeInput = false;
        }
    }}
Screenshot :
User-added image

User-added image

User-added image

Hope It will help you!
Regards,
Akshay

 
Sharad SoniSharad Soni
Hi Deepak,

Here is the code for your requirement.

I have an Object naming Expense below which I am having the object Expense Item.

This is the code for mass updating the Expense Item from the related list of the Expense Object.
 
<apex:page standardController="Expense_Item__c" recordSetVar="e" docType="html-5.0" >
    <apex:sectionHeader title="Edit" />
    
    <!--page form-->
    <apex:form >
        
        <apex:pageMessage rendered="{!(selected = null)}" severity="Error" summary="No record Selected">
        </apex:pageMessage>
        <!--page Block-->
        <apex:pageBlock mode="Mass Edit" rendered="{!(selected != null)}">
            
            <!--page Block Section-->
            <apex:pageBlockSection title="Edit Records" columns="1">
                
                <!--page block table-->
                
                <apex:pageBlockTable value="{!selected}" var="s" >
                    
                    <!--columns of table-->
                    <apex:column headerValue="Expense Amount" >
                        <apex:inputField type="auto"  value="{!s.Expense_Amount__c}"/> 
                    </apex:column>
                    <apex:column headerValue="Description" >
                        <apex:inputfield type="text" value="{!s.Description__c}"/>
                    </apex:column>
                    <apex:column headerValue="Bill No" >
                        <apex:inputfield type="text" value="{!s.Bill_No__c}"/>
                    </apex:column>
                    <apex:column headerValue="Date" >
                        <apex:inputfield type="date"  value="{!s.Date__c}"/>
                    </apex:column>
                    <apex:column headerValue="Expense Type" >
                        <apex:inputField type="text" value="{!s.Expense_Type__c}"/>  
                    </apex:column>
                    <apex:column headerValue="Expense " >
                        <apex:inputfield type="auto"  value="{!s.Expense__c}"/>
                    </apex:column>
                </apex:pageBlockTable>
                
            </apex:pageBlockSection>
            
            <!--Button-->
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
Deepak Tumkur ShivanandaiahDeepak Tumkur Shivanandaiah
@Akshay_Dhiman

Thanks for the quick reply mate. I knew how to do with a custom controller. But the thing was I had to give Pagination as well and it should take inputs from the user so that they can set how many records they want to see. Because of this I had to use StandardSetController which gives very easy pagination.
Thanks for the reply tough, I was able to get it done 1hr later i posted the question. My code was pretty much ok. The problem was that i used apex:pageBlockButtons tag for button and for some reason it was not working and my save function was was a bit off. All I had to use was .Save();.

Thanks you very much here is the code, same as above and some parts alterd.

  VF Page:
<apex:page standardController="Contact"
           recordSetVar="contacts"
           extensions="stanset_EX_Controller1" showHeader="true" sidebar="false" >
    <apex:form >
        <div draggable="false" class=" slds-align--absolute-center ">
            <apex:commandButton value="Save" action="{!save}" styleClass="slds-button--icon-more"/>
            
            <apex:commandButton value="Cancel" action="{!cancel}" styleClass="slds-button--icon-more"/>
        </div>

        <apex:pageBlock >
            <apex:pageBlockTable value="{!accountContactsPagination}" var="contact">
                <apex:column >
                    <apex:outputField value=" {!contact.FirstName}">
                        <apex:inlineEditSupport disabled="false"/>
                    </apex:outputField>
                </apex:column>
                <apex:column >
                    <apex:outputField value=" {!contact.LastName}">
                        <apex:inlineEditSupport disabled="false"/>
                    </apex:outputField>
                </apex:column>
                <apex:column >
                    <apex:outputField value=" {!contact.Phone}">
                        <apex:inlineEditSupport disabled="false"/>
                    </apex:outputField>
                </apex:column>
                <apex:column >
                    <apex:outputField value=" {!contact.Email}">
                        <apex:inlineEditSupport disabled="false"/>
                    </apex:outputField>
                </apex:column>
                <apex:column >
                    <apex:outputField value=" {!contact.Title}">
                        <apex:inlineEditSupport disabled="false"/>
                    </apex:outputField>
                </apex:column>
                <apex:inlineEditSupport />

            </apex:pageBlockTable>
        </apex:pageBlock>


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

StandardSetController: 
public class stanset_EX_Controller1{

    private final Contact setCon;

    public stanset_EX_Controller1(ApexPages.StandardSetController controller)
    {
        this.setCon = (Contact) controller.getRecord();
    }


    public ApexPages.StandardSetController  doSomething{


        get {
            if (doSomething == null) {

            // do something with the selected records
            doSomething = new ApexPages.StandardSetController(
                    Database.getQueryLocator([
                            SELECT Phone,
                                    Birthdate,
                                    Name,
                                    AccountId,
                                    Email,
                                    FirstName,
                                    Id,
                                    LastName,
                                    Title
                            FROM Contact
                            WHERE AccountId
                                    = :ApexPages.currentPage().getParameters().get('id')


                    ]));
        }
            return doSomething;
        }
        private set;
    }

    public List<Contact> getAccountContactsPagination() {
        System.debug(doSomething.getRecords());
        return (List<Contact>) doSomething.getRecords();
    }
    public PageReference save() {
        system.debug(getAccountContactsPagination());
      //  upsert (getAccountContactsPagination());
        doSomething.save();
        PageReference pageRef = ApexPages.currentPage();
        pageRef.setRedirect(true);
        return pageRef;

    }
}
Need to do pagination for the same.
Thnaks hope it helps you.
Cheers!