top of page
skillbirdsjc

Real Time Interview Frequently Asked Apex Trigger Scenarios | Salesforce Interview Question | Apex Triggers Concepts & Scenarios | Cognizant Interview Question | Tech SkillBirds Enterprise

Updated: Oct 5


Write trigger checks for duplicate contacts based on email and phone numbers during insert and update operations.


1. Trigger Definition


trigger DuplicateCheck on Contact (before insert, before update) {

This is a trigger on the Contact object.

It fires before an insert or an update operation on the Contact records.

2. Map Initialization


Map<String, Contact> emailMap = new Map<String, Contact>();

Map<String, Contact> phoneMap = new Map<String, Contact>();

Two maps are initialized: emailMap and phoneMap.

These maps will store contacts from the trigger with their emails and phone numbers as keys.

3. Loop Through Triggered Contacts


for(Contact contact : trigger.new) {

Loops through each Contact record being inserted or updated (trigger.new contains the new versions of the records).

4. Handling Insert Operations



if(trigger.isInsert) {

emailMap.put(contact.Email, contact);

phoneMap.put(contact.Phone, contact);

}

Checks if the current operation is an insert.

Adds the contact's email and phone to the respective maps (emailMap and phoneMap).

5. Handling Update Operations


if(trigger.isUpdate) {

if(trigger.oldMap.get(contact.Id).Email != contact.Email){

emailMap.put(contact.Email,contact);

}

if(trigger.oldMap.get(contact.Id).Phone != contact.Phone){

phoneMap.put(contact.Phone,contact);

}

}

Checks if the current operation is an update.

If the email or phone number has changed, the new email or phone number is added to the respective map.

trigger.oldMap.get(contact.Id).Email retrieves the old email value.

Compares it to the new email (contact.Email). If different, it adds the new email to emailMap.

Similarly, it handles phone numbers.

6. Error Message Initialization


String errorMessage = '';

Initializes a string variable to store any potential error message.

7. Query for Existing Contacts


List<Contact> existingContactList = [Select Id, Email, Phone FROM Contact Where Email IN:emailMap.keySet() OR Phone IN:phoneMap.keySet()];

Queries the Contact object for any existing records where the email or phone matches those in emailMap or phoneMap.

This helps to identify if there's a duplicate based on email or phone number.

8. Check for Duplicates


if(existingContactList.size() > 0){

for(Contact contactRec : existingContactList){

if(contactRec.Email != null){

if(emailMap.get(contactRec.Email) != null){

errorMessage = 'Email';

}

}

if(contactRec.Phone != null){

if(phoneMap.get(contactRec.Phone) != null) {

errorMessage = errorMessage + (errorMessage != '' ? 'and Phone ' : 'Phone ');

}

}

if(errorMessage != ''){

trigger.new[0].addError('Your Contact ' + errorMessage + ' already exists in system.');

}

}

}

If any contacts are found that match the queried email or phone, it iterates through these contacts.

It checks:

If the Email of the existing contact matches one in emailMap, it sets errorMessage to 'Email'.

If the Phone of the existing contact matches one in phoneMap, it appends 'and Phone' to errorMessage if errorMessage already has 'Email'. Otherwise, it sets it to 'Phone'.

If errorMessage is not empty, it adds an error to the first contact in trigger.new.

addError prevents the record from being saved and shows the message to the user.

The error message indicates that the contact with this email or phone already exists.







Building greater futures through innovation and collective knowledge

Tech SkillBirds Enterprise is an IT services, consulting and business solutions organization that has been partnering with many of the world’s largest businesses for the past 13 years. We believe innovation and collective knowledge can transform all our futures with greater purpose.


Website : www.techskillbirds.com

Telegram : https://t.me/techskillbirds

What's Up : +91 63010 61963

Facebook : profile.php?id=61565870777368

Instagram : techskillbirds/

LinkedIn : company/skillbirds-acquire-enterprise/

YouTube : /@techskillbirds638/videos

Pinterest : TechSkillBirds/

Twitter : @techskillbirds

urbanpro : hyderabad/tech-skillbirds-enterprise-hitec-city/32237376

JustDail : Hyderabad/Tech-Skillbirds-Enterprise-Secunderabad-City

Indiamart: skillbirds-acquire-enterprise/



About Us : https://www.techskillbirds.com/

Services : https://www.techskillbirds.com/blank-1

Buy Services : https://www.techskillbirds.com/book-o...

Corporate Trainings : https://www.techskillbirds.com/shop

Software Training : https://www.techskillbirds.com/s-proj...

Blog : https://www.techskillbirds.com/blog

Contact Us : https://www.techskillbirds.com/about-1

Plans & Pricing : https://www.techskillbirds.com/plans-...

21 views0 comments

Recent Posts

See All

Opmerkingen


bottom of page