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
Jan VandeveldeJan 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:
<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:
User-added image
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?

 
Best Answer chosen by Jan Vandevelde
SonamSonam (Salesforce Developers) 
Try the below code for controller:

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

Vishnu VaishnavVishnu Vaishnav
Hi Jan,

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>
======================================================
SonamSonam (Salesforce Developers) 
Try the below code for controller:

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;}
    
    
}
 
This was selected as the best answer
Jan VandeveldeJan Vandevelde
Hi Vishnu,

I created a temporary VF page and Controller with the contents of your code. When saving the controller I get the following error:
User-added image
Jan VandeveldeJan Vandevelde
Hi Sonam,

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 ;-)
SonamSonam (Salesforce Developers) 
Yes Jan, you had it 99.9% right :) 

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;}
}
 

 
Jan VandeveldeJan Vandevelde
Thanks for the explanation, It sounds logic, guess I had a brainfreeze ;-) I looked over and over the code and just couldn't find what I was doing wrong ;-)