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
Aaron Persich 8Aaron Persich 8 

Update Account Status Field based on Contract Active Status

Hello,
 
I am very new to triggers and classes and have recently been assigned a task to populate an Account Status field on the account based on the related contract status.  I have researched this and came across another post with the same question so I have tried to mirror this. 
 
I have created a checkbox labeled Active_Contract__c on both the Account and Contract objects.  I would like the trigger to set the Active_Contract__c to true on the account if there are any related contracts where the Active_Contract__c is also set to true.  If all contracts related to the Account turns to inactive (false) then I would like to set the Active_Contract__c to false on the account.
 
I found the below trigger but I think I will need to create a Class as well.  Again, I am very new to this and any help would be much appreciated.
 
 
trigger OnAccountCreated on Contract(after insert, after Update) {

    Set<Id> setAccountId = new Set<Id>();   

    List<Account> lstToUpdate = new List<Account>();

    for(Contract rec : Trigger.New)

    {

        setAccountId.add(rec.AccountId);

    }

    List<Account> lstAccount = [SELECT Id,(SELECT Id FROM Contracts WHERE Active_Contract__c = true),Active_Contract__c FROM Account Where Id IN:setAccountId];

    for(Account acc : lstAccount)

    {

        if(acc.Contracts.size() > 0 && !acc.Active_Contract__c){

            acc.Active_Contract__c = true;

            lstToUpdate.add(acc);

        }

        else if(acc.Contracts.size() == 0 && acc.Active_Contract__c)

        {

            acc.Active_Contract__c = false;

            lstToUpdate.add(acc);

        }

    }

         Database.update(lstToUpdate);
}
 
Thanks,

Aaron
Best Answer chosen by Aaron Persich 8
VineetKumarVineetKumar
Can you try by changing the name of the trigger from OnAccountCreated  to something else?

All Answers

VineetKumarVineetKumar
I suppose the above code will execute without any error, no need for a class here.
Try saving the trigger and testing it, let me know the outcome.
Aaron Persich 8Aaron Persich 8
Hi Vineet,

Thanks for the relpy.  Every time I try to save the trigger I get this error.

Error: Compile Error: Trigger name exists on different SObject type: Account at line 1 column 1

I am not sure how to fix this.  Any advice is much appreciated. 
VineetKumarVineetKumar
Can you try by changing the name of the trigger from OnAccountCreated  to something else?
This was selected as the best answer
Aaron Persich 8Aaron Persich 8
Hi Vineet,

The good news is I got the trigger to work in our sandbox.  However, the bad news is I do not have a test class and cannot push it into production without it.  Can you please help me to develop a test class?

trigger AaronPTest on Contract (after insert, after Update) {

    Set<Id> setAccountId = new Set<Id>();   

    List<Account> lstToUpdate = new List<Account>();

    for(Contract rec : Trigger.New)

    {
        setAccountId.add(rec.AccountId);
    }
    List<Account> lstAccount = [SELECT Id,(SELECT Id FROM Contracts WHERE Contract_Status__c = true),Contract_Status__c FROM Account Where Id IN:setAccountId];

    for(Account acc : lstAccount)

    {

        if(acc.Contracts.size() > 0 && !acc.Contract_Status__c){

            acc.Contract_Status__c = true;

            lstToUpdate.add(acc);

        }

        else if(acc.Contracts.size() == 0 && acc.Contract_Status__c)

        {

            acc.Contract_Status__c = false;

            lstToUpdate.add(acc);

        }

    }

         Database.update(lstToUpdate);
}
VineetKumarVineetKumar