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
Prashant Ranjan 12Prashant Ranjan 12 

triggers on Account

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
Prashant Ranjan 12Prashant Ranjan 12
// Trigger 
trigger demo on Account (after update) {
    if(trigger.isAfter && trigger.isUpdate ){
        system.debug('hello buddy');
        demohandler.validationdemo(trigger.old);
    }
}

handler class
public class demohandler {
    public static void validationdemo(List<Account> acc){
        Set<Id> idSet = new Set<Id>();
        for(Account acc1: acc){
            idSet.add(acc1.id);
        }
        List<Account> accList = [Select id, Area__c,(select id ,Title__c from Designations__r) from Account where Id IN: idSet];
        for(Account act:Designation__c.){
            for(Designation__c deg:accList.act){  //Account has designation with Title CEO (field name= Title__c)
                if(deg.Title__c == 'CEO' && act.Area__c == ''){
                          // Account field Area cannot be blank
                        act.addError('Area should not be blank');
                    }
                
            }
        }
    }
}

 but i am getting an error. does any one provide me a right solutons
Maharajan CMaharajan C
Hi Prashant,

Please try the below updated code:

Trigger:  (Use before update)
 
trigger demo on Account (before update) {
    if(trigger.isBefore && trigger.isUpdate ){
        system.debug('hello buddy');
        demohandler.validationdemo(trigger.new);
    }
}

Apex Class:
 
public class demohandler {
    public static void validationdemo(List<Account> acc){
        Set<Id> idSet = new Set<Id>();
        for(Account acc1: acc){
            idSet.add(acc1.id);
        }
        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');
			}
        }
    }
}

Thanks,
Maharajan.C
Prashant Ranjan 12Prashant Ranjan 12
hello maharajan
why you had written this act.Designations__r.size()>0
can you explain
 
Maharajan CMaharajan C
To check is there any Designations__r with CEO title was found or not under the particular account...Because we have already filtered the Designations via below subquery. So need of addition if condition.

[Select id, Area__c,(select id ,Title__c from Designations__r where Title__c='CEO') from Account where Id IN: idSet];

The above code is working or not?

Thanks,
Maharajan.C
Prashant Ranjan 12Prashant Ranjan 12
okay got it .But query is not working code is fine
Prashant Ranjan 12Prashant Ranjan 12
And how can i check my trigger is working fine or not?