I would like to know that is there any way my trigger will be fired when any record is added to a related list .
Right now , The trigger iterates through the related list records and updates a field in Account .
So suppose a new record is added to the related list , the field will not be updated unless the account is edited . Is there any way it can be updated ?
Edit :Here Account and Client are in Lookup relationship.
trigger ABCD on Account (before insert, after update) {
if(RecursiveClass.updateOnce == false){
RecursiveClass.updateOnce = true;
List<Account> AccList = [Select id, Field1__c, (Select Id, Status__c from Clients__r) from Account WHERE Id IN: trigger.new];
List<Account> updateList = new List<Account>();
Integer demoField = 0;
for (Account acc: AccList) {
for (Client pc: acc.Clients__r) {
If(pc.Status__c != 'Lapsed') {
demoField++;
}
}
acc.Field1__c = demoField;
updateList.add(acc);
}
if(updateList.size()>0){
update updateList;
}
}
}