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
si risi ri 

How to print error message using trigger?

Hi!
 I wrote a  before trigger when a Checkbox is CHECKED in an Account object it should print an error message on Case object when certain criteria met. But whatever the method in my class is not invoking when the trigger fires. Can anyone help?

Apex class
    public class updatemonitoringservice{
     public static Boolean caseValidation(List<Account> accountList){
        set<Id> accId = new set<Id>();
        for(Account acc : accountList){
            accId.add(acc.id); //Adding accountId's to Set.    
             }
        List<Account> accList = [select id, Name, Suspend__c,(select id,RecordTypeId, RecordType.Name from Cases) from Account where Id=:accId];
          map<Id,List<Case>> mapInst = new map<Id,List<Case>>();
        for(Account accVar : accList){
            mapInst.put(accVar.Id,accVar.Cases); 
        }
        List<Case> csList = new List<Case>();
         for(Account a : accList){
                                       
           if(a.Suspend__c) 
              {
            
                system.debug('a.Suspend__c::'+a.Suspend__c);
               csList.addAll(mapInst.get(a.Id));  
               
            }
         }
        List<Case> caseError = new List<Case>();
        Boolean hadErrors = false;
        for(Case caseVar:csList){
            if(caseVar.RecordType.Name  != 'Accounting_Request' && caseVar.RecordType.Name  == 'Service_Request')
                  
            {
                caseVar.Type = 'Removal';   
                
            } 
            else
            {
                caseVar.addError('Account is currently Suspended, please contact Collections / CEM for more information');
                 hadErrors = true;
            }
        }
       return hadErrors;
    }
   }

trigger
trigger UpdateMonitoringtrigger on Account (before insert) {
updatemonitoringservice.caseValidation(trigger.new);
   }


    
Raj VakatiRaj Vakati
The problem i am seeing here .. 

You have a trigger on the account so this trigger will fire on account and error will be displayed on account .. in case if you want to display on case create a trigger on case so that you can able to show the error message on case ..