This trigger enforces data integrity by preventing the creation of Contact records without a parent Account. Users receive an error message when attempting to save such Contacts.
For more trigger examples, visit this YouTube playlist.
Solution:
public class ContactTriggerHandler {
public static void handleBeforeInsert (List<<Contact> newRecords) {
for (Contact conRecord : newRecords) {
if (conRecord.AccountId == null) {
//throw an error.
conRecord.addError(‘Parent Information is mandatory for contact creation’);
}
}
}
}