How to Master Apex Triggers? Apex Trigger Scenarios

How to Master Apex Triggers? Apex Trigger Scenarios

Dear #Trailblazers,

Here is the Link to Many Apex Trigger Scenarios that you can practice and get more and more strong into Apex Trigger.

https://docs.google.com/spreadsheets/d/e/2PACX-1vTuWxvvEdeu1TIBrla8cD9-jiMaWUEJbylenZN4gU7qtEp-pbF2vCKfTDRdGce9e9pJdLG3GiYDTnJYH15vCkc/pubhtml

Thanks & Happy Coding

Amit Singh
Amit Singh

Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion

Articles: 299

Newsletter Updates

Enter your email address below and subscribe to our newsletter

5 Comments

  1. 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!');
    }
    }
    }

    }

  2. 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');
    }
    }

    }

Leave a Reply

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

Discover more from Panther Schools

Subscribe now to keep reading and get access to the full archive.

Continue reading