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
Jeremy DeseezJeremy Deseez 

Save method for a PageBlockTable

Hi SF Dev,

I have a PageBlockTable that return the users with their related quotas in a <apex:inputField.
All I want is to save it when I change the amount of the quota, but when I click on save, nothing happen, the page is refreshing with the old data on the table.

Here is my apex code :
public with sharing class AssignQuotaTest {

public List<ForecastingQuota> resultsquotas{get;set;}
public List<ForecastingQuota> resultsquotas2{get;set;}
public List<ForecastingQuota> allthequotas{get;set;}
public List<USer> resultsusers{get;set;}

public Map<Id, ForecastingQuota> allUserFqsByOwnerId {get;set;}
public Map<Id, ForecastingQuota> fqsByOwnerId {get;set;}

public String QuotaAmount{get;set;}
public Id QuotaOwnerId{get;set;}
public String QuotaQuantity{get;set;}
public Id PeriodId{get;set;}
public String ProductFamily{get;set;}
public Boolean IsQuantity{get;set;}
public String CurrencyIsoCode{get;set;}
public Id ForecastingTypeId{get;set;}
public Boolean IsAmount{get;set;}


public Date dateperiod{get;set;}
public Date dateperiodlist {get;set;}
public Date dateperiodlist1 {get;set;}
public Date datetest {get;set;}

public String testquarter {get;set;}
public Date countryOptions {get;set;}
public Date theperiodlist {get;set;}
public Date theperiodlist2 {get;set;}

public String next{get;set;}
public String previous{get;set;}
public Date namequarter{get;set;}

public Date namequarter2{get;set;}

public String assignquotaamount {get;set;}

public Id idofuserrole{get;set;}
public Id idofuserrole1{get;set;}
public Id idofuserrole2{get;set;}

public Boolean usertype {get;set;}
public Boolean usertype2 {get;set;}
public Boolean usertypetest {get;set;}

public string testtest2{get;set;}
public string testtest{get;set;}

public Boolean changeuser{get;set;}




public AssignQuotaTest(ApexPages.StandardController controller) {

	    searchQuotas();
	}
	
	public void searchQuotas(){
		System.debug(changeuser);
		if(namequarter == null) {
			namequarter = Date.today();
		}

		if(next == 'next'){
			namequarter = namequarter.addDays(90);
		}  


		if(previous == 'previous'){
			namequarter = namequarter.addDays(-90);
		} 

		namequarter2 = namequarter.addDays(90);
		
		if(theperiodlist == null){
			theperiodlist = Date.today();
		}

		theperiodlist2 = theperiodlist.addDays(90);
		


		List<ForecastingQuota> resultsquotas = new List<ForecastingQuota>();
	    resultsquotas = [SELECT Id,QuotaAmount,QuotaOwnerId, StartDate FROM ForecastingQuota WHERE StartDate >=:namequarter AND StartDate <=:namequarter2 ORDER BY QuotaOwnerId ASC];
	    List<User> resultsusers = new List<User>();
	    if(idofuserrole != null){
	    		resultsusers = [SELECT Id FROM User WHERE IsActive =:changeuser AND ForecastEnabled = TRUE AND UserRoleId =:idofuserrole];
	    	} else {
				resultsusers = [SELECT Id FROM User WHERE IsActive =:changeuser AND ForecastEnabled = TRUE];
			}
			
		Map<Id, ForecastingQuota> fqsByOwnerId = new Map<Id, ForecastingQuota>();
			for (ForecastingQuota fq : resultsquotas)
			{
			    fqsByOwnerId.put(fq.QuotaOwnerId, fq);
			}
		
			// new map of quotas keyed by all user ids
		Map<Id, ForecastingQuota> allUserFqsByOwnerId = new Map<Id, ForecastingQuota>();
			for (User u : resultsusers)
			{
			     allUserFqsByOwnerId.put(u.id, fqsByOwnerId.containsKey(u.id) ? fqsByOwnerId.get(u.id) : new ForecastingQuota(QuotaOwnerId = u.id, QuotaAmount = null));
			    	
			}
			allthequotas = new List<ForecastingQuota>();
			allthequotas = allUserFqsByOwnerId.values();
			allthequotas.sort();
	}



	public List<SelectOption> getQuarterDate() {
	        List<SelectOption> countryOptions = new List<SelectOption>();
	        countryOptions.add(new SelectOption('','-None-'));
	        countryOptions.add(new SelectOption('12/01/2016','Q4 2016'));
	        countryOptions.add(new SelectOption('01/01/2017','Q1 2017'));
	        countryOptions.add(new SelectOption('04/01/2017','Q2 2017'));
	        countryOptions.add(new SelectOption('07/01/2017','Q3 2017'));

	 
	        return countryOptions;
	}

 	public List<SelectOption> getPeriodList(){
 		List<SelectOption> optionspl = new List<SelectOption>();
 		List<SObject> resultspl = [SELECT FullyQualifiedLabel,EndDate,IsForecastPeriod,StartDate
									FROM Period
									WHERE EndDate > TODAY
									AND Type != 'Year'
									AND IsForecastPeriod = true
									ORDER BY EndDate ASC];
 		for(SObject pl : resultspl){
 			optionspl.add(new SelectOption(String.valueOf(pl.get('StartDate')),String.valueOf(pl.get('FullyQualifiedLabel'))));
 		}
 		return optionspl;
 	}


  	public List<SelectOption> getQuarterList(){
  		List<SelectOption> optionsquarter = new List<SelectOption>();
  		List<SObject> resultsquarter = [SELECT FullyQualifiedLabel,EndDate,StartDate,Type
										FROM Period
										WHERE Type = 'Quarter'
										AND StartDate >= TODAY];
		for(SObject rq : resultsquarter){
			optionsquarter.add(new SelectOption(String.valueOf(rq.get('StartDate')),String.valueOf(rq.get('FullyQualifiedLabel'))));
		}
		return optionsquarter;
  	}


 	public List<SelectOption> getUserActive(){
 		List<SelectOption> optionsua = new List<SelectOption>();
 		List<SObject> resultsua = [SELECT IsActive FROM User GROUP BY IsActive ORDER BY IsActive DESC];
 		for(SObject ua : resultsua){
 			optionsua.add(new SelectOption(String.valueOf(ua.get('IsActive')),String.valueOf(ua.get('IsActive'))));
 		}
 		return optionsua;
 	}


 	public List<SelectOption> getUserRole(){

 		List<SelectOption> optionsur = new List<SelectOption>();
 		List<SObject> resultsur = [SELECT Id, Name FROM UserRole WHERE Name LIKE '%Sales/%'];
 		for(SObject ur : resultsur){
 			
 			optionsur.add(new SelectOption(String.valueOf(ur.get('Id')),String.valueOf(ur.get('Name'))));
 		}optionsur.add(new SelectOption('','ALL'));
 		return optionsur;

 	}
	public List<User> getUsers(){
		List<User> resultsusers = new List<User>();
		resultsusers = [SELECT Id, Name FROM User WHERE IsActive = TRUE AND ForecastEnabled = TRUE LIMIT 999];
		return resultsusers; 
	}

	public void save(){
		
		upsert resultsquotas;
	}

}

