You need to sign in to do that
Don't have an account?
nksf
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.
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.
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
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);
}
if your issue get resolved, please make it as Solved and Select Best Answer. It'll helps Other.
Thanks!
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?
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);
}
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.
Please mark as best answer so it will help to other who will serve same problem.
Thanks!