Trigger Challenge #2: Activity Score Triggers | Salesforce Cody

0
1887
ATTACHMENT DETAILS TriggerChallenge2-SalesforceCody
TriggerChallenge2-SalesforceCody

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

Hello Salesforce Cody’s,

Let’s get started

Assignment Brief

Code a few simple triggers on Activity (Task/Event) based on details below.

Data Model

Create new custom fields or use existing standard fields (as suitable).

ObjectFieldTypeValues
Task and EventTypePicklistEmail,Call,Meeting, Task
Account and ContactActivity ScoreNumber(18,2) 

Task #1 

 Update Activity “Type” field with following:

  • Type = Call, if Subject starts with “Call” case-insensitive.
  • Type = Email, if Subject starts with “Email” case-insensitive.
  • Type = Task, for other Subject.
  • Type = Meeting, for all Events.

Task #2

Activity Score on Contact and Account should be updated based on following formula.

Contact/Account Activity Score = 

Count of Meetings * 3 + Count of calls * 2 + Count of Emails * 1

Note: This count will be done separately for all child activities of Account/Contact records!

Deliverables

  1. Trigger(s) to achieve above functionality.
  2. Test Cases which thoroughly verify the functionality. 

Scoring Criteria

Scoring would be done not only based on achieving the requirements. But also based on quality of Trigger and Test code, please follow Salesforce best practices as applicable.

Well Check you answers here, here you go!

#1 task answer : Task Object Trigger

trigger ActivityUpdation on Task (before insert , after insert) {

    String s ='';
    Set<Id> aid = new Set<Id>();
    Set<Id> cid = new Set<Id>();
    Integer a=0,b=0,c=0,d=0,e,f;
    
    if(Trigger.isBefore && Trigger.isInsert){
        for(Task t : Trigger.New){
    
            if(t.subject.Contains('Call')){  
                t.type = 'Call'; 
            }
            else if(t.subject.Contains('Email')){      
                    t.type = 'Email';
            }
            else if(t.subject.Contains('Other')){
                    t.type = 'Task';
            }
            else if(t.subject.Contains('for all Events')){
                    t.type = 'Meeting';             
            }
        }   
    }
     
    if(Trigger.isAfter && Trigger.isInsert){
        for(Task t : Trigger.New){
            aid.add(t.WhatId);
            cid.add(t.WhoId);
            if(t.type == 'Call'){  
                ++a;
                s+='_Call'; 
            }
            else if(t.type == 'Email'){    
                    ++b;
                    s+='_Email'; 
            }
            else if(t.type == 'Task'){
                    ++c;
                    s+='_Task';
            }
            else if(t.type == 'Meeting'){
                    ++d;
                    s+='_Meeting';
            }  
        }
    
    if(!aid.isEmpty()){
        List<Account> acc = [Select id , name , Activity_Score__c from Account where id IN : aid];
        e = ((d*3)+(a*2)+(b*1));
        if(!acc.isEmpty()){
            for(Account a : acc){
                a.name+=s;
                if(a.Activity_Score__c == null){
                a.Activity_Score__c = e;
                }
                else{
                    a.Activity_Score__c += e;
                }
            }
        }
        update acc;
    }
    
    if(!cid.isEmpty()){
        List<Contact> con = [Select id , Lastname , Activity_Score__c from Contact where id IN : cid];
        e = ((d*3)+(a*2)+(b*1));
        
        if(!con.isEmpty()){
            for(Contact c : con){
                c.lastname+=s;
                if(c.Activity_Score__c == null){
                c.Activity_Score__c = e;
                }
                else{
                c.Activity_Score__c += e;
                }   
            }
        }   
        update con;
    }
    }
}

#2nd Task answer : Event Object Trigger 

trigger ActivityUpdations on Event (after insert) {
    Set<Id> aid = new Set<Id>();
    Set<Id> cid = new Set<Id>();
    Integer a=0,b=0,c=0,d=0,e,f;
     
    
    for(Event ev: Trigger.New){
    
        aid.add(ev.WhatId);
        cid.add(ev.WhoId);
        
        if(ev.type == 'Call'){  
            ++a;
        }
        else if(ev.type == 'Email'){    
                ++b;
        }
        else if(ev.type == 'Task'){
                ++c;
        }
        else if(ev.type == 'Meeting'){
                ++d;
        }  
    }
    
    List<Account> acc = [Select id , name , Activity_Score__c from Account where id IN : aid];
    e = ((d*3)+(a*2)+(b*1));
    for(Account a : acc){
    
        if(a.Activity_Score__c == null){
        a.Activity_Score__c = e;
        }
        else{
            a.Activity_Score__c += e;
        }
    }
    
    update acc;
    
    List<Contact> con = [Select id , Lastname , Activity_Score__c from Contact where id IN : cid];
    e = ((d*3)+(a*2)+(b*1));
    for(Contact c : con){
    
        if(c.Activity_Score__c == null){
        c.Activity_Score__c = e;
        }
        else{
        c.Activity_Score__c += e;
        }   
    }
    update con;
}

Let us know in the comments if you stuck at somewhere

Contact us @salesforcecodyadmin [email protected] for any quries.

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