You need to sign in to do that
Don't have an account?
Ludivine
Could you help me write a test Class for 2 Triggers?
Dear all,
I have made 2 triggers to create an event when a condition is true and another one to send the link by mail when the event is created.
Now I need help to build a test class for these triggers because test coverage is 0%
Here are my triggers :
And I have modified an existing test class on triggers but code coverage is not changing on my trigger :(
From : // Test Triggers TriggerEventForCustomerToContact to
I have made 2 triggers to create an event when a condition is true and another one to send the link by mail when the event is created.
Now I need help to build a test class for these triggers because test coverage is 0%
Here are my triggers :
trigger TriggerEventForCustomerToContact on Amcor_Surveys__c (After insert) { set<id> setamcorIds= new set<id>(); For (Amcor_Surveys__c ams : trigger.new) { If(ams.Q6_NeedAmcorToContactYou__c == 'Yes') { setamcorIds.add(ams.Id); } } list<Amcor_Surveys__c> listSelectedAmcor= [select Account_ID__r.OwnerId,Contact_ID__c from Amcor_Surveys__c where id in :setamcorIds]; System.debug('Creating event record'); list<Event> listEventToCreate= new list<Event> (); for(Amcor_Surveys__c ams :listSelectedAmcor) { //create an event record Event ev = new Event(); Ev.Business_group__c ='Amcor Flexibles Europe & Americas'; Ev.Whatid = ams.id; ev.RecordTypeId='012g00000004ajF';//'Visit Report VOC'; ev.Type ='Customer Satisfaction Survey'; ev.DurationInMinutes = 720; ev.Ownerid = ams.Account_ID__r.OwnerId; ev.whoId =ams.Contact_ID__c; ev.Subject ='Customer Satisfaction Survey - Customer contact' ; ev.objective__c = 'Improve Relationship'; //System.debug('************ ' + system.today()); Date startDate = system.today(); ev.ActivityDate = startDate; ev.StartDateTime = Datetime.newInstanceGmt(startDate.year(), startDate.month(), startDate.day(), 0, 0, 9); ev.ActivityDateTime = Datetime.newInstanceGmt(startDate.year(), startDate.month(), startDate.day(), 0, 0, 09); Date EndDate = startdate +5; System.debug('Attempting to insert...'); listEventToCreate.add(ev); } try{ insert listEventToCreate; } catch(Exception e) { System.debug(e.getMessage()); } }
trigger Trigger_Event_Send_Email on Event (After insert) { for(Event evt: Trigger.New) if(evt.type == 'Customer Satisfaction Survey'){ Set<Id> ownerIds = new Set<Id>(); Set<Id> initiatorIds = new Set<Id>(); Set<Id> amsIds = New Set<Id>(); Set<Id> AccIds = New Set<Id>(); //Value in AssignedTO ownerIds.add(evt.OwnerId); Map<Id, User> userMap = new Map<Id,User>([select Name, Email from User where Id in :ownerIds]); //Value in CreatedBy initiatorIds.add(evt.CreatedById); Map<Id, User> userMap2 = new Map<Id,User>([select Name, Email from User where Id in :initiatorIds]); //Related To WhatId = Opportunity amsIds.add(evt.WhatId); Map<id, Amcor_Surveys__c > amsMap = new Map<Id,Amcor_Surveys__c>([select Name from Amcor_Surveys__c where Id in :amsIds]); //Related To WhatId = Account AccIds.add(evt.WhatId); Map<id, Account> AccMap = new Map<Id,Account>([select Name from Account where Id in :AccIds]); // parameters String Whatid ; User User = userMap2.get(evt.CreatedById); User Assignee = userMap.get(evt.ownerId); if(amsMap.containsKey(evt.whatid)){ Whatid = amsMap.get(evt.WhatId).Name; } if(AccMap.containsKey(evt.whatid)){ Whatid = AccMap.get(evt.WhatId).Name; } // email config Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {Assignee.Email}; mail.setToAddresses(toAddresses); // Set the TO addresses mail.setSubject('VOC Survey : A new event is added in your Calendar'); // Set the subject String template = 'Hello {0}, \nThe following Visit Report has been created for you :\n'; String duedate = ''; template+= '\n'; template+= 'Due Date - {1}\n'; template+= 'Type - {2}\n'; template+= 'Subject - {3}\n'; template+= 'Assign To - {4}\n'; template+= 'Related To - {5}\n'; template+= '\n'; template+='Click on the following link to access the Visit Report:\n'; template+= '{6}\n'; // Check field Send Email Alert // If(System.now() == evt.Send_Email_Alert__c ) // evt.SendEmailAlert__c = True; // calculate end date of event if (evt.EndDateTime ==null) duedate = ''; Else duedate = evt.EndDateTime.format(); List<String> args = new List<String>(); args.add(Assignee.Name); args.add(duedate); args.add(evt.type); args.add(evt.subject); args.add(Assignee.Name); args.add(whatid); args.add('https://'+System.URL.getSalesforceBaseURL().getHost()+'/'+evt.Id); // Here's the String.format() call. String formattedHtml = String.format(template, args); mail.setPlainTextBody(formattedHtml); Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail}); // } } }
And I have modified an existing test class on triggers but code coverage is not changing on my trigger :(
From : // Test Triggers TriggerEventForCustomerToContact to
// // This class contains unit tests for validating all Triggers // @isTest private class TriggerTests { private static testMethod void triggersTestAccountUpdate(){ Profile p = [select id from profile where name='Standard User']; User u = new User(alias = 'standt', email='standarduser4@testorg.com',emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles', username='TESTUSER1234@testorg.com'); System.runAs(u) { // account update Account A = new Account(); A.Name = 'Test A'; try{ insert A; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } Account A1 = ([Select Id from Account where Name='Test A' limit 1]); //A1.Last_Object_Updated_Delta__c = 5; update A1; List <Account> acclist = ([Select Id from Account where Name='Test A']); String createdbyId = UserInfo.getName(); String UserProfileId = UserInfo.getProfileId(); for (Account acc:acclist) { double Delta = 100; // if the Last_Modified_Date_Time__c hasn't been modified for the last second then it is an account update if (Delta >1){ if (UserProfileId != '00e20000000ju28AAA'){ String ModifiedDateTime = ''; Datetime cDT = System.now(); ModifiedDateTime = cDT.format('d/MM/yyyy hh:mm a'); // Compose the Modified_By with user name and update date/time acc.Last_Modified_By__c = createdbyId +', '+ ModifiedDateTime; acc.Last_Object_Modified__c='Account'; // This last one re-introduced by Carl V on 24th, to address navigation request. acc.Last_Updated_Date_Time__c = cDT; } } } // Opportunity update Opportunity Op = new Opportunity(); Op.Name = 'Op test'; Op.Type = 'Volume Change'; Op.accountId = A.id; Op.Unadj_Expected_Annual_Impact__c = 101.01; Op.Expected_PM_of_New_Volume__c = 12; Op.Expected_Units_Annually__c = 5; Op.Actual_Units_Annually__c = 12; Op.StageName = 'Idea Action'; Op.CloseDate = system.today(); try{ insert Op; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // AC update Amcor_Contract__c AC = new Amcor_Contract__c(); AC.Name = 'AC test'; AC.Account__c = A.id; try{ insert AC; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // ACP update Amcor_Contract_Portfolio__c ACP = new Amcor_Contract_Portfolio__c(); ACP.CurrencyIsoCode = 'AUD'; ACP.Account__c = A.id; try{ insert ACP; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // ACT update Amcor_Contract_Term__c ACT = new Amcor_Contract_Term__c(); ACT.CurrencyIsoCode = 'AUD'; ACT.Account__c = A.id; try{ insert ACT; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // AE update Account_Economics__c AE = new Account_Economics__c(); AE.CurrencyIsoCode = 'AUD'; AE.Account__c = A.id; try{ insert AE; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // attachement Attachment At = new Attachment(); At.ParentId = A.id; At.Name = 'test At'; At.Body = Blob.valueOf( 'this is an attachment test' ); insert At; // BAI update Basic_Account_Intelligence__c BAI = new Basic_Account_Intelligence__c(); BAI.CurrencyIsoCode = 'AUD'; BAI.Account__c = A.id; try{ insert BAI; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // BAI update Bottom_Performing_Amcor_Product__c BPAP = new Bottom_Performing_Amcor_Product__c(); BPAP.Name = 'Test BPAP'; BPAP.Account__c = A.id; try{ insert BPAP; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // CNA update Customer_Needs_Assessment__c CNA = new Customer_Needs_Assessment__c(); CNA.CurrencyIsoCode = 'AUD'; CNA.Account__c = A.id; try{ insert CNA; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // CRA update Competitive_Risk_Assessment__c CRA = new Competitive_Risk_Assessment__c(); CRA.Name = 'Test CRA'; CRA.Account__c = A.id; try{ insert CRA; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // KF Insert Key_Financials__c KF = new Key_Financials__c(); KF.CurrencyIsoCode = 'AUD'; KF.Account__c = A.id; try{ insert KF; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // KPI Insert Key_Performance_Indicator__c KPI = new Key_Performance_Indicator__c(); KPI.CurrencyIsoCode = 'AUD'; KPI.Account__c = A.id; try{ insert KPI; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // KTM Insert Account_Team_Member__c KTM = new Account_Team_Member__c(); KTM.CurrencyIsoCode = 'AUD'; KTM.Account__c = A.id; try{ insert KTM; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // LL Insert Largest_Leakage__c LL = new Largest_Leakage__c(); LL.Name = 'Test LL'; LL.Account__c = A.id; try{ insert LL; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // NA Insert Note NA = new Note(); NA.Title = 'Test Note'; NA.ParentId = A.id; try{ insert NA; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // OB Insert Business_with_other_BGs_BUs__c OB = new Business_with_other_BGs_BUs__c(); OB.CurrencyIsoCode = 'AUD'; OB.Account__c = A.id; try{ insert OB; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // RSA Insert Risk_of_Substitution_Assessment__c RSA = new Risk_of_Substitution_Assessment__c(); RSA.Name = 'Test RSA'; RSA.Account__c = A.id; try{ insert RSA; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // SMRA Insert Self_Manufacturing_Risk_Assessment__c SMRA = new Self_Manufacturing_Risk_Assessment__c(); SMRA.Name = 'Test SMRA'; SMRA.Account__c = A.id; try{ insert SMRA; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // STL Insert Ship_to_Location__c STL = new Ship_to_Location__c(); STL.Name = 'Test STL'; STL.Account__c = A.id; try{ insert STL; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // Task Insert Task Ta = new Task(); Ta.Type = 'Test Ta'; Ta.WhatId = A.id; try{ insert Ta; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // TCP Insert Top_Customer_Product__c TCP = new Top_Customer_Product__c(); TCP.Name = 'Test TCP'; TCP.Account__c = A.id; try{ insert TCP; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // TPAP Insert Top_Performing_Amcor_Product__c TPAP = new Top_Performing_Amcor_Product__c(); TPAP.Name = 'Test TPAP'; TPAP.Account__c = A.id; try{ insert TPAP; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // VPD Insert Value_Proposition__c VPD = new Value_Proposition__c(); VPD.CurrencyIsoCode = 'AUD'; VPD.Account__c = A.id; try{ insert VPD; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // Contact Insert Contact Co = new Contact(); Co.Email = 'testcontact@test.com'; Co.AccountId = A.id; try{ insert Co; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } // Test Triggers TriggerEventForCustomerToContact Amcor_Surveys__c amsv = new Amcor_Surveys__c(); amsv.Account_ID__c = a.id ;//'TEST COMPANY'; amsv.Contact_ID__c = co.id ;//'Ludivine Jobit' amsv.Q6_NeedAmcorToContactYou__c ='Yes'; try{ insert amsv; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } set<id> setamcorIds= new set<id>(); If(amsv.Q6_NeedAmcorToContactYou__c == 'Yes') { setamcorIds.add(amsv.Id); } list<Amcor_Surveys__c> listSelectedAmcor= [select Account_ID__r.OwnerId,Contact_ID__c from Amcor_Surveys__c where id in :setamcorIds]; System.debug('Creating event record'); list<Event> listEventToCreate= new list<Event> (); for(Amcor_Surveys__c ams :listSelectedAmcor) { // Event Insert Event Ev = new Event(); Ev.Business_group__c ='Amcor Flexibles Europe & Americas'; Ev.Whatid = ams.id; ev.RecordTypeId='012g00000004ajF';//'Visit Report VOC'; ev.Type ='Customer Satisfaction Survey'; ev.DurationInMinutes = 720; ev.Ownerid =p.id ; ev.whoId =co.id; ev.Subject ='Customer Satisfaction Survey - Customer contact' ; ev.objective__c = 'Improve Relationship'; //System.debug('************ ' + system.today()); Date startDate = system.today(); ev.ActivityDate = startDate; ev.StartDateTime = Datetime.newInstanceGmt(startDate.year(), startDate.month(), startDate.day(), 0, 0, 9); ev.ActivityDateTime = Datetime.newInstanceGmt(startDate.year(), startDate.month(), startDate.day(), 0, 0, 09); Date EndDate = startdate +5; listEventToCreate.add(ev); } try{ insert listEventToCreate; } catch (System.DmlException e){ System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } //Fire Email for Event Customer Satisfaction Survey //tbd // end tests } }
All Answers
The issue was that some mandatory fields on the object were missing in my Test Class. Thank you so much Shyam!!Have a nice week end.