In this post we are going to write Trigger which we will show the error on Contact object if the contact for selected Account is more than 2. So it will restrict the user from creating contact if it is more than two. We cannot directly check the contact count which is associated with particular account in 'before trigger' scenario. So we have taken one temporary Set Id from Accounts and one list (listErrorContacts) for contact which will store all the values coming from Trigger new. Then we will check in both and add the error function.