Trigger Challenge #1 : SalesforceCody

0
2147

Welcome Back with new Challenge Series, Yes Triggers. Let’s do Live Use Case Trigger

Hello Salesforce Cody’s,

Let’s get started

Firstly, Create a Custom SObject Names as “Contact Optout

Fields to Be Created

  • Contact (Master Details)
  • SuppressionDate (Date)
  • CreateType (pick list => ‘Data Issue’, ‘ReCheck’)

Create some new Fields on Contact SObject

  • Data_issue (Checkbox)
  • Recheck (Checkbox)

We need a trigger on Contact SObject on the even of insert/update:

  1. If contact record has missed the value in the following fields then Data_Issue should
    • mark as TRUE else FALSE
    • 1.1 FirstName, Email, MailingStreet, MailingCity, MailingState, MailingPostalCode,MailingCountry
  2. If Data_issue/Recheck is true then we need to create Contact_Optout record for that
    • Contact record as following:
    • Data_Issue = true than Contact_Optout CreatedType = Data_Issue
    • Recheck = true than Contact_Optout CreatedType = Recheck
    • SuppressionDate = Today + 10 Years
  3. If Data_issue/Recheck is false then we need to delete respect Contact_Optout record
    • of that Contact of the respected CreatedType
  • Notes
    • There should only be one Contact_Optout record with each CreateType value
    • for a single Contact record, that mean there should not be two Contact_Optout
    • records where Created_Type=‘Data_issue’.

Here is the Solution:

trigger ContactOptout on Contact (Before Insert , Before Update , After Insert , After Update) {
    
    Id cid;
    String createdType ;
    List<Contact_Optout__c> conOptList = new List<Contact_Optout__c>();
    List<Contact_Optout__c> extConOptList = new List<Contact_Optout__c>();
    List<Contact>oldContactList = new List<Contact>();
    
    if((Trigger.isBefore && Trigger.isInsert) || (Trigger.isBefore && Trigger.isUpdate)){
        for(Contact c : Trigger.New){
            cid = c.id;    
            if(c.FirstName == null || c.Email == null || c.MailingStreet == null || c.MailingCity == null || c.MailingPostalCode == null || 
               c.MailingState == null || c.MailingCountry == null){
                   c.Data_Issue__c = true;
                   c.ReCheck__c = false;         
               }
            else{
                c.ReCheck__c = true;  
                c.Data_Issue__c = false;       
            } 
        }         
    }    
    
    if(Trigger.isAfter && Trigger.isInsert){
        for(Contact c : Trigger.New){
            if(c.Data_Issue__c == true){
                Contact_Optout__c conOpt = new Contact_Optout__c();
                conOpt.Name = 'New1';
                conOpt.Contact__c = c.id;
                conOpt.CreatedType__c = 'Data Issue'; 
                conOpt.SuppressionDate__c = System.Today().addYears(10);
                conOptList.add(conOpt); 
                
            }
            else if(c.ReCheck__c == True){
                Contact_Optout__c conOpt = new Contact_Optout__c();
                conOpt.Name = 'New1';
                conOpt.Contact__c = c.id;
                conOpt.CreatedType__c = 'ReCheck'; 
                conOpt.SuppressionDate__c = System.Today().addYears(10);
                conOptList.add(conOpt);  
                
            }
        }
        if(!conOptList.isEmpty()){
            insert conOptList;
        }
        if(!extConOptList.isEmpty()){
            insert extConOptList;
        }
        
        
    }
    if(Trigger.isAfter && Trigger.isUpdate){
        
        for(Contact c : Trigger.New){  
            cid = c.id;              
        }
        oldContactList =[Select id , Data_Issue__c , ReCheck__c From Contact Where id=:cid];
        System.debug('The OldCOntactList is '+ oldContactList);
        conOptList = [Select id , name , CreatedType__c , SuppressionDate__c From Contact_Optout__c where Contact__c IN: oldContactList ];
        if(oldContactList[0].Data_Issue__c == false && oldContactList[0].ReCheck__c == false){
            for(Contact_Optout__c conOpt : conOptList){
                extConOptList.add(conOpt);    
            }
        }
        
        for(Contact_Optout__c conOpt : conOptList){
            System.debug('the Contact Optout List is-----------------------> '+ conOptList);
            if(oldContactList[0].Data_Issue__c == true){
                System.debug('the Contact Optout List is IN'+ conOptList);
                conOpt.CreatedType__c = 'Data Issue';
                
            }
            else{
                conOpt.CreatedType__c = 'ReCheck';    
            }      
        }
        
        if(!extConOptList.isEmpty()){
            delete extConOptList;
        }
        update conOptList;       
    }       
}

Let us know in the comments if you stuck at somewhere

Contact us @salesforcecodyadmin [email protected]

Connect Us on Facebook : https://www.facebook.com/SalesforceCody

Connect me on Linkedin: https://www.linkedin.com/in/umeshbeti

Affordable Ways to Boost Your Resume Affordable Ways to Demonstrate Your Salesforce Knowledge Affordable Ways to Enhance Your Career Affordable Ways to Get Certified Affordable Ways to Validate Your Salesforce Skills Apex Buy Your Certification Vouchers Today Discounted Certification Exam Vouchers Discounted Exam Vouchers Discounted Exam Vouchers for Salesforce Certifications Discounted Salesforce Certification Exams Discounted Vouchers for Salesforce Certifications Get Certified and Boost Your Career Get Certified and Boost Your Earnings Get Certified and Get Ahead Get Certified and Stand Out from the Crowd Get Certified at a Discount Get Certified at a Discounted Price Invest in Your Career with Certification Invest in Your Future with Salesforce Certification Maximize Your Salesforce Career Potential Saleforce crm Salesforce Salesforce Certification salesforce certification coupon codes Salesforce Certification Preparation salesforce certification vouchers SalesforceCody Salesforce Cody Salesforce Exam Salesforce Interview Salesforce Interview Questions Salesforce Trigger Salesforce Trigger Advance Salesforce Trigger Basics Salesforce Trigger Example Salesforce Triggers Tasks Salesforce Trigger Tasks Salesforce Updates Save Money and Get Certified Save Money on Certification Exams Save Money on Salesforce Certification Exams Save Money on Your Exams Trigger Trigger Tasks Apex