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
VinuVinu 

How to update visualforce page

How to update visualforce page while clicking save button??????

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

There's a couple of things that jump out at me:

 

In your save method, you have the following:

 

selectedAccounts.clear();
update selectedAccounts;

 This won't have any effect as you are emptying the list via the clear statement before executing the update.

 

In getAccounts you are adding a new instance of accountwrapper to the list every time  - this is causing the doubling up:

 

public List<accountwrapper> getAccounts()
{
for(Attendance__c a : [SELECT Name, date__c, Excused_Absence__c, Excuse_Details__c,
Present__c, Rubric__c,Session__c, Youth__r.LastName FROM Attendance__c
ORDER BY Youth__r.LastName asc ]
)

accountList.add(new accountwrapper(a));
return accountList;
}

 

 

All Answers

bob_buzzardbob_buzzard

Can you explain a little more about your use case - do you just want to refresh the page with the latest data or dynamically change parts of the page?

VinuVinu

The Actual process is " I just wanted to update only the selected values through the checkbox" And the updation works fine, But the problem here is im getting the repeated values in the current form. If i just refresh the form by manually, it just shows a single record.. What should i do for this?????

bob_buzzardbob_buzzard

There could be a number of reasons for this.  Can you post some of your code?

VinuVinu

Controller: 

 

public class Checkbox_Class
{

public PageReference save()
{
try
{
selectedAccounts.clear();
update selectedAccounts;
}
catch(DmlException ex)
{
ApexPages.addMessages(ex);
}
return null;
}

public PageReference cancel()
{
return null;
}

List<accountwrapper> accountList = new List<accountwrapper>();
List<Attendance__c> selectedAccounts = new List<Attendance__c>();

public List<accountwrapper> getAccounts()
{
for(Attendance__c a : [SELECT Name, date__c, Excused_Absence__c, Excuse_Details__c,
Present__c, Rubric__c,Session__c, Youth__r.LastName FROM Attendance__c
ORDER BY Youth__r.LastName asc ]
)

accountList.add(new accountwrapper(a));
return accountList;
}

public PageReference getSelected()
{
selectedAccounts.clear();
for(accountwrapper accwrapper : accountList)
if(accwrapper.selected == true)
selectedAccounts.add(accwrapper.acc);
System.debug('selectedAccounts:'+ selectedAccounts);
return null;
}
public List<Attendance__c> GetSelectedAccounts()
{
if(selectedAccounts.size()>0)
return selectedAccounts;
else
return null;
}
public class accountwrapper
{
public Attendance__c acc{get; set;}
public Boolean selected {get; set;}
public accountwrapper(Attendance__c a)
{
acc = a;
selected = false;
}
}
}

 

 

Visualforce page:

 

<apex:page controller="Checkbox_Class" Tabstyle="Contact" sidebar="false" showHeader="false">
<apex:form >

<apex:pageBlock Title="Selected value with CheckBoxes">

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" OnClick="Reload()" />
<apex:commandButton value="Cancel" action="{!cancel}" onclick="confirmcancel()"/>
</apex:pageBlockButtons>

<apex:pageBlockTable value="{!Accounts}" var="a" >
<apex:column >
<apex:facet name="header">

<apex:inputCheckbox >
<apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="checkAll(this)" rerender="Selected_PBS"/>
</apex:inputCheckbox>
</apex:facet>

<apex:inputCheckbox value="{!a.selected}" id="checkedone">
<apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/>
</apex:inputCheckbox>
</apex:column>

<apex:column headervalue="Name" value="{!a.acc.Name}" />
<apex:column headervalue="Youth Last Name" value="{!a.acc.Youth__r.LastName}" />

<apex:column headervalue="Excused Absence">
<apex:inputfield value="{!a.acc.Excused_Absence__c}" />
</apex:column>

<apex:column headervalue="Present">
<apex:inputfield value="{!a.acc.Present__c}" />
</apex:column>

<apex:column headervalue="Excuse Details">
<apex:inputField value="{!a.acc.Excuse_Details__c}" />
</apex:column>

<apex:column headervalue="Rubric (#)">
<apex:inputfield value="{!a.acc.Rubric__c}" />
</apex:column>

<apex:column headervalue="Session" value="{!a.acc.Session__c}" />
<apex:column headervalue="Date" value="{!a.acc.date__c}" />
</apex:pageBlockTable>

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


<script>
function checkAll(cb)
{
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{
if(inputElem[i].id.indexOf("checkedone")!=-1)
inputElem[i].checked = cb.checked;
}
}
</script>
</apex:page>

bob_buzzardbob_buzzard

There's a couple of things that jump out at me:

 

In your save method, you have the following:

 

selectedAccounts.clear();
update selectedAccounts;

 This won't have any effect as you are emptying the list via the clear statement before executing the update.

 

In getAccounts you are adding a new instance of accountwrapper to the list every time  - this is causing the doubling up:

 

public List<accountwrapper> getAccounts()
{
for(Attendance__c a : [SELECT Name, date__c, Excused_Absence__c, Excuse_Details__c,
Present__c, Rubric__c,Session__c, Youth__r.LastName FROM Attendance__c
ORDER BY Youth__r.LastName asc ]
)

accountList.add(new accountwrapper(a));
return accountList;
}

 

 

This was selected as the best answer
VinuVinu

If possible, Could you please suggest any of the alter way to avoid or to solve this problem

bob_buzzardbob_buzzard

I'd suggest that you only add an entry to the list if it is empty,  e.g.

 

public List<accountwrapper> getAccounts()
{
   if (accountList.isEmpty())
   {
      for(Attendance__c a : [SELECT Name, date__c, Excused_Absence__c,   
              Excuse_Details__c,
              Present__c, Rubric__c,Session__c, Youth__r.LastName FROM 
              Attendance__c
           ORDER BY Youth__r.LastName asc ])
   {
      accountList.add(new accountwrapper(a));
   }

   return accountList;
}

 and in your save, move the clear(), call to after you have carried out the update.

VinuVinu

Hi Bob,

 

I've searched the forums for info and have tried cobbling together a solution for my needs.

But your suggestion only drives me to acheive what i expect..

 

Thank you and i expect the same from you always..