Dear #Trailblazers,
Here is the Link to Many Apex Trigger Scenarios that you can practice and get more and more strong into Apex Trigger.
Thanks & Happy Coding
Dear #Trailblazers,
Here is the Link to Many Apex Trigger Scenarios that you can practice and get more and more strong into Apex Trigger.
Thanks & Happy Coding
Comments are closed.
Hello could you please provide solutions
Unfortunately, We have not developed the solution and the participants have not shared their code.
Solution for Row#3
// Write a trigger on contact to prevent duplicate records based on Contact Email & Contact Phone.
// Asumption: Only on insert context, if you want to consider the update case then check if any of the values have changed between oldMap and new list
trigger ContactTrigger on Contact(before insert){
Set<String> setEmailPhoneKeys = new Set<String>();Set<String> setEmails = new Set<String>();
Set<String> setPhones = new Set<String>();
for(Contact objContact : Trigger.new){
if(objContact.Email != null && objContact.Phone != null){
setEmails.add(objContact.Email);
setPhones.add(objContact.Phone);
}
}
for(Contact objContact : [SELECT Phone, Email from Contact WHERE Phone IN : setPhones AND Email IN : setEmails]){
setEmailPhoneKeys.add(objContact.Email + '-' + objContact.Phone);
}
for(Contact objContact : Trigger.new){
if(objContact.Email != null && objContact.Phone != null){
String key = objContact.Email + '-' + objContact.Phone;
if(setEmailPhoneKeys.contains(key)){
objContact.addError('Duplicate contact exists!');
}
}
}
}
Solution Row#4
// Write a trigger, only system admin user should be able to delete the task.
trigger TaskTrigger on Task(before delete){
Id profileId = UserInfo.getProfileId();String profileName = [SELECT Name from Profile WHERE ID =: profileId].Name;
for(Task objTask : trigger.old){
if(profileName != 'System Administrator'){
objTask.addError('Not System admin');
}
}
}