You need to sign in to do that
Don't have an account?
Hima Bindu Moka
Help in writing the test class for Trigger
Hi ,
I'm very new to triggers and managed to write a trigger and it works in Sandbox and it's test cass is success but not covering code coverage show only 0. Unable to achieve code coverage, any suggestions to direct me to achieve my 100% code coverage.
I appreciate you help in advance!!
My trigger:
trigger parentCaseClose on case(before update)
{
set<id> openCaseIDs = new set<id>();
for(case caseList : [select id, ParentId, status from case where ParentId In:trigger.newMap.keyset() AND status != 'Closed'])
{
openCaseIDs.add(caseList.ParentId);
}
for(case newCase : trigger.new)
{
if(newCase.status != trigger.oldMap.get(newCase.id).status && newCase.status == 'Closed' && openCaseIDs.contains(newCase.id))
{
newCase.addError('The associated child cases are open');
}
}
}
My Test class:
@isTest
private class TestParentCaseCloseValidation {
public static List<Case> createCase(string caseStatus) {
// test account insert
Account a = new Account();
a.Name = 'sample account';
insert a;
// test contact insert
Contact c = new Contact();
c.FirstName = 'Test';
c.LastName = 'contact';
c.AccountId= a.Id;
insert c;
List<Case> cases = new List<Case>();
// string caseStatus = 'In Progress';
Case parent = new Case( Subject='TEST', contactID = c.id,Status=caseStatus );
insert parent;
cases.add(parent);
// system.debug('insert parent',cases.isEmpty());
Case child1 = new Case(Subject='child1', contactID = c.id, Status=caseStatus,ParentId=parent.Id);
insert child1;
cases.add(child1);
Case child2 = new Case(Subject='child2', contactID = c.id, Status=caseStatus,ParentId=parent.Id);
insert child2;
cases.add(child2);
return cases;
}
static testMethod void testCloseParent() {
List<Case> cases = createCase('In Progress');
String parentId = null;
//Update one of the children and see that the parent stays open
for (Case c:cases) {
if (c.ParentId!=null) {
parentId = c.ParentId;
c.Status = 'Closed';
}
}
update cases;
//Shouldn't be closed yet
Case parent = [select IsClosed from Case where Id=:parentId];
System.assert(parent.IsClosed==false);
for (Case c:cases) {
if (c.ParentId!=null && c.Status!='Closed') {
parentId = c.ParentId;
c.Status = 'Closed';
}
}update cases;
test.startTest();
try
{
update parent;
}
catch(System.DMLException e)
{
System.assert(e.getMessage().contains('There are still Child Cases Open '));
}
test.stopTest();
}
}
I'm very new to triggers and managed to write a trigger and it works in Sandbox and it's test cass is success but not covering code coverage show only 0. Unable to achieve code coverage, any suggestions to direct me to achieve my 100% code coverage.
I appreciate you help in advance!!
My trigger:
trigger parentCaseClose on case(before update)
{
set<id> openCaseIDs = new set<id>();
for(case caseList : [select id, ParentId, status from case where ParentId In:trigger.newMap.keyset() AND status != 'Closed'])
{
openCaseIDs.add(caseList.ParentId);
}
for(case newCase : trigger.new)
{
if(newCase.status != trigger.oldMap.get(newCase.id).status && newCase.status == 'Closed' && openCaseIDs.contains(newCase.id))
{
newCase.addError('The associated child cases are open');
}
}
}
My Test class:
@isTest
private class TestParentCaseCloseValidation {
public static List<Case> createCase(string caseStatus) {
// test account insert
Account a = new Account();
a.Name = 'sample account';
insert a;
// test contact insert
Contact c = new Contact();
c.FirstName = 'Test';
c.LastName = 'contact';
c.AccountId= a.Id;
insert c;
List<Case> cases = new List<Case>();
// string caseStatus = 'In Progress';
Case parent = new Case( Subject='TEST', contactID = c.id,Status=caseStatus );
insert parent;
cases.add(parent);
// system.debug('insert parent',cases.isEmpty());
Case child1 = new Case(Subject='child1', contactID = c.id, Status=caseStatus,ParentId=parent.Id);
insert child1;
cases.add(child1);
Case child2 = new Case(Subject='child2', contactID = c.id, Status=caseStatus,ParentId=parent.Id);
insert child2;
cases.add(child2);
return cases;
}
static testMethod void testCloseParent() {
List<Case> cases = createCase('In Progress');
String parentId = null;
//Update one of the children and see that the parent stays open
for (Case c:cases) {
if (c.ParentId!=null) {
parentId = c.ParentId;
c.Status = 'Closed';
}
}
update cases;
//Shouldn't be closed yet
Case parent = [select IsClosed from Case where Id=:parentId];
System.assert(parent.IsClosed==false);
for (Case c:cases) {
if (c.ParentId!=null && c.Status!='Closed') {
parentId = c.ParentId;
c.Status = 'Closed';
}
}update cases;
test.startTest();
try
{
update parent;
}
catch(System.DMLException e)
{
System.assert(e.getMessage().contains('There are still Child Cases Open '));
}
test.stopTest();
}
}
Please check once following code :
Hope this helps you!
Thanks
Varaprasad
For Support: varaprasad4sfdc@gmail.com
All Answers
Please let me know your requirement. I think your trigger is not working properly.
So based on that I will help you.
Sample test class code :
Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Please check once following code :
Hope this helps you!
Thanks
Varaprasad
For Support: varaprasad4sfdc@gmail.com
Thanks again! for your help but I noticed one more challege while deploying these two class (trigger with 100% code coverage and testclass 0% code coverage ) which shows error that deploy is failed saying Each trigger must have at least 1% code coverage.
Please Note: there are no Hardcoded reference Id's in the Trigger and Test Class.
I appreciate your help!