we have 2 objects Account and Dsignation
parent object:Account
Child object: Designation
Relationship :Lookup(Field name "Account__C" on Designation object)
If an Account has Designation with Title CEO(Field name="Title__c") then the Account field "Area__c" cannot be blank
//this is my trigger code
trigger demo on Account (after update) {
if(trigger.isAfter && trigger.isUpdate ){
system.debug('hello buddy');
demohandler.validationdemo(trigger.old);
}
}
this is my handler class
public class demohandler {
public static void validationdemo(List<Account> acc){
try{
Set<Id> idSet = new Set<Id>();
for(Account acc1: acc){
idSet.add(acc1.id);
system.debug('@@@@');
}
List<Account> accList = [Select id, Area__c,(select id ,Title__c from Designations__r where Title__c='CEO') from Account where Id IN: idSet];
for(Account act : accList){
if(act.Designations__r.size()>0 && (act.Area__c == '' || act.Area__c == null)){
act.addError('Area should not be blank');
system.debug('$$$$');
}
}
}catch (Exception e){
system.debug('An exception occurred: ' + e.getStackTraceString());
}
}
}
I have to write a trigger on another object also to fulfil my requirement but I am unable to write it.