You need to sign in to do that
Don't have an account?
Milan Vadhia
As I am new in salesforce, please someone help me to write a test class for this given triggers..... Thank you.
ContactHandler.apxc
How to write test class for an apex trigger?
As I am new in salesforce, please someone help me to write a test class for this given triggers..... Thank you.
ContactTrigger.apxt
trigger ContactTrigger on Contact (after insert, after update, after delete, after undelete) { // Trigger to Insert contact Name from Contact CSV field of Account if(Trigger.isInsert){ if(Trigger.isAfter){ ContactHandler insertContact = new ContactHandler(); insertContact.onAfterInsert(Trigger.new); } } // Trigger to Update contact Name from Contact CSV field of Account if(Trigger.isUpdate){ if(Trigger.isAfter){ ContactHandler updateContact = new ContactHandler(); updateContact.onAfterUpdate(Trigger.new , Trigger.oldMap); } } // Trigger to Delete contact Name from Contact CSV field of Account if(Trigger.isDelete){ if(Trigger.isAfter){ ContactHandler deleteContact = new ContactHandler(); deleteContact.onAfterDelete(Trigger.old); } } // Trigger to Undelete contact Name from Contact CSV field of Account if(Trigger.isUndelete){ if(Trigger.isAfter){ ContactHandler undeleteContact = new ContactHandler(); undeleteContact.onAfterUndelete(Trigger.new); } } } // End of Trigger ContactTrigger
ContactHandler.apxc
-----------------------------
public class ContactHandler { // Insert Trigger Handler Class public void onAfterInsert(List<Contact> lstContact){ updateContactNameOnAccount(lstContact, new Map<Id, Contact>()); } // End of onAfterInsert Class // Update Trigger Handler Class public void onAfterUpdate(List<Contact> lstContact , Map<Id, Contact> mapContactOld){ updateContactNameOnAccount(lstContact, mapContactOld); } // End of onAfterUpdate Class // Delete Trigger Handler Class public void onAfterDelete(List<Contact> lstContact){ updateContactNameOnAccount(lstContact, new Map<Id, Contact>()); } // End of onAfterDelete Class // Delete Trigger Handler Class public void onAfterUndelete (List<Contact> lstContact){ updateContactNameOnAccount(lstContact, new Map<Id, Contact>()); } // End of onAfterUndelete Class private void updateContactNameOnAccount(List<Contact> lstContactNew, Map<Id, Contact> mapContactOld){ Set<Id> setAccountId = new Set<Id>(); for(Contact objContact : lstContactNew){ if(mapContactOld.containskey(objContact.Id)) setAccountId.add(mapContactOld.get(objContact.Id).AccountId); setAccountId.add(objContact.AccountId); } List<Account> lstAccount = new List<Account>(); for(Account objAccount : [SELECT Id, (SELECT Id, Name FROM Contacts) FROM Account WHERE Id IN: setAccountId]){ List<String> lstContactName = new List<String>(); for(Contact objContact : objAccount.Contacts){ lstContactName.add(objContact.Name); } objAccount.Contact_CSV__c = String.join(lstContactName, ', '); lstAccount.add(objAccount); } update lstAccount; } } // End of ContactTriggerHandler Class
ContactTrigger.apxt : 100% Coverage
ContactHandler.apxc : 100% Coverage
All Answers
ContactTrigger.apxt : 100% Coverage
ContactHandler.apxc : 100% Coverage
trigger Deleterecordondosage on NNIO_Dosage__c (before delete) {
for (NNIO_Dosage__c dos:trigger.old ){
if (dos.NNIO_Care_Plan__c != Null )
{
string cid =dos.NNIO_Care_Plan__c;
case c =[select status from case where id =:cid];
if(c.status=='Closed')
{
dos.addError('can not delete the dosage');
}
else if(c.NNIO_OnboardedPatient__c == True)
{
dos.addError('can not delete the dosage') ;
}
}
}
}