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
Yogesh Kumar 35Yogesh Kumar 35 

Account to case update : Trigger

Hi All,

I am new to Salesforce and trying to write a trigger and test class so that I can create a trigger that fires when the Active__c field on the Account object is updated to ‘No’. This trigger should find all related Case records and update their Status fields to ‘Closed’. 

This trigger should create at least 150 Accounts and at least 10 Cases for each Account. ​I need  to use assert statements to validate that my trigger is working. 

I have written the below code and getting 3 errors:

Trigger:

trigger  AccountTrigger  on  Account   ( after update )   { 
Account.CaseUpdate(Trigger.new);
}


or 


trigger  AccountTrigger  on  Account   ( after update )   { 
CaseUpdate update = new CaseUpdate();
   if (Trigger.isAfter && Trigger.isUpdate){
        update.CaseUpdate(Trigger.new);
   }   
}

=========================================================================
Class:

public class CaseUpdate {
       set <Account> accId = new Set <Id>();
       set <Cases> caseId = new Set <Id>();

  for (Case c : Trigger.new); {
  List<Case> opencase = [SELECT Id, Status, Account Id from Case where Id = :accId.Id];
 if (a.Active__c = 'No')
 {
  c.Status = 'Closed';
update opencase;
 } 
}
   }

======================================================================

Testclass:

@isTest
public   class   AccountTriggerTest  {
public   static  testMethod  void  doTest ()  {

    Account acc = new Account(
          Name = 'Test Account',

          Active__c = 'None'
       );
    insert acc;
       Case c = new Case(
           Account = acc.Id,
           Status = 'None';
       );
    insert c;
       Account accTest = [SELECT Id, Active__c FROM Account WHERE Id = :acc.id];
       System.assertEquals(accTest.Active__c, 'Yes');
       c.Status = 'Closed';
       update c;
       accTest = [SELECT Id, Active__C FROM Account WHERE Id = :acc.id];
       System.assertEquals(accTest.Status, 'Closed');
   }
}
}

Any suggestions and guidance would really help me. Thanks in advance. 
Best Answer chosen by Yogesh Kumar 35
sfdcMonkey.comsfdcMonkey.com
hi Yogesh Kumar
try below code for update case status field when account field Active__c is true
trigger  AccountTrigger  on  Account   ( after update )   { 
   if (Trigger.isAfter && Trigger.isUpdate){
       CaseUpdate oupdate = new CaseUpdate();
        oupdate.CaseUpdatemethod(Trigger.new);
   }   
}

class
note-: make sure in your status picklist on case object, you have "Closed" value in your picklist
public class CaseUpdate {
    
 public void CaseUpdatemethod(List<account> oTriggernew){
      set <Id> accId = new Set <Id>();
        
        for(account acc : oTriggernew){  
              accId.add(acc.id);
        } 
   List<case> lstOfCase = [SELECT Id, Status, AccountId from Case where AccountId IN : accId] ;  
   List<case> caseToUpdate = new List<case>();  
       for(account acc : oTriggernew){ 
       for(case c : lstOfCase){
        if (acc.Active__c == true){
         c.Status = 'Closed';
                  }
       caseToUpdate.add(c);   
       }
          
       }
     update caseToUpdate;
  }
 }
test class (100% code cover)
@isTest 
public class TestForcTrigger {
    static testMethod void insertNewUser() {
       
       account acc =  new account();
       acc.name = 'test'; 
        insert acc;
        
       case cc = new case();
        cc.status = 'new';
        cc.AccountId = acc.Id;
        cc.Origin = 'Web';
        insert cc;
        
        acc.Active__c = true;
        update acc;
      
    
        
          }
}

thanks
let me inform if it helps you
and mark it best answer if it helps you so it make proper solution for others in future :)

 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi Yogesh Kumar
try below code for update case status field when account field Active__c is true
trigger  AccountTrigger  on  Account   ( after update )   { 
   if (Trigger.isAfter && Trigger.isUpdate){
       CaseUpdate oupdate = new CaseUpdate();
        oupdate.CaseUpdatemethod(Trigger.new);
   }   
}

class
note-: make sure in your status picklist on case object, you have "Closed" value in your picklist
public class CaseUpdate {
    
 public void CaseUpdatemethod(List<account> oTriggernew){
      set <Id> accId = new Set <Id>();
        
        for(account acc : oTriggernew){  
              accId.add(acc.id);
        } 
   List<case> lstOfCase = [SELECT Id, Status, AccountId from Case where AccountId IN : accId] ;  
   List<case> caseToUpdate = new List<case>();  
       for(account acc : oTriggernew){ 
       for(case c : lstOfCase){
        if (acc.Active__c == true){
         c.Status = 'Closed';
                  }
       caseToUpdate.add(c);   
       }
          
       }
     update caseToUpdate;
  }
 }
test class (100% code cover)
@isTest 
public class TestForcTrigger {
    static testMethod void insertNewUser() {
       
       account acc =  new account();
       acc.name = 'test'; 
        insert acc;
        
       case cc = new case();
        cc.status = 'new';
        cc.AccountId = acc.Id;
        cc.Origin = 'Web';
        insert cc;
        
        acc.Active__c = true;
        update acc;
      
    
        
          }
}

thanks
let me inform if it helps you
and mark it best answer if it helps you so it make proper solution for others in future :)

 
This was selected as the best answer
Yogesh Kumar 35Yogesh Kumar 35
thanks for your help, its working perfectly fine...
Yogesh Kumar 35Yogesh Kumar 35
I do have one more question, how can we use assert statement in test class and check that this trigger should create at least 150 Accounts and at least 10 Cases for each Account in virtual database.
Alekya K 2Alekya K 2
Hi,
I still get 3 errors while running test class saying:
1. CaseUpdate -  Comparision arguments must be compatible types, String:Boolean  (if (acc.Active__c == true))
2. AccountTrigger-  Dependent class is invalid and needs recompilation (CaseUpdate oupdate = new CaseUpdate();)
3. AccountTriggerTest - Illegal assignment from Boolean to String do not know why. ( acc.Active__c = true;)
It would be great if you could help.