You need to sign in to do that
Don't have an account?
salesforce instance
not covering test class coverage
Trigger
___________
Trigger RecordPSOwnerAndMainContact on Case (before insert,after Insert)
{
public List<Case> lscase=Trigger.New;
public List<ID> acntsIds=new List<ID>();
public Set<ID> conIds=new Set<ID>();
Profile CurrentUserprofile = [select Name from Profile where Id = :UserInfo.getProfileId() LIMIT 1 ];
if(CurrentUserprofile.Name ==System.Label.U4_PS_Consultant)
{
if(Trigger.isBefore && Trigger.isInsert)
{
for(Case c : lscase)
{
acntsIds.add(c.AccountID);
}
List<Contact> ls = new List<Contact>([SELECT id, Name, Email from Contact where AccountId IN : acntsIds]);
for(Contact c : ls)
{
conIds.add(c.Id);
}
for (case CaseX: lscase)
{
if(!conIds.contains(CaseX.ContactID))
{
CaseX.addError('Please select valid Contact using Lookup Search Icon');
}
}
}
if(Trigger.isAfter && Trigger.isInsert)
{
List<Case> CasesToInsert = new List<Case>();
for (case CaseX: trigger.New)
{
Case NewCase = new Case(Id = CaseX.Id);
List<User> usrList = new List<User>([SELECT id, Formula_Profiel_name__c from user where Id = :UserInfo.getuserId() LIMIT 1]);
List<User> CntList = new List<user>([SELECT id, Name, Email, UserName from User where ContactId = :CaseX.ContactID LIMIT 1]);
for (User usr : usrList)
{
if(Usr.Formula_Profiel_name__c == System.Label.U4_PS_Consultant) {
NewCase.PS_Owner__c = UserInfo.getuserId();
Update NewCase;
List<CaseTeam__c> CaseTeamMember = new List<CaseTeam__c>();
CaseTeam__c CaseTeam = new CaseTeam__c ();
CaseTeam.case__c = CaseX.id;
CaseTeam.Type_of_User__c = 'PS User';
CaseTeam.MemberRole__c = 'Unit4 PS Consultant';
CaseTeam.User__c = UserInfo.getuserId();
CaseTeamMember.add(CaseTeam);
insert CaseTeamMember;
For (User Cntxt : CntList)
{
List<CaseTeam__c> CaseContactTeamMember = new List<CaseTeam__c>();
CaseTeam__c ContactMember = new CaseTeam__c (); //** New member
ContactMember.case__c = CaseX.id;
ContactMember.Type_of_User__c = 'Customer User';
ContactMember.MemberRole__c = 'Main Contact';
ContactMember.Contact__C = CaseX.ContactID;
ContactMember.User__c = Cntxt.id;
CaseContactTeamMember .add(ContactMember);
insert CaseContactTeamMember;
}
}
Else
{
For (User Cntxt : CntList)
{
List<CaseTeam__c> CaseContactTeamMember = new List<CaseTeam__c>();
CaseTeam__c ContactMember = new CaseTeam__c (); //** New member
ContactMember.case__c = CaseX.id;
ContactMember.Type_of_User__c = 'Customer User';
ContactMember.MemberRole__c = 'Main Contact';
ContactMember.Contact__C = CaseX.ContactID;
ContactMember.User__c = Cntxt.id;
CaseContactTeamMember .add(ContactMember);
insert CaseContactTeamMember;
}
NewCase.PS_Owner__c = NULL;
Update NewCase;
}
}
}
}
}
}
test class
-------------------------------------
private static testMethod void SECaseTriggerHandlerTest1(){
Case objCase1 = [select id from Case where Status ='New'];
test.startTest();
SEConstants.Case_Beforeupdate= true;
objCase1.OwnerId = UserInfo.getUserId();
update objCase1;
System.assertEquals(UserInfo.getUserId(),objCase1.OwnerId);
SEConstants.Case_Beforeupdate= true;
SEConstants.Case_Afterupdate= true;
objCase1.Status = SEConstants.CASE_STATUS_OWNED;
DateTime currentDate = System.now();
DateTime newDate = currentDate.addMinutes(-20);
objCase1.DeadLine__c = newDate;
update objCase1;
System.assertEquals(newDate,objCase1.DeadLine__c);
Notification_Table__c objNotificationTable = [select id from Notification_Table__c where Notification_Status__c = 'Scheduled' ];
objNotificationTable.Case__c = objCase1.id;
update objNotificationTable;
System.assertEquals(objCase1.id,objNotificationTable.Case__c);
SEConstants.Case_Beforeupdate= true;
SEConstants.Case_Afterupdate= true;
objCase1.Status = SEConstants.CASE_STATUS_PENDING_EXTERNAL_INFO;
update objCase1;
System.assertEquals(SEConstants.CASE_STATUS_PENDING_EXTERNAL_INFO,objCase1.Status);
SEConstants.Case_Afterupdate= true;
SEConstants.Case_Beforeupdate= true;
objCase1.Status = SEConstants.CASE_INPROGRESS;
update objCase1;
System.assertEquals( SEConstants.CASE_INPROGRESS, objCase1.Status);
Notification_Master__c objNotificationMaster2 = [select id from Notification_Master__c where No_of_Days__c = 6];
objNotificationMaster2.Case_Status__c = 'Completed';
update objNotificationMaster2;
test.stopTest();
}
private static testMethod void RecordPsmaincontacttest(){
test.startTest();
Case objCase1 = [select id from Case where Status ='New'];
SEConstants.Case_Beforeupdate= true;
objCase1.OwnerId = UserInfo.getUserId();
update objCase1;
System.assertEquals(UserInfo.getUserId(),objCase1.OwnerId);
SEConstants.Case_Afterupdate= true;
SEConstants.Case_Beforeupdate= true;
objCase1.Status = SEConstants.CASE_STATUS_OWNED;
objCase1.isNewPublicComment__c = true;
update objCase1;
System.assertEquals( true, objCase1.isNewPublicComment__c);
SEConstants.Case_Afterupdate= true;
SEConstants.Case_Beforeupdate= true;
objCase1.Status = SEConstants.CASE_INPROGRESS;
objCase1.isNeweMailadded__c = true;
objCase1.isNewPublicComment__c = false;
update objCase1;
System.assertEquals( SEConstants.CASE_INPROGRESS, objCase1.Status);
SEConstants.Case_Beforeupdate= true;
SEConstants.Case_Afterupdate= true;
objCase1.Status = 'Closed';
objCase1.CloseCase__c=true;
update objCase1;
System.assertEquals('Closed', objCase1.Status);
test.stopTest();
}
public static testMethod void SECaseTriggerHandlerTest3() {
Contact SampleContact = [select id from Contact where Lastname = 'testContact1'];
Profile objProfile = TestUtility.getProfileByProfileName(SEConstants.PROFILE_PRIMARY_SUPPORT_CONTACT);
User objUser = TestUtility.createUser(objProfile , SampleContact.id);
objUser = (User)TestUtility.createAndValidateSObject(objUser);
CaseTeam__c objCaseTeam = [select id,MemberRole__c,User__c,Case__c from CaseTeam__c where MemberRole__c =: SEConstants.MAIN_CONTACT];
objCaseTeam.User__c = objUser.id;
objCaseTeam.Contact__c= SampleContact.id ;
update objCaseTeam;
CaseTeam__c objCaseTeamTest = [select id,User__c from CaseTeam__c where User__c =: objUser.id];
System.assertEquals(objUser.id , objCaseTeamTest.User__c);
System.runAs(objUser){
test.startTest();
caseTeamEditController objCaseTeamEditController = new caseTeamEditController(new ApexPages.StandardController(objCaseTeam));
objCaseTeamEditController.init();
List<SelectOption> ListOfOptions = objCaseTeamEditController.getSelectedoptions();
System.assertEquals(4,ListOfOptions.size());
objCaseTeamEditController.save();
objCaseTeamEditController.cancel();
test.stopTest();
}
}
}
___________
Trigger RecordPSOwnerAndMainContact on Case (before insert,after Insert)
{
public List<Case> lscase=Trigger.New;
public List<ID> acntsIds=new List<ID>();
public Set<ID> conIds=new Set<ID>();
Profile CurrentUserprofile = [select Name from Profile where Id = :UserInfo.getProfileId() LIMIT 1 ];
if(CurrentUserprofile.Name ==System.Label.U4_PS_Consultant)
{
if(Trigger.isBefore && Trigger.isInsert)
{
for(Case c : lscase)
{
acntsIds.add(c.AccountID);
}
List<Contact> ls = new List<Contact>([SELECT id, Name, Email from Contact where AccountId IN : acntsIds]);
for(Contact c : ls)
{
conIds.add(c.Id);
}
for (case CaseX: lscase)
{
if(!conIds.contains(CaseX.ContactID))
{
CaseX.addError('Please select valid Contact using Lookup Search Icon');
}
}
}
if(Trigger.isAfter && Trigger.isInsert)
{
List<Case> CasesToInsert = new List<Case>();
for (case CaseX: trigger.New)
{
Case NewCase = new Case(Id = CaseX.Id);
List<User> usrList = new List<User>([SELECT id, Formula_Profiel_name__c from user where Id = :UserInfo.getuserId() LIMIT 1]);
List<User> CntList = new List<user>([SELECT id, Name, Email, UserName from User where ContactId = :CaseX.ContactID LIMIT 1]);
for (User usr : usrList)
{
if(Usr.Formula_Profiel_name__c == System.Label.U4_PS_Consultant) {
NewCase.PS_Owner__c = UserInfo.getuserId();
Update NewCase;
List<CaseTeam__c> CaseTeamMember = new List<CaseTeam__c>();
CaseTeam__c CaseTeam = new CaseTeam__c ();
CaseTeam.case__c = CaseX.id;
CaseTeam.Type_of_User__c = 'PS User';
CaseTeam.MemberRole__c = 'Unit4 PS Consultant';
CaseTeam.User__c = UserInfo.getuserId();
CaseTeamMember.add(CaseTeam);
insert CaseTeamMember;
For (User Cntxt : CntList)
{
List<CaseTeam__c> CaseContactTeamMember = new List<CaseTeam__c>();
CaseTeam__c ContactMember = new CaseTeam__c (); //** New member
ContactMember.case__c = CaseX.id;
ContactMember.Type_of_User__c = 'Customer User';
ContactMember.MemberRole__c = 'Main Contact';
ContactMember.Contact__C = CaseX.ContactID;
ContactMember.User__c = Cntxt.id;
CaseContactTeamMember .add(ContactMember);
insert CaseContactTeamMember;
}
}
Else
{
For (User Cntxt : CntList)
{
List<CaseTeam__c> CaseContactTeamMember = new List<CaseTeam__c>();
CaseTeam__c ContactMember = new CaseTeam__c (); //** New member
ContactMember.case__c = CaseX.id;
ContactMember.Type_of_User__c = 'Customer User';
ContactMember.MemberRole__c = 'Main Contact';
ContactMember.Contact__C = CaseX.ContactID;
ContactMember.User__c = Cntxt.id;
CaseContactTeamMember .add(ContactMember);
insert CaseContactTeamMember;
}
NewCase.PS_Owner__c = NULL;
Update NewCase;
}
}
}
}
}
}
test class
-------------------------------------
private static testMethod void SECaseTriggerHandlerTest1(){
Case objCase1 = [select id from Case where Status ='New'];
test.startTest();
SEConstants.Case_Beforeupdate= true;
objCase1.OwnerId = UserInfo.getUserId();
update objCase1;
System.assertEquals(UserInfo.getUserId(),objCase1.OwnerId);
SEConstants.Case_Beforeupdate= true;
SEConstants.Case_Afterupdate= true;
objCase1.Status = SEConstants.CASE_STATUS_OWNED;
DateTime currentDate = System.now();
DateTime newDate = currentDate.addMinutes(-20);
objCase1.DeadLine__c = newDate;
update objCase1;
System.assertEquals(newDate,objCase1.DeadLine__c);
Notification_Table__c objNotificationTable = [select id from Notification_Table__c where Notification_Status__c = 'Scheduled' ];
objNotificationTable.Case__c = objCase1.id;
update objNotificationTable;
System.assertEquals(objCase1.id,objNotificationTable.Case__c);
SEConstants.Case_Beforeupdate= true;
SEConstants.Case_Afterupdate= true;
objCase1.Status = SEConstants.CASE_STATUS_PENDING_EXTERNAL_INFO;
update objCase1;
System.assertEquals(SEConstants.CASE_STATUS_PENDING_EXTERNAL_INFO,objCase1.Status);
SEConstants.Case_Afterupdate= true;
SEConstants.Case_Beforeupdate= true;
objCase1.Status = SEConstants.CASE_INPROGRESS;
update objCase1;
System.assertEquals( SEConstants.CASE_INPROGRESS, objCase1.Status);
Notification_Master__c objNotificationMaster2 = [select id from Notification_Master__c where No_of_Days__c = 6];
objNotificationMaster2.Case_Status__c = 'Completed';
update objNotificationMaster2;
test.stopTest();
}
private static testMethod void RecordPsmaincontacttest(){
test.startTest();
Case objCase1 = [select id from Case where Status ='New'];
SEConstants.Case_Beforeupdate= true;
objCase1.OwnerId = UserInfo.getUserId();
update objCase1;
System.assertEquals(UserInfo.getUserId(),objCase1.OwnerId);
SEConstants.Case_Afterupdate= true;
SEConstants.Case_Beforeupdate= true;
objCase1.Status = SEConstants.CASE_STATUS_OWNED;
objCase1.isNewPublicComment__c = true;
update objCase1;
System.assertEquals( true, objCase1.isNewPublicComment__c);
SEConstants.Case_Afterupdate= true;
SEConstants.Case_Beforeupdate= true;
objCase1.Status = SEConstants.CASE_INPROGRESS;
objCase1.isNeweMailadded__c = true;
objCase1.isNewPublicComment__c = false;
update objCase1;
System.assertEquals( SEConstants.CASE_INPROGRESS, objCase1.Status);
SEConstants.Case_Beforeupdate= true;
SEConstants.Case_Afterupdate= true;
objCase1.Status = 'Closed';
objCase1.CloseCase__c=true;
update objCase1;
System.assertEquals('Closed', objCase1.Status);
test.stopTest();
}
public static testMethod void SECaseTriggerHandlerTest3() {
Contact SampleContact = [select id from Contact where Lastname = 'testContact1'];
Profile objProfile = TestUtility.getProfileByProfileName(SEConstants.PROFILE_PRIMARY_SUPPORT_CONTACT);
User objUser = TestUtility.createUser(objProfile , SampleContact.id);
objUser = (User)TestUtility.createAndValidateSObject(objUser);
CaseTeam__c objCaseTeam = [select id,MemberRole__c,User__c,Case__c from CaseTeam__c where MemberRole__c =: SEConstants.MAIN_CONTACT];
objCaseTeam.User__c = objUser.id;
objCaseTeam.Contact__c= SampleContact.id ;
update objCaseTeam;
CaseTeam__c objCaseTeamTest = [select id,User__c from CaseTeam__c where User__c =: objUser.id];
System.assertEquals(objUser.id , objCaseTeamTest.User__c);
System.runAs(objUser){
test.startTest();
caseTeamEditController objCaseTeamEditController = new caseTeamEditController(new ApexPages.StandardController(objCaseTeam));
objCaseTeamEditController.init();
List<SelectOption> ListOfOptions = objCaseTeamEditController.getSelectedoptions();
System.assertEquals(4,ListOfOptions.size());
objCaseTeamEditController.save();
objCaseTeamEditController.cancel();
test.stopTest();
}
}
}
To cover test classes first we need to insert test data in the test class.
Please check once below sample test class.
http://salesforceprasad.blogspot.sg/2017/10/salesforce-test-class-template.html
Hope it helps you.
Thanks
Varaprasad