+ Start a Discussion
nksfnksf 

Update Field on Account based on Active Contract

Hi Guys,
Can you please help me out with a Trigger. I have a checkbox field on Account Page as Active_Contract__c and I need to update this field to True if this Account has any Active Contract. On Contract I have the same checkbox field as Active_Contract__c which tells us if contract is Active or not. So whenever we generate a Contract and set it as Active Contract then on its related Account Page Active Contract field should get updated to True.
If all the Contracts related to Account turns Inactive then Active Contract Field on Account Field should be updated to False. 

Also I need a script which I can run to update all the Accounts which have any active contracts.  

Thanks in advance. 
Best Answer chosen by nksf
Shrikant BagalShrikant Bagal
try following


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

}
 

All Answers

Shrikant BagalShrikant Bagal
Please try
 
trigger OnAccountCreated on Contract(after insert, after Update) {
	Set setAccountId = new Set();    
	List lstToUpdate = new List();
	for(Contract rec : Trigger.New)
	{
		setAccountId.add(rec.Account);
	}
	List lstAccount = [SELECT Id,(SELECT Id FROM Contracts WHERE Active_Contract__c = true) 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);
}

 
nksfnksf
Thanks for quick response Shrikant! Let me try this. How do I update the existing Accounts?
Shrikant BagalShrikant Bagal
it will also update the existing account :)
nksfnksf
Thanks Shrikant!I am getting this error Error: Compile Error: expecting a semi-colon, found 'setAccountId' at line 2 column 8
Shrikant BagalShrikant Bagal
*trigger OnAccountCreated on Contract(after insert, after Update) { Set setAccountId = new Set(); List lstToUpdate = new List(); for(Contract rec : Trigger.New) { setAccountId.add(rec.AccountId); } List lstAccount = [SELECT Id,(SELECT Id FROM Contracts WHERE Active_Contract__c = true) 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);}*
nksfnksf
Hi Again,I tried this code but still getting errors. I have made couple of changes and highlighted them please let me know if these changes are correct? Also after making these changes I am still getting this error. Compile Error: Incompatible element type Account for collection of Contract at line 10 column 1 trigger OnAccountCreated on Contract(after insert, after Update) { Set setAccountId = new Set(); List lstToUpdate = new List(); for(Contract rec : Trigger.New) { setAccountId.add(rec.AccountId); } List lstAccount = [SELECT Id,(SELECT Id FROM Contracts WHERE Active_Contract__c = true) 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);}
nksfnksf
I am not able to see the code in your last comment
Shrikant BagalShrikant Bagal
I don't know what happens with developer forum - Use following screenshot for code: http://postimg.org/image/y8p35hzat/
nksfnksf
I am getting this error message this time. Error: Compile Error: AND operator can only be applied to Boolean expressions at line 15 column 43Line 15 is: else if(acc.Contracts.size() = 0 && acc.Active_Contract__c)
Shrikant BagalShrikant Bagal
else if(acc.Contracts.size() = 0 && acc.Active_Contract__c) change it to else if(acc.Contracts.size() == 0 && acc.Active_Contract__c)
Shrikant BagalShrikant Bagal
Please try

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

}
Shrikant BagalShrikant Bagal
Hello nksf,

if your issue get resolved, please make it as Solved and Select Best Answer. It'll helps Other.

Thanks!
nksfnksf
Hi Shrikant,
I am not getting any error now. But when I try to edit Contract it gives me error message and I am not able to edit Contracts. 
Also I need to update this field on All existing Accounts. How can I do it?
nksfnksf
Thats the error message I am getting when I try to edit any Contract. Error:Apex trigger OnAccountCreated caused an unexpected exception, contact your administrator: OnAccountCreated: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Active_Contract__c: Trigger.OnAccountCreated: line 21, column 1
Shrikant BagalShrikant Bagal
try following


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

}
 
This was selected as the best answer
nksfnksf
Looks good now. Thanks
Just a quick question do I have to run this script to update all the existing Account or this Trigger should also work to update all the existing Accounts? I believe Trigger will only work when I update any record. 
Shrikant BagalShrikant Bagal
Yes It will update existing account also.

Please mark as best answer so it will help to other who will serve same problem.

Thanks!