You need to sign in to do that
Don't have an account?
noor Zabi
Create a test class for contact trigger?
trigger OpportunityTrigger on Opportunity (before update, after insert, after update, after delete, after undelete) {
//trigger.oldMap --> Map<Id,Opportunity>
if(Trigger.isBefore){
for(Opportunity opp: Trigger.new) {
if(Trigger.oldMap.get(opp.Id).StageName == 'Closed Won') {
opp.addError('You cannot modified the opportunity when stage is Closed Won.');
}
} }
else{
OpportunityTriggerHandler.rollupV3();
}}
rollupV3:- Contact Trigger Handler
public static void rollup_v3(){
Set<Id> accIds = getAccIds();
List<Account> accLst = new List<Account>();
for(AggregateResult agg : [SELECT AccountId, Count(Id) FROM Contact
WHERE AccountId in: accIds AND MailingCountry != 'USA'GROUP BY AccountId]) {
Id accId = (Id)agg.get('AccountId');
Account acc = new Account(
Id= accId,
Number_of_non_USA_contact__c = (Integer)agg.get('expr0')
);
accLst.add(acc);
accIds.remove(accId);
}
//To fix the last but USA contact deleted or update to other country
if(accIds.size() > 0) {
for(Id accId : accIds) {
Account acc = new Account(
Id = accId,
Number_of_non_USA_contact__c = 0
);
accLst.add(acc);
}
}
if(accLst.size() > 0)
update accLst;
}
public static Set<Id> getAccIds(){
//trigger.new---> List<sObject>
List<Contact> triggerOld = (List<Contact>)Trigger.old;
List<Contact> triggerNew = (List<Contact>)Trigger.new;
//Collect the account ids to update
Set<Id> accIds = new Set<Id>();
for(Contact con: trigger.isDelete ? triggerOld: triggerNew){
accIds.add(con.AccountId);
}
System.debug('accIds: ' + accIds);
return accIds;
}
Test Class:
@isTest
public class ContactTriggerTest {
@isTest Public Static void myTest(){
Contact con = new Contact();
test.startTest();
Delete con; Insert con; Undelete con; Update con;
test.stopTest();
}
}
//trigger.oldMap --> Map<Id,Opportunity>
if(Trigger.isBefore){
for(Opportunity opp: Trigger.new) {
if(Trigger.oldMap.get(opp.Id).StageName == 'Closed Won') {
opp.addError('You cannot modified the opportunity when stage is Closed Won.');
}
} }
else{
OpportunityTriggerHandler.rollupV3();
}}
rollupV3:- Contact Trigger Handler
public static void rollup_v3(){
Set<Id> accIds = getAccIds();
List<Account> accLst = new List<Account>();
for(AggregateResult agg : [SELECT AccountId, Count(Id) FROM Contact
WHERE AccountId in: accIds AND MailingCountry != 'USA'GROUP BY AccountId]) {
Id accId = (Id)agg.get('AccountId');
Account acc = new Account(
Id= accId,
Number_of_non_USA_contact__c = (Integer)agg.get('expr0')
);
accLst.add(acc);
accIds.remove(accId);
}
//To fix the last but USA contact deleted or update to other country
if(accIds.size() > 0) {
for(Id accId : accIds) {
Account acc = new Account(
Id = accId,
Number_of_non_USA_contact__c = 0
);
accLst.add(acc);
}
}
if(accLst.size() > 0)
update accLst;
}
public static Set<Id> getAccIds(){
//trigger.new---> List<sObject>
List<Contact> triggerOld = (List<Contact>)Trigger.old;
List<Contact> triggerNew = (List<Contact>)Trigger.new;
//Collect the account ids to update
Set<Id> accIds = new Set<Id>();
for(Contact con: trigger.isDelete ? triggerOld: triggerNew){
accIds.add(con.AccountId);
}
System.debug('accIds: ' + accIds);
return accIds;
}
Test Class:
@isTest
public class ContactTriggerTest {
@isTest Public Static void myTest(){
Contact con = new Contact();
test.startTest();
Delete con; Insert con; Undelete con; Update con;
test.stopTest();
}
}
Hi, Noor Zabi
Please find the solution. "Create a test class for contact trigger?"
Here you are asking for a Contact trigger but you have shared the code of Opportunity Trigger.
But I have written a test class for Contact trigger only.
I want to tell you that if you are writing a trigger for any objects then no need to write "test.startTest() and test.stopTest()". Trigger automatically fire when we use DML(insert,update,delete etc) in test class;
Please let me know it is working or not??
If it helps you please mark it as Best Answer so that other people would take reference from it.
Thank You!