You need to sign in to do that
Don't have an account?
Jan Vandevelde
Update list of contracts for account visualforce and controller
Hi guru's,
I've got a small request. I've got a Visualforcepage called CheckContracts and a Controller CheckContractsController.
All I want to do is show all contracts for a given accountid passed in the URL to the VF controller, where there is only 1 field that can be filled in for each record by the user and then those contracts should be updated with the field clicking a button.
This is the code of the VF page:
This is the controller:
The page looks like:
When I click the button it should just update the list of contracts where Effective_enddate__c is filled. But when I put the statement update contracts; in the updateContracts method it gives an error so I commented out for now.
Can you help me with this? I'm terrible at getter and setter methods, so I don't know why it doesn't just update all contracts with the values I filled in.
Pretty please?
I've got a small request. I've got a Visualforcepage called CheckContracts and a Controller CheckContractsController.
All I want to do is show all contracts for a given accountid passed in the URL to the VF controller, where there is only 1 field that can be filled in for each record by the user and then those contracts should be updated with the field clicking a button.
This is the code of the VF page:
<apex:page standardController="Account" extensions="CheckContractsController" showHeader="false" sidebar="false" > <apex:form > <apex:pageBlock title="Contracts List"> <br></br><br></br> <h3>Gelieve de nodige contracten na te kijken en eventueel af te sluiten door een Effectieve Einddatum in te voeren!</h3> <br></br><br></br> <apex:pageBlockButtons location="bottom"> <apex:commandButton action="{!updateContracts}" value="Update Contracts"/> </apex:pageBlockButtons> <apex:pageBlockTable value="{!Contracts}" var="c"> <apex:column headerValue="ContractName"> <apex:outputLink value="{!URLFOR('/')}{!c.Id}" target="_blank">{!c.ContractNumber}</apex:outputLink> </apex:column> <apex:column value="{!c.Name}" /> <apex:column value="{!c.StartDate}" /> <apex:column value="{!c.Contract_Enddate__c}" /> <apex:column headerValue="Effective Enddate" > <apex:inputField value="{!c.Effective_Enddate__c}" /> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>
This is the controller:
public with sharing class CheckContractsController { String acctId = ApexPages.currentPage().getParameters().get('acctId'); public PageReference updateContracts(){ //update contracts; return null; } public List<Contract> getContracts(){ return [Select Id, Name, ContractNumber, StartDate, Contract_enddate__c, Effective_enddate__c from Contract where AccountId= :acctId]; } public CheckContractsController(ApexPages.StandardController controller) { theAccount = [Select Id, Name, Accountnumber__c from Account where Id= :acctId LIMIT 1]; } public Account theAccount{get; set;} }
The page looks like:
When I click the button it should just update the list of contracts where Effective_enddate__c is filled. But when I put the statement update contracts; in the updateContracts method it gives an error so I commented out for now.
Can you help me with this? I'm terrible at getter and setter methods, so I don't know why it doesn't just update all contracts with the values I filled in.
Pretty please?
public with sharing class CheckContractsController {
String acctId = ApexPages.currentPage().getParameters().get('acctId');
List<Contract> contracts = new List<Contract>();
public PageReference updateContracts(){
update contracts;
return null;
}
public List<Contract> getContracts(){
contracts = [Select Id, Name, ContractNumber, StartDate, Contract_enddate__c, Effective_enddate__c from Contract where AccountId= :acctId];
return contracts;
}
public CheckContractsController(ApexPages.StandardController controller) {
theAccount = [Select Id, Name, Accountnumber__c from Account where Id= :acctId LIMIT 1];
}
public Account theAccount{get; set;}
}
All Answers
Here is modified code , u can try once :
Class
===============================================
public with sharing class CheckContractsController {
String acctId ;
public CheckContractsController(ApexPages.StandardController controller) {
acctId = controller.getId();
}
public PageReference updateContracts(){
update MyContracts;
return null;
}
public List<Contract> getMyContracts(){
return [Select Id, Name, ContractNumber, StartDate, Contract_enddate__c, Effective_enddate__c from Contract where AccountId= :acctId];
}
}
====================================================
Page
======================================================
<apex:page standardController="Account" extensions="CheckContractsController" showHeader="false" sidebar="false" >
<apex:form >
<apex:pageBlock title="Contracts List">
<br></br><br></br>
<h3>Gelieve de nodige contracten na te kijken en eventueel af te sluiten door een Effectieve Einddatum in te voeren!</h3>
<br></br><br></br>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!updateContracts}" value="Update Contracts"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!MyContracts}" var="c">
<apex:column headerValue="ContractName">
<apex:outputLink value="{!URLFOR('/')}{!c.Id}" target="_blank">{!c.ContractNumber}</apex:outputLink>
</apex:column>
<apex:column value="{!c.Name}" />
<apex:column value="{!c.StartDate}" />
<apex:column value="{!c.Contract_Enddate__c}" />
<apex:column headerValue="Effective Enddate" >
<apex:inputField value="{!c.Effective_Enddate__c}" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
======================================================
public with sharing class CheckContractsController {
String acctId = ApexPages.currentPage().getParameters().get('acctId');
List<Contract> contracts = new List<Contract>();
public PageReference updateContracts(){
update contracts;
return null;
}
public List<Contract> getContracts(){
contracts = [Select Id, Name, ContractNumber, StartDate, Contract_enddate__c, Effective_enddate__c from Contract where AccountId= :acctId];
return contracts;
}
public CheckContractsController(ApexPages.StandardController controller) {
theAccount = [Select Id, Name, Accountnumber__c from Account where Id= :acctId LIMIT 1];
}
public Account theAccount{get; set;}
}
I created a temporary VF page and Controller with the contents of your code. When saving the controller I get the following error:
That just did the trick. If I go through your code I think the only thing I missed was declaring the
List<Contract> contracts = new List<Contract>(); and setting the soql query to variable contracts in the getter and then returning contracts.
I was not too far of then. Could you briefly explain why I needed to do it that way? I would appreciate that a lot ;-)
We are using contracts property to store the the contracts which we are showing on the VF that need to be updated but we had not initialised it - that's why it was giving an error: " unknown property contracts " when you uncommented that update line.
Once we initialise it, its done, I just added two lines highlighted below:
public with sharing class CheckContractsController {
String acctId = ApexPages.currentPage().getParameters().get('acctId');
List<Contract> contracts = new List<Contract>();
public PageReference updateContracts(){
update contracts; //this was NOT defined before so compiler didn't know what this list was and where to get it from
return null;
}
public List<Contract> getContracts(){
contracts = [Select Id, Name, ContractNumber, StartDate, Contract_enddate__c, Effective_enddate__c from Contract where AccountId= :acctId];
return contracts;
}
public CheckContractsController(ApexPages.StandardController controller) {
theAccount = [Select Id, Name, Accountnumber__c from Account where Id= :acctId LIMIT 1];
}
public Account theAccount{get; set;}
}