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

Contract = active then get the info in the account

Hi all,


I would like to populate an account custom field with "active contract” if at least one of the contracts related to this account is still active


I guess I can use a formula to look at the deadline of the contract and say "still active" or "ended" 


But then I should look at all the account contracts and if one of the contracts is "still active", have the account custom field populated with "active contract"


Any ideas?

Thank you!!!:)



Sounds like you would need to use a trigger for this. 


Create a trigger on the contract object, and then right the code to check for your criteria.

trigger ActiveContract on Contract (after insert, after update) {

List<Contract> contr = [Select Id,Name,The_Contract_is__c from Contract where The_Contract_is__c == 'Still On Date'];

 I think here I have called the Contracts to see those contracts that are still active......


The_Contract_is__c is a formula field that looks at the end date and works fine.


What would be the next step? and what if there is more than 1 active? 


I do not mind to get the total of active contracts in the account......


How do i relate these Contracts with the Accounts now?




Well, it sounds like your checkbox on the account is if "at least one contract is still active."


Triggers are kind of confusing as there you can't assume that only one record is being processed. I'm not going to write all the code for you, but here are the steps you would need to take:


1. Loop through the Contracts that are causing the trigger to run. (

2. Put all of their account id's in a list. 

3. Query all Contracts where who's AccountId's are in the list from step 2, and that are still active. And put them in a map of AccountId to Contract


That might look like this:


Map<Id,Contract> accIdToContractMap = new Map<Id,Contract>();
for (Contract c:[select Id, Whatever from Contract where AccountId in :accountIdList]) {


4. Query all the appropriate Accounts.

5. Do a loop through the accounts:


for (Account a:accountList) {
    Integer activeCount = 0;

    List<Contract> currentContracts = accountIdToContractMap.get(a.Id);

    for (Contract c:currentContracts) {
        if (contract active) {

    a.ActiveContract__c = activeCount > 0;


 5. Update the accounts.



trigger ActivateContract on Contract (after insert, after update, before insert) {

    Set<String> AccountIDs = new Set<String>(); 
    List<Account> updAccounts = new List<Account>();
    for(Contracts c :{
    Account[] Accounts = new List<Account>();
    Accounts = [SELECT Id, (SELECT Id, Account Name FROM Contracts WHERE The_Contract_is__c = 'Still On Date') FROM Account WHERE Id IN :AccountIDs];

 I got here at the moment but I  am getting an error with the SOQL:


Error: Compile Error: only aggregate expressions use field aliasing at line 14 column 16


am I on the right way? :)) 




Change "Set<String>" to "List<Id>"


Also, put a comma between "Account" and "Name" in your query.