You need to sign in to do that
Don't have an account?
Aaron 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
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
All Answers
Try saving the trigger and testing it, let me know the outcome.
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.
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);
}
https://developer.salesforce.com/blogs/developer-relations/2016/07/six-tips-great-unit-tests.html?language=en