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.