And here is my visualforce :
 
<apex:page standardController="ForecastingQuota" extensions="AssignQuotaTest" sidebar="false" docType="html-5.0">


<apex:form id="formid">

<apex:pageBlock title="Kyriba Quotas" id="allthequotas">
<apex:pageBlockSection columns="2">

	<apex:outputlabel value="Date"/>
	<apex:input type="date" required="false" id="namequarter" value="{!namequarter}"/>

	<apex:outputlabel value="User Role"/>
	<apex:selectList value="{!idofuserrole}" size="1">
    	<apex:selectOptions value="{!userrole}" />
	</apex:selectList>

	<!-- <apex:outputlabel value="Active Users"/>
	<apex:selectList value="{!usertype}" size="1">
		<apex:selectOptions value="{!useractive}" />
	</apex:selectList><br/><br/> -->

<apex:commandButton value="Inactive Users" reRender="allquotas" action="{!searchQuotas}">
	<apex:param id="inactive" name="inactive" value="false" assignTo="{!changeuser}" /> 
</apex:commandButton>
<apex:commandButton value="Active Users" reRender="allquotas" action="{!searchQuotas}">
	<apex:param id="active" name="active" value="true" assignTo="{!changeuser}" /> 
</apex:commandButton>



<ul></ul>

<input type="checkbox" id="select_all"/>First Column<br/>
<input type="checkbox" id="select_all2"/>Second Column<br/>
<input type="checkbox" id="select_all3"/>Third Column<br/>
<input type="checkbox" id="select_all4"/>Fourth Column<br/>

