You need to sign in to do that
Don't have an account?
Test class on Trigger to create Task on Opportunity update.
Hello all,
I have created below trigger and a test class for it, still its not covering the trigger. What needs to be changed in this test class?
Please help
Trigger:
trigger OppTask on Opportunity (after update) {
List <Contact> conList = [SELECT ID,AccountID FROM Contact WHERE AccountId IN (SELECT AccountId FROM Opportunity WHERE Id IN :Trigger.new)];
map<ID,Contact> accountIDWithContactMap = new map<ID,Contact>();
for(Contact con : conList){
accountIDWithContactMap.put(con.AccountID, con);
}
for(Opportunity Opp : Trigger.new){
if(Trigger.newMap.get(opp.Id).StageName != Trigger.oldMap.get(opp.Id).StageName && opp.StageName == 'Closed Won' && accountIDWithContactMap.containsKey(Opp.AccountId)){
Task T = new Task();
T.WhatId = opp.Id;
T.OwnerId = opp.OwnerId;
T.Subject = 'Call';
T.Status = 'In Progress';
T.IsReminderSet = TRUE;
T.WhoId = accountIDWithContactMap.get(Opp.AccountId).ID;
T.ReminderDateTime = DateTime.now().addHours(24);
insert T;
}
}
Test Class:
@isTest
public class testOppTaskTrigger{
static testMethod void ValidateOppTaskTrigger(){
Test.startTest();
Account testAcc = new Account (Name = 'Test Account');
insert testAcc;
User U = [SELECT ID, Name FROM User WHERE Profile.Name = 'System Administrator'];
Opportunity testOpp = new Opportunity (Name = 'Test Name',
OwnerId =U.Id,
AccountId = testAcc.Id,
StageName = 'Open',
Amount = 50000.00,
CloseDate = System.today()
);
insert testOpp;
Opportunity testOpp2 = [SELECT Name, Id, StageName FROM Opportunity WHERE Id =: testOpp.Id];
if(testOpp2.StageName == 'Closed Won') {
update testOpp;
Task testTask = new Task ( WhatId = testOpp2.Id,
OwnerId = testOpp2.OwnerId,
Subject = 'Call',
Status = 'In Progress',
IsReminderSet = TRUE,
ReminderDateTime = DateTime.now().addHours(24));
insert testTask;
Test.stopTest();
}
}
}
I have created below trigger and a test class for it, still its not covering the trigger. What needs to be changed in this test class?
Please help
Trigger:
trigger OppTask on Opportunity (after update) {
List <Contact> conList = [SELECT ID,AccountID FROM Contact WHERE AccountId IN (SELECT AccountId FROM Opportunity WHERE Id IN :Trigger.new)];
map<ID,Contact> accountIDWithContactMap = new map<ID,Contact>();
for(Contact con : conList){
accountIDWithContactMap.put(con.AccountID, con);
}
for(Opportunity Opp : Trigger.new){
if(Trigger.newMap.get(opp.Id).StageName != Trigger.oldMap.get(opp.Id).StageName && opp.StageName == 'Closed Won' && accountIDWithContactMap.containsKey(Opp.AccountId)){
Task T = new Task();
T.WhatId = opp.Id;
T.OwnerId = opp.OwnerId;
T.Subject = 'Call';
T.Status = 'In Progress';
T.IsReminderSet = TRUE;
T.WhoId = accountIDWithContactMap.get(Opp.AccountId).ID;
T.ReminderDateTime = DateTime.now().addHours(24);
insert T;
}
}
Test Class:
@isTest
public class testOppTaskTrigger{
static testMethod void ValidateOppTaskTrigger(){
Test.startTest();
Account testAcc = new Account (Name = 'Test Account');
insert testAcc;
User U = [SELECT ID, Name FROM User WHERE Profile.Name = 'System Administrator'];
Opportunity testOpp = new Opportunity (Name = 'Test Name',
OwnerId =U.Id,
AccountId = testAcc.Id,
StageName = 'Open',
Amount = 50000.00,
CloseDate = System.today()
);
insert testOpp;
Opportunity testOpp2 = [SELECT Name, Id, StageName FROM Opportunity WHERE Id =: testOpp.Id];
if(testOpp2.StageName == 'Closed Won') {
update testOpp;
Task testTask = new Task ( WhatId = testOpp2.Id,
OwnerId = testOpp2.OwnerId,
Subject = 'Call',
Status = 'In Progress',
IsReminderSet = TRUE,
ReminderDateTime = DateTime.now().addHours(24));
insert testTask;
Test.stopTest();
}
}
}
Try with below code it will give you 100% code coverage ,
Let me know if it helps .
Thanks
Manoj
Why would we need the soql on lines 7, 14 and 23 which is selecting Id from opp? Obviously it will give an error while compiling. And why do we need to create a Contact record for this?
Have tried with the code ? It is best practice to check with Either System.assert/assertEquals/assertNotEquals to check the record is inserted or not .
I think will not give you compile time or run time error .
To know more about the test class please check below link it has a lot of links which will give you a lot of infromation about test class .
Let me if it help or still you have doubt .
Thanks
Manoj
Thanks!