This trigger validates that no more than 3 contacts are added to an account. It fires on before insert, before update, and after undelete events. The trigger gets all account IDs from the new/updated contacts, queries for accounts with more than 3 contacts, and adds an error to any contacts on accounts that exceed the limit.
This trigger validates that no more than 3 contacts are added to an account. It fires on before insert, before update, and after undelete events. The trigger gets all account IDs from the new/updated contacts, queries for accounts with more than 3 contacts, and adds an error to any contacts on accounts that exceed the limit.
This trigger validates that no more than 3 contacts are added to an account. It fires on before insert, before update, and after undelete events. The trigger gets all account IDs from the new/updated contacts, queries for accounts with more than 3 contacts, and adds an error to any contacts on accounts that exceed the limit.
Create a trigger to validate that no more than 3 contact get added to an
account. */
trigger LimitOfContactOnAcc on Contact (before insert, before update, after
undelete) { Set<Id> accIds=new Set<Id>(); if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) { if(trigger.isBefore && trigger.isInsert) { for(Contact con:trigger.new) { if(con.AccountId <> null) { accIds.add(con.AccountId); } } } else if(trigger.isBefore && trigger.isUpdate) { for(Contact con:trigger.new) { if(con.AccountId <> null){ Contact oldMapValue=trigger.oldMap.get(con.Id); if(oldMapValue.AccountId !=con.AccountId) { accIds.add(con.AccountId); } } } } Set<Id> accListWithCon = new Map<Id, AggregateResult>([SELECT AccountId Id FROM Contact WHERE AccountId = :accIds GROUP BY AccountId HAVING COUNT(Id) >=3]).keySet(); if(accListWithCon.size()>0){ for(Contact newCon: Trigger.new) { if(newCon.AccountId <> null && accListWithCon.contains(newCon.AccountId)) { newCon.addError('SORRY!! You can not insert more than 3 contacts on Account.'); } } } }
if(trigger.isAfter && trigger.isUndelete)
{ for(Contact con:trigger.new) { if(con.AccountId <> null) { accIds.add(con.AccountId); } } Set<Id> accListWithCon = new Map<Id, AggregateResult>([SELECT AccountId Id FROM Contact WHERE AccountId = :accIds GROUP BY AccountId HAVING COUNT(Id) > 3]).keySet(); if(accListWithCon.size()>0){ for(Contact newCon: Trigger.new) { if(newCon.AccountId <> null && accListWithCon.contains(newCon.AccountId)) { newCon.addError('SORRY!! You can not insert more than 3 contacts on Account.'); } } } } }
// using after event trigger
/* Q2.Create a trigger to validate that no more than 3 contact get added to an
account. */
trigger LimitOfContactOnAcc on Contact (after insert, before Insert, before update,
after update, after undelete) { Set<Id> accIds = new Set<Id>(); if (Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete)){ for(Contact con:trigger.new) { if(con.AccountId <> null) { accIds.add(con.AccountId); } } Set<Id> accListWithCon = new Map<Id, AggregateResult>([SELECT AccountId Id FROM Contact WHERE AccountId = :accIds GROUP BY AccountId HAVING COUNT(Id) > 3]).keySet(); if(accListWithCon.size()>0){ for(Contact newCon: Trigger.new) { if(newCon.AccountId <> null && accListWithCon.contains(newCon.AccountId)) { newCon.addError('SORRY!! You can not insert more than 3 contacts on Account.'); } } } } }
Online Learning Management System - For The Enhancement and Development of The New Normal Online Based Learning System of Muntinlupa National High School Main - Senior High School