0

This question already has an answer here:

I am new to Salesforce and I am writing a test class for the trigger. Can anyone tell me how to write a test class for the trigger below?

trigger QBRNewRec on Account (After insert, After Update) {
if(Trigger.isInsert)
{
    List<Event> ev = new List<Event>();
    for(Account acc: trigger.new)
    {
        DateTime cur=System.now();
        if(acc.Customer_Tier__c=='Tier 1')
        {
            for(Integer i = 1;i<=4;i++)
            {
                Event eve = new Event();
                eve.OwnerId=acc.CreatedById;
                eve.WhatId=acc.id;
                eve.QBR_Name__c='QBR:Q'+i+' '+acc.Name;
                eve.Subject='Call';

                if(eve.QBR_Name__c==('QBR:Q1'+' '+acc.Name))
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 1, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 1, 1+1);
                }
                else if(eve.QBR_Name__c==('QBR:Q2'+' '+acc.Name))
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 4, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 4, 1+1);
                }
                else if(eve.QBR_Name__c==('QBR:Q3'+' '+acc.Name))
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 7, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 7, 1+1);
                }
                else
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 10, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 10, 1+1);
                }

                eve.Status__c='In Progress';
                eve.Follow_up__c='Add Follow up Check List';
                eve.Notes__c='Add Notes';
                ev.add(eve);
            }
            insert ev;
        }
        else if(acc.Customer_Tier__c=='Tier 2')
        {
            for(Integer i = 1;i<=3;i++)
            {
                Event eve = new Event();
                eve.OwnerId=acc.CreatedById;
                eve.WhatId=acc.id;
                eve.QBR_Name__c='QBR:Q'+i+' '+acc.Name;
                eve.Subject='Call';

                if(eve.QBR_Name__c==('QBR:Q1'+' '+acc.Name))
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 1, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 1, 1+1);
                }
                else if(eve.QBR_Name__c==('QBR:Q2'+' '+acc.Name))
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 5, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 5, 1+1);
                }
                else
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 9, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 9, 1+1);
                }

                eve.Status__c='In Progress';
                eve.Follow_up__c='Add Follow up Check List';
                eve.Notes__c='Add Notes';
                ev.add(eve);
            }
            insert ev;
        }
        else{}
    }
}
if(Trigger.isUpdate)
{
    for(Account ac1:Trigger.old)
    {
        DateTime cur=System.now();
        List<Event> ev = new List<Event>();
        Id recordId=ac1.Id;
        System.debug('ID of Account'+recordId);
        for(Event a: [SELECT id, QBR_Name__c, StartDateTime, EndDateTime, Account.Name, Status__c, Follow_up__c, Notes__c, Subject from Event where whatid=:recordId AND RecordTypeId in (SELECT Id FROM RecordType WHERE Name = 'QBR')])
        {
            delete a;
        }
        for(Account ac:[SELECT id, Name, Customer_Tier__c, CreatedById from Account where id=:recordId])
        {
        if(ac.Customer_Tier__c=='Tier 1')
        {
            for(Integer i = 1;i<=4;i++)
            {
                Event eve = new Event();
                eve.OwnerId=ac.CreatedById;
                eve.WhatId=ac.id;
                eve.QBR_Name__c='QBR:Q'+i+' '+ac.Name;
                eve.Subject='Call';

                if(eve.QBR_Name__c==('QBR:Q1'+' '+ac.Name))
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 1, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 1, 1+1);
                }
                else if(eve.QBR_Name__c==('QBR:Q2'+' '+ac.Name))
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 4, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 4, 1+1);
                }
                else if(eve.QBR_Name__c==('QBR:Q3'+' '+ac.Name))
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 7, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 7, 1+1);
                }
                else
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 10, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 10, 1+1);
                }

                eve.Status__c='In Progress';
                eve.Follow_up__c='Add Follow up Check List';
                eve.Notes__c='Add Notes';
                ev.add(eve);
            }
            insert ev;
        }
        else if(ac.Customer_Tier__c=='Tier 2')
        {
            for(Integer i = 1;i<=3;i++)
            {
                Event eve = new Event();
                eve.OwnerId=ac.CreatedById;
                eve.WhatId=ac.id;
                eve.QBR_Name__c='QBR:Q'+i+' '+ac.Name;
                eve.Subject='Call';

                if(eve.QBR_Name__c==('QBR:Q1'+' '+ac.Name))
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 1, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 1, 1+1);
                }
                else if(eve.QBR_Name__c==('QBR:Q2'+' '+ac.Name))
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 5, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 5, 1+1);
                }
                else
                {
                eve.StartDateTime=Date.newinstance(cur.year(), 9, 1)+1;
                eve.EndDateTime=Date.newinstance(cur.year(), 9, 1+1);
                }

                eve.Status__c='In Progress';
                eve.Follow_up__c='Add Follow up Check List';
                eve.Notes__c='Add Notes';
                ev.add(eve);
            }
            insert ev;
        }
        else{}
        }
    }
}}

Thanks.

marked as duplicate by David Reed apex May 8 at 11:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Best you go through the relevant Trailhead modules and make an attempt yourself.

FYI the trigger code contains a lot of duplication which makes it harder to understand and so harder to write tests for. Changing to bits of code more like this should help with that:

List<Event> events = new List<Event>();
...
        for(Integer i = 1; i <= 4;i++)
        {
            Integer month;
            switch on i {
                when 1 { month = 1; }
                when 2 { month = 4; }
                when 3 { month = 7; }
                when else { month = 10; }
            }
            events.add(new Event(
                OwnerId = acc.CreatedById,
                WhatId = acc.id,
                QBR_Name__c = 'QBR:Q' + i + ' ' + acc.Name,
                Subject = 'Call',
                Status__c = 'In Progress',
                Follow_up__c = 'Add Follow up Check List',
                Notes__c = 'Add Notes',
                // Same day generated here? Date vs DateTime?
                StartDateTime = Date.newInstance(cur.year(), month, 1) + 1,
                EndDateTime = Date.newInstance(cur.year(), month, 1 + 1)
            ));
        }
...
insert events;

Not the answer you're looking for? Browse other questions tagged or ask your own question.