You need to sign in to do that
Don't have an account?
Ashley Cobb 25
Trigger not firing in test class
Hello Everyone!
I have created a trigger and class in my sandbox that fires after insert and after update. I am now working on creating a test class for it, and I am achieving code coverage, but as soon as I add in System.assertEquals(1, [select count() from Commissions_Chart__c]); to verify that a record has been created, it fails. Any help is appreciated!! Trigger, class, and test class are below!
Trigger:
trigger CommissionsTrigger on Opportunity (after update, after insert) {
List<Opportunity> comToUpdate = new List<Opportunity>();
List<Opportunity> comToInsert = new List<Opportunity>();
List<Opportunity> comToDelete = new List<Opportunity>();
For(Opportunity opp:Trigger.new){
If(Trigger.isupdate){
Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
If(oldOpp.OwnerId != opp.OwnerId){
comToDelete.add(opp);
comToInsert.add(opp);
}
}
system.debug('Opps to Delete: ' + comToDelete);
System.debug('Opps to Insert: ' + comToInsert);
}
For(Opportunity o: Trigger.new){
If(Trigger.isinsert){
comToInsert.add(o);
}
If(Trigger.isUpdate){
comToUpdate.add(o);
}
}
if(comToInsert.size()>0){
CommissionsClass.CreateCommissionsObject(comToInsert);
system.debug('comToInsert List is : ' + comToInsert);
}
If(comToDelete.size()>0){
CommissionsClass.DeleteCommissionsObject(comToDelete);
system.debug('comToDelete List is : ' + comToDelete);
}
if(comToUpdate.size()>0){
CommissionsClass.UpdateCommissionsObject(comToUpdate);
System.debug('comToUpdate List is : ' + comToUpdate);
}
}
Class:
public class CommissionsClass {
public static void CreateCommissionsObject(Opportunity[] comToInsert){
List<Commissions_Chart__c> comInsert = new List<Commissions_Chart__c>();
List<Commissions__c> comToAttach = [select Id, OwnerId, Commissions_Month__c FROM Commissions__c WHERE Commissions_Month__c = THIS_MONTH];
For(Commissions__c cm : comToAttach){
For(Opportunity o : comToInsert){
If(cm.OwnerId == o.OwnerId){
Commissions_Chart__c c = new Commissions_Chart__c(Account_Name__c = o.AccountId, Close_Date__c = o.closeDate,
Opportunity_MRR__c = o.Billing_Amount_Monthly_Calcualted__c,Opportunity_Name__c = o.Id,
Opportunity_Stage__c = o.StageName, User__c = o.OwnerId, Commissions__c = cm.Id, Payment_Term__c = o.Contract_Payment_Terms__c,
Services_Amount__c = o.Professional_Service_Amount__c);
If(o.Total_Renewal_Term__c != NULL){
c.Term__c = o.Contract_Initial_Term_Months__c + o.Total_Renewal_Term__c;
}else{
c.Term__c = o.Contract_Initial_Term_Months__c;
}
comInsert.add(c);
}
}
}
System.debug('ComInsert list: ' + comInsert);
insert comInsert;
}
public static void DeleteCommissionsObject(Opportunity[] comToDelete){
List<Commissions_Chart__c> ccToDelete = new List<Commissions_Chart__c>();
List<Commissions_Chart__c> charts = [select Id, Opportunity_Name__c, User__c from Commissions_Chart__c where Opportunity_Name__c In : ComToDelete];
List<Opportunity> opps = [select ID, OwnerId from Opportunity where ID in :comToDelete];
For(Commissions_Chart__c ch : charts){
for (opportunity ops : opps){
If(ch.User__c != ops.OwnerId){
ccToDelete.add(ch);
}
}
}
Database.DeleteResult[] drList = Database.delete(ccToDelete, false);
for(Database.DeleteResult dr : drList){
if(dr.isSuccess()){
System.debug('Successfully deleted Commissions Chart with ID: ' + dr.getID());
}
else {
for (Database.Error err : dr.getErrors()){
system.debug('The following error has occurred.');
system.debug(err.getStatusCode() + ' : ' + err.getMessage());
System.debug('Commissions Chart fields that affected this error: ' + err.getFields());
}
}
}
}
public static void UpdateCommissionsObject(Opportunity[] comToUpdate){
List<Commissions_Chart__c> comUpdate = [SELECT Id, Account_Name__c, Close_Date__c, Opportunity_MRR__c, Opportunity_Name__c, Opportunity_Stage__c, User__c, Term__c
FROM Commissions_Chart__c WHERE Close_Date__c = THIS_YEAR];
List<Commissions_Chart__c>updatecom = new List<Commissions_Chart__C>();
for(Commissions_Chart__c cc:comUpdate){
For(opportunity op : comToUpdate){
If(cc.Opportunity_Name__c == op.Id){
cc.opportunity_stage__C = Op.StageName;
cc.Close_Date__c = op.CloseDate;
cc.Opportunity_MRR__C = op.Billing_Amount_Monthly_Calcualted__c;
cc.Payment_Term__c = op.Contract_Payment_Terms__c;
cc.Services_Amount__c = op.Professional_Service_Amount__c;
If(op.Total_Renewal_Term__c != NULL){
cc.Term__c = op.Contract_Initial_Term_Months__c + op.Total_Renewal_Term__c;
}else{
cc.Term__c = op.Contract_Initial_Term_Months__c;
}
updatecom.add(cc);
}
system.debug(updatecom);
}
}
update updatecom;
}
}
Test Class:
@isTest
public class CommissionsCalculatorTest {
static testMethod void CommissionsTriggerTest() {
//fetch IDs for records
Profile P = [select ID from Profile where Name = 'Standard User'];
RecordType rt = [select ID from recordtype where name ='New Business'];
//Create a user for testing
User u = new User(FirstName = 'Testing',
LastName = 'User',
email = 'testing@bizible.com',
monthly_quota__c = 5000,
username = 'testing@bizible.com',
Alias = 'tuser',
profileId = p.Id,
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US');
Insert u;
//Create account for Opportunities
Account a = new Account(Name = 'TestingCommissions',
OwnerId = u.Id,
ADR_Owner__C = u.Id);
Insert a;
//Create Commissions__C record for commissions Chart
Commissions__c c = new Commissions__c(Name = u.FirstName,
OwnerId = u.Id,
Commissions_month__c = System.today());
//Create Opportuntitiy for Trigger
List<Opportunity> Opps = new List<Opportunity>();
Opportunity o1 = new Opportunity(Name = 'TestCommissions1',
stageName = 'Qualified Discovery',
CloseDate = system.date.today(),
Amount = 12000,
AccountID = a.ID,
Contract_Payment_Terms__c = 'Monthly',
Contract_Initial_Term_Months__c = 12,
OwnerId = u.Id,
recordtypeID = rt.Id);
Test.startTest();
insert o1;
Test.stopTest();
System.assertEquals(1, [select count() from Opportunity]);
System.assertEquals(1, [select count() from Commissions_Chart__c]);
}
}
I have created a trigger and class in my sandbox that fires after insert and after update. I am now working on creating a test class for it, and I am achieving code coverage, but as soon as I add in System.assertEquals(1, [select count() from Commissions_Chart__c]); to verify that a record has been created, it fails. Any help is appreciated!! Trigger, class, and test class are below!
Trigger:
trigger CommissionsTrigger on Opportunity (after update, after insert) {
List<Opportunity> comToUpdate = new List<Opportunity>();
List<Opportunity> comToInsert = new List<Opportunity>();
List<Opportunity> comToDelete = new List<Opportunity>();
For(Opportunity opp:Trigger.new){
If(Trigger.isupdate){
Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
If(oldOpp.OwnerId != opp.OwnerId){
comToDelete.add(opp);
comToInsert.add(opp);
}
}
system.debug('Opps to Delete: ' + comToDelete);
System.debug('Opps to Insert: ' + comToInsert);
}
For(Opportunity o: Trigger.new){
If(Trigger.isinsert){
comToInsert.add(o);
}
If(Trigger.isUpdate){
comToUpdate.add(o);
}
}
if(comToInsert.size()>0){
CommissionsClass.CreateCommissionsObject(comToInsert);
system.debug('comToInsert List is : ' + comToInsert);
}
If(comToDelete.size()>0){
CommissionsClass.DeleteCommissionsObject(comToDelete);
system.debug('comToDelete List is : ' + comToDelete);
}
if(comToUpdate.size()>0){
CommissionsClass.UpdateCommissionsObject(comToUpdate);
System.debug('comToUpdate List is : ' + comToUpdate);
}
}
Class:
public class CommissionsClass {
public static void CreateCommissionsObject(Opportunity[] comToInsert){
List<Commissions_Chart__c> comInsert = new List<Commissions_Chart__c>();
List<Commissions__c> comToAttach = [select Id, OwnerId, Commissions_Month__c FROM Commissions__c WHERE Commissions_Month__c = THIS_MONTH];
For(Commissions__c cm : comToAttach){
For(Opportunity o : comToInsert){
If(cm.OwnerId == o.OwnerId){
Commissions_Chart__c c = new Commissions_Chart__c(Account_Name__c = o.AccountId, Close_Date__c = o.closeDate,
Opportunity_MRR__c = o.Billing_Amount_Monthly_Calcualted__c,Opportunity_Name__c = o.Id,
Opportunity_Stage__c = o.StageName, User__c = o.OwnerId, Commissions__c = cm.Id, Payment_Term__c = o.Contract_Payment_Terms__c,
Services_Amount__c = o.Professional_Service_Amount__c);
If(o.Total_Renewal_Term__c != NULL){
c.Term__c = o.Contract_Initial_Term_Months__c + o.Total_Renewal_Term__c;
}else{
c.Term__c = o.Contract_Initial_Term_Months__c;
}
comInsert.add(c);
}
}
}
System.debug('ComInsert list: ' + comInsert);
insert comInsert;
}
public static void DeleteCommissionsObject(Opportunity[] comToDelete){
List<Commissions_Chart__c> ccToDelete = new List<Commissions_Chart__c>();
List<Commissions_Chart__c> charts = [select Id, Opportunity_Name__c, User__c from Commissions_Chart__c where Opportunity_Name__c In : ComToDelete];
List<Opportunity> opps = [select ID, OwnerId from Opportunity where ID in :comToDelete];
For(Commissions_Chart__c ch : charts){
for (opportunity ops : opps){
If(ch.User__c != ops.OwnerId){
ccToDelete.add(ch);
}
}
}
Database.DeleteResult[] drList = Database.delete(ccToDelete, false);
for(Database.DeleteResult dr : drList){
if(dr.isSuccess()){
System.debug('Successfully deleted Commissions Chart with ID: ' + dr.getID());
}
else {
for (Database.Error err : dr.getErrors()){
system.debug('The following error has occurred.');
system.debug(err.getStatusCode() + ' : ' + err.getMessage());
System.debug('Commissions Chart fields that affected this error: ' + err.getFields());
}
}
}
}
public static void UpdateCommissionsObject(Opportunity[] comToUpdate){
List<Commissions_Chart__c> comUpdate = [SELECT Id, Account_Name__c, Close_Date__c, Opportunity_MRR__c, Opportunity_Name__c, Opportunity_Stage__c, User__c, Term__c
FROM Commissions_Chart__c WHERE Close_Date__c = THIS_YEAR];
List<Commissions_Chart__c>updatecom = new List<Commissions_Chart__C>();
for(Commissions_Chart__c cc:comUpdate){
For(opportunity op : comToUpdate){
If(cc.Opportunity_Name__c == op.Id){
cc.opportunity_stage__C = Op.StageName;
cc.Close_Date__c = op.CloseDate;
cc.Opportunity_MRR__C = op.Billing_Amount_Monthly_Calcualted__c;
cc.Payment_Term__c = op.Contract_Payment_Terms__c;
cc.Services_Amount__c = op.Professional_Service_Amount__c;
If(op.Total_Renewal_Term__c != NULL){
cc.Term__c = op.Contract_Initial_Term_Months__c + op.Total_Renewal_Term__c;
}else{
cc.Term__c = op.Contract_Initial_Term_Months__c;
}
updatecom.add(cc);
}
system.debug(updatecom);
}
}
update updatecom;
}
}
Test Class:
@isTest
public class CommissionsCalculatorTest {
static testMethod void CommissionsTriggerTest() {
//fetch IDs for records
Profile P = [select ID from Profile where Name = 'Standard User'];
RecordType rt = [select ID from recordtype where name ='New Business'];
//Create a user for testing
User u = new User(FirstName = 'Testing',
LastName = 'User',
email = 'testing@bizible.com',
monthly_quota__c = 5000,
username = 'testing@bizible.com',
Alias = 'tuser',
profileId = p.Id,
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US');
Insert u;
//Create account for Opportunities
Account a = new Account(Name = 'TestingCommissions',
OwnerId = u.Id,
ADR_Owner__C = u.Id);
Insert a;
//Create Commissions__C record for commissions Chart
Commissions__c c = new Commissions__c(Name = u.FirstName,
OwnerId = u.Id,
Commissions_month__c = System.today());
//Create Opportuntitiy for Trigger
List<Opportunity> Opps = new List<Opportunity>();
Opportunity o1 = new Opportunity(Name = 'TestCommissions1',
stageName = 'Qualified Discovery',
CloseDate = system.date.today(),
Amount = 12000,
AccountID = a.ID,
Contract_Payment_Terms__c = 'Monthly',
Contract_Initial_Term_Months__c = 12,
OwnerId = u.Id,
recordtypeID = rt.Id);
Test.startTest();
insert o1;
Test.stopTest();
System.assertEquals(1, [select count() from Opportunity]);
System.assertEquals(1, [select count() from Commissions_Chart__c]);
}
}
@isTest
public class CommissionsCalculatorTest {
static testMethod void CommissionsTriggerTest() {
//fetch IDs for records
Profile P = [select ID from Profile where Name = 'Standard User'];
RecordType rt = [select ID from recordtype where name ='New Business'];
//Create a user for testing
User u = new User(FirstName = 'Testing',
LastName = 'User',
email = 'testing@bizible.com',
monthly_quota__c = 5000,
username = 'testing@bizible.com',
Alias = 'tuser',
profileId = p.Id,
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US');
Insert u;
//Create account for Opportunities
Account a = new Account(Name = 'TestingCommissions',
OwnerId = u.Id,
ADR_Owner__C = u.Id);
Insert a;
//Create Commissions__C record for commissions Chart
Commissions__c c = new Commissions__c(Name = u.FirstName,
OwnerId = u.Id,
Commissions_month__c = System.today());
insert c;
//Create Opportuntitiy for Trigger
List<Opportunity> Opps = new List<Opportunity>();
Opportunity o1 = new Opportunity(Name = 'TestCommissions1',
stageName = 'Qualified Discovery',
CloseDate = system.date.today(),
Amount = 12000,
AccountID = a.ID,
Contract_Payment_Terms__c = 'Monthly',
Contract_Initial_Term_Months__c = 12,
OwnerId = u.Id,
recordtypeID = rt.Id);
Test.startTest();
insert o1;
Test.stopTest();
System.assertEquals(1, [select count() from Opportunity]);
System.assertEquals(1, [select count() from Commissions_Chart__c]);
}
}
Let us know if this will help you
All Answers
@isTest
public class CommissionsCalculatorTest {
static testMethod void CommissionsTriggerTest() {
//fetch IDs for records
Profile P = [select ID from Profile where Name = 'Standard User'];
RecordType rt = [select ID from recordtype where name ='New Business'];
//Create a user for testing
User u = new User(FirstName = 'Testing',
LastName = 'User',
email = 'testing@bizible.com',
monthly_quota__c = 5000,
username = 'testing@bizible.com',
Alias = 'tuser',
profileId = p.Id,
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US');
Insert u;
//Create account for Opportunities
Account a = new Account(Name = 'TestingCommissions',
OwnerId = u.Id,
ADR_Owner__C = u.Id);
Insert a;
//Create Commissions__C record for commissions Chart
Commissions__c c = new Commissions__c(Name = u.FirstName,
OwnerId = u.Id,
Commissions_month__c = System.today());
insert c;
//Create Opportuntitiy for Trigger
List<Opportunity> Opps = new List<Opportunity>();
Opportunity o1 = new Opportunity(Name = 'TestCommissions1',
stageName = 'Qualified Discovery',
CloseDate = system.date.today(),
Amount = 12000,
AccountID = a.ID,
Contract_Payment_Terms__c = 'Monthly',
Contract_Initial_Term_Months__c = 12,
OwnerId = u.Id,
recordtypeID = rt.Id);
Test.startTest();
insert o1;
Test.stopTest();
System.assertEquals(1, [select count() from Opportunity]);
System.assertEquals(1, [select count() from Commissions_Chart__c]);
}
}
Let us know if this will help you