Scenario Sales reps often create Contacts quickly from the mobile app and sometimes leave the Last Name empty or type only spaces. Contacts without a last name cannot be searched, merged or used in email campaigns, so the data-quality team wants the save to be blocked with a clear message.
Task
Write a trigger handler on Contact that, before a Contact is inserted or updated:
LastName of every Contact in the batch.Enter a last name for this contact. to that record, using addError.Expected behavior
LastName and one with Valid, produces exactly one error. The message mentions the last name.LastName is only spaces also produces an error.Example
A batch of three Contacts: LastName = empty, LastName = three spaces, LastName = Smith.
Enter a last name for this contact.Write the logic for the before insert and before update events. Process every record in Trigger.new (the batch can hold many Contacts). Do not run SOQL or DML in this trigger; the check only needs the Contact's own field.