<apex:commandButton id="saveBtn" value="Save" action="{!save}" />
<apex:commandButton value="Filter" reRender="allquotas" action="{!searchQuotas}">
</apex:commandButton>
 
</apex:pageBlockSection>

<br/><br/>
	<apex:pageBlockSection columns="4" id="allquotas">
			<apex:pageBlockTable value="{!allthequotas}" id="table" var="key">
			<apex:column>
			    <apex:inputCheckbox id="checkbox" styleClass="checkbox1"/>
			</apex:column>
			<apex:column headerValue="Name">
			    <apex:outputField  value="{!key.QuotaOwnerId}"/>
			</apex:column>
			<apex:column headerValue="Quota">
		    	<apex:inputField value="{!key.QuotaAmount}" required="false"/>
		    </apex:column>
			</apex:pageBlockTable>
			 <apex:pageBlockTable value="{!allthequotas}" var="key2">
			 <apex:facet name="header">
            	<input type="checkbox"  id="checkbox" styleClass="checkbox1"/>
            </apex:facet>
			<apex:column>
			    <apex:inputCheckbox id="checkbox" styleClass="checkbox2"/>
			</apex:column>
				<apex:column headerValue="Name">
				    <apex:outputField value="{!key2.QuotaOwnerId}"/>
				</apex:column>
    			  <apex:column headerValue="Quota">
			    	<apex:inputField value="{!key2.QuotaAmount}" id="firstquota2" required="false"/>
			    </apex:column> 
			    <apex:column headerValue="Quota">
			    	<apex:outputField value="{!key.StartDate}"/>
			    </apex:column>
			</apex:pageBlockTable> 
			 <apex:pageBlockTable value="{!allthequotas}" var="key3">
			 <apex:facet name="header">
            	<input type="checkbox"  id="checkbox" styleClass="checkbox1"/>
            </apex:facet>
			<apex:column>
			    <apex:inputCheckbox id="checkbox" styleClass="checkbox3"/>
			</apex:column>
				<apex:column headerValue="Name">
				    <apex:outputField value="{!key3.QuotaOwnerId}"/>
				</apex:column>
    			  <apex:column headerValue="Quota">
			    	<apex:inputField value="{!key3.QuotaAmount}" required="false"/>
			    </apex:column> 
			</apex:pageBlockTable> 
			 <apex:pageBlockTable value="{!allthequotas}" var="key4">
			 <apex:facet name="header">
            	<input type="checkbox"  id="checkbox" styleClass="checkbox1"/>
            </apex:facet>
			<apex:column>
			    <apex:inputCheckbox id="checkbox" styleClass="checkbox4"/>
			</apex:column>
				<apex:column headerValue="Name">
				    <apex:outputField value="{!key4.QuotaOwnerId}"/>
				</apex:column>
    			  <apex:column headerValue="Quota">
			    	<apex:inputField value="{!key4.QuotaAmount}" required="false"/>
			    </apex:column>
			</apex:pageBlockTable>
	</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

