Prevent Contact Creation Without Parent Account

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’);
                            }
                 }
        }
}

 

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)