<script type="text/javascript">
$(document).ready(function(){



var select_all = document.getElementById("select_all"); //select all checkbox
console.log(select_all);
var select_all2 = document.getElementById("select_all2");
var select_all3 = document.getElementById("select_all3");
var select_all4 = document.getElementById("select_all4");
var checkboxes = document.getElementsByClassName("checkbox1"); //checkbox items
console.log(checkboxes);
var checkboxes2 = document.getElementsByClassName("checkbox2");
var checkboxes3 = document.getElementsByClassName("checkbox3");
var checkboxes4 = document.getElementsByClassName("checkbox4");

//select all checkboxes
select_all.addEventListener("change", function(e){
    for (i = 0; i < checkboxes.length; i++) { 
        checkboxes[i].checked = select_all.checked;
    }
});
select_all2.addEventListener("change", function(e){
    for (i = 0; i < checkboxes2.length; i++) { 
        checkboxes2[i].checked = select_all2.checked;
    }
});
select_all3.addEventListener("change", function(e){
    for (i = 0; i < checkboxes3.length; i++) { 
        checkboxes3[i].checked = select_all3.checked;
    }
});
select_all4.addEventListener("change", function(e){
    for (i = 0; i < checkboxes4.length; i++) { 
        checkboxes4[i].checked = select_all4.checked;
    }
});


for (var i = 0; i < checkboxes.length; i++) {
    checkboxes[i].addEventListener('change', function(e){ //".checkbox" change 
        //uncheck "select all", if one of the listed checkbox item is unchecked
        if(this.checked == false){
            select_all.checked = false;
        }
        //check "select all" if all checkbox items are checked
        if(document.querySelectorAll('.checkbox:checked').length == checkboxes.length){
            select_all.checked = true;
        }
    });
}
for (var i = 0; i < checkboxes2.length; i++) {
    checkboxes2[i].addEventListener('change', function(e){ //".checkbox" change 
        //uncheck "select all", if one of the listed checkbox item is unchecked
        if(this.checked == false){
            select_all2.checked = false;
        }
        //check "select all" if all checkbox items are checked
        if(document.querySelectorAll('.checkbox2:checked').length == checkboxes2.length){
            select_all2.checked = true;
        }
    });
}
for (var i = 0; i < checkboxes3.length; i++) {
    checkboxes3[i].addEventListener('change', function(e){ //".checkbox" change 
        //uncheck "select all", if one of the listed checkbox item is unchecked
        if(this.checked == false){
            select_all3.checked = false;
        }
        //check "select all" if all checkbox items are checked
        if(document.querySelectorAll('.checkbox3:checked').length == checkboxes3.length){
            select_all3.checked = true;
        }
    });
}
for (var i = 0; i < checkboxes4.length; i++) {
    checkboxes4[i].addEventListener('change', function(e){ //".checkbox" change 
        //uncheck "select all", if one of the listed checkbox item is unchecked
        if(this.checked == false){
            select_all4.checked = false;
        }
        //check "select all" if all checkbox items are checked
        if(document.querySelectorAll('.checkbox4:checked').length == checkboxes4.length){
            select_all4.checked = true;
        }
    });
}
});


</script>
</apex:page>
Can you guide me for the custom save method ?

Many thanks.

 
Pankaj MehraPankaj Mehra
Hi Jeremy,

I can see that you are upserting "resultsquotas"  line 184 AssignQuotaTest which is never referenced on the VF page , on VF page you are updating values of allthequotas, try upserting List "allthequotas" in save method.

Thanks
Jeremy DeseezJeremy Deseez
Hi Pankaj, I have try your answer, and for the users who have already a QuotaAmount, nothing happend and for the users who haven't yet a QuotaAmount, there are fields missing. How can I set the missing fields from my visualforce page to save method ?
 
Jeremy DeseezJeremy Deseez
That's work, it was not working because I show the same 4 tables and I was just modify the first table, but with just one table on visualforce page it's work.
I have an other question, on the next 3 tables I want to show the next 3 quarter, how can i do this? I have to do a loop in SearchQuotas() to make the others 3 tables ? Wich increment the date to 90 days ?
Pankaj MehraPankaj Mehra
Hi Jeremy, 

Create  4 List, one for each quater
public List<ForecastingQuota> quater1{get;set;}
public List<ForecastingQuota> quater2{get;set;}
public List<ForecastingQuota> quater3{get;set;}
public List<ForecastingQuota> quater4{get;set;}

add data to each list according to quater in controller , update field from UI and when saving create a master list add all four list in it and update master list.