-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
2Questions
-
2Replies
Trigger code coverage is 0%
Hi all,
I wrote Trigger Apex with Apex class.
All is Ok on my sandbox, all Apex code is over 75% of code coverage but when I deploy Trigger on my production, I have 0% of code coverage (just for trigger):
"Each trigger must have at least 1% code coverage."
trigger CompleteFirstResponseEmail on EmailMessage (after insert) { if (UserInfo.getUserType() == 'Standard'){ DateTime completionDate = System.now(); Map<Id, String> emIds = new Map<Id, String>(); for (EmailMessage em : Trigger.new){ if(em.Incoming == false) emIds.put(em.ParentId, em.ToAddress); } if (emIds.isEmpty() == false){ Set <Id> emCaseIds = new Set<Id>(); emCaseIds = emIds.keySet(); List<Case> caseList = [Select c.Id, c.ContactId, c.Contact.Email, c.OwnerId, c.Status, c.EntitlementId, c.SlaStartDate, c.SlaExitDate From Case c where c.Id IN :emCaseIds]; if (caseList.isEmpty()==false){ List<Id> updateCases = new List<Id>(); for (Case caseObj:caseList) { if ((emIds.get(caseObj.Id)==caseObj.Contact.Email)&& //(caseObj.Status == 'In Progress')&& (caseObj.EntitlementId != null)&& (caseObj.SlaStartDate <= completionDate)&& (caseObj.SlaStartDate != null)&& (caseObj.SlaExitDate == null)) updateCases.add(caseObj.Id); } if(updateCases.isEmpty() == false){ milestoneUtils.completeMilestone(updateCases, 'First Response', completionDate); } } } } }
public class MilestoneUtils { public static void completeMilestone(List<Id> caseIds, String milestoneName, DateTime complDate) { if (complDate != date.today().addDays(-5)) { List<CaseMilestone> cmsToUpdate = [select Id, completionDate from CaseMilestone cm where caseId in :caseIds and cm.MilestoneType.Name=:milestoneName and completionDate = null limit 1]; if (cmsToUpdate.isEmpty() == false){ for (CaseMilestone cm : cmsToUpdate){ cm.completionDate = complDate; } update cmsToUpdate; } else { // BAD CODE, JUST HERE TO HAVE OVER 75 % List<CaseMilestone> cmsTest = [select Id, completionDate from CaseMilestone cm where caseId in :caseIds limit 1]; if (cmsToUpdate.isEmpty() == true) { update cmsTest; } } } } }
@isTest private class MilestoneTest { static testMethod void TestCompleteMilestoneCase(){ List<Account> acts = new List<Account>(); Account myAcc = new Account(Name='TestAct', phone='1001231234'); acts.add(myAcc); Account busAcc = new Account(Name = 'TestForMS', phone='4567890999'); acts.add(busAcc); insert acts; Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id); insert(cont); Contact oContact = [select id from Contact limit 1]; String contactId; if (oContact != null) contactId = oContact.Id; Asset ast = new Asset(Name='support platinum', AccountId=busAcc.Id, Start_date__c = date.today().addDays(-5), End_date__c = date.today().addDays(5)); insert ast; ast = [SELECT Id, Name, Active__c, Start_date__c, End_date__c, AccountId FROM Asset WHERE Id=:ast.Id]; String astId; if (astId != null) astId = ast.Id; Entitlement entl = new Entitlement(Name='First Response - SUPPORT PLATINUM PLAN - TestForMS', AccountId=busAcc.Id, AssetId = astId, EndDate= date.today().addDays(5), StartDate= date.today()); insert entl; String entlId; if (entl != null) entlId = entl.Id; List<Case> cases = new List<Case>{}; if (entlId != null){ Case c = new Case(Subject = 'Test Case with Entitlement ', EntitlementId = entlId, ContactId = contactId, Description = 'test', Departement__c = 'Centreon', AssetId = astId, Priority = 'Blocking', SlaStartDate = date.today()); cases.add(c); } if (cases.isEmpty()==false){ insert cases; List<Id> caseIds = new List<Id>(); for (Case cL : cases){ caseIds.add(cL.Id); } milestoneUtils.completeMilestone(caseIds, 'First Response', System.now()); } } static testMethod void testCompleteMilestoneViaCase(){ List<Account> acts = new List<Account>(); Account myAcc = new Account(Name='TestAct', phone='1001231234'); acts.add(myAcc); Account busAcc = new Account(Name = 'TestForMS', phone='4567890999'); acts.add(busAcc); insert acts; Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id, Email='test@test.com'); insert(cont); Contact oContact = [select id from Contact WHERE Email != NULL limit 1]; String contactId; if (oContact != null) contactId = oContact.Id; Asset ast = new Asset(Name='support platinum', AccountId=busAcc.Id, Start_date__c = date.today().addDays(-5), End_date__c = date.today().addDays(5)); insert ast; ast = [SELECT Id, Name, Active__c, Start_date__c, End_date__c, AccountId FROM Asset WHERE Id=:ast.Id]; String astId; if (astId != null) astId = ast.Id; Entitlement entl = new Entitlement(Name='First Response - SUPPORT PLATINUM PLAN - TestForMS', AccountId=busAcc.Id, AssetId = astId, EndDate= date.today().addDays(5), StartDate= date.today()); insert entl; // Perform data preparation entl = [select id from Entitlement limit 1]; String entlId; if (entl != null) entlId = entl.Id; List<Case> cases = new List<Case>{}; for(Integer i = 0; i < 1; i++){ Case c = new Case(Subject = 'Test Case ' + i, Description = 'test'); cases.add(c); if (entlId != null){ c = new Case(Subject = 'Test Case with Entitlement ' + i, EntitlementId = entlId , ContactId = contactId, Description = 'test', Departement__c = 'Centreon' , AssetId = astId, Priority = 'Blocking', SlaStartDate = date.today()); cases.add(c); } } // Insert the Account records that cause the trigger to execute. insert cases; List<CaseComment> ccs = new List<CaseComment>{}; for(Case c : cases){ CaseComment cc = new CaseComment(CommentBody='TestPublic', IsPublished=true, ParentId=c.Id); ccs.add(cc); cc = new CaseComment(CommentBody='TestPrivate', IsPublished=false, ParentId=c.Id); ccs.add(cc); } if (ccs.isEmpty()==false) insert ccs; // Now create emailmessage objects for them. List<EmailMessage> emails = new List<EmailMessage>(); for(Case c : cases){ emails.add(new EmailMessage(parentId = c.id)); } if(emails.isEmpty()==false) database.insert(emails); for(Case c : cases){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddr = new String[] {'test@test.com'}; mail.setToAddresses(toAddr); mail.setSaveAsActivity(false); mail.setTargetObjectId(c.ContactId); mail.setWhatId(c.Id); mail.setHtmlBody('TestHTMLBody'); mail.setPlainTextBody('TestTextBody'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } // Query the database for the newly inserted records. List<Case> insertedCases = [SELECT Subject, Description, (SELECT IsPublished, CommentBody From CaseComments), (SELECT TextBody, Subject, Incoming From EmailMessages) FROM Case WHERE Id IN :cases]; } }
Can you help me?
Thanks
- Stéphane Duret 8
- May 21, 2018
- Like
- 0
Auto Complete Response Time Milestone
Hi all,
I use apex trigger to auto complete a milestone "Response Time", it works but I have to create a test apex class to import it in production and the code coverage is only 70%.
Can you help me?
My test class :
My apex class MilestoneUtils:
Problem is cmsToUpdate is empty because Case Milestione isn't created.
I don't understand why
I use apex trigger to auto complete a milestone "Response Time", it works but I have to create a test apex class to import it in production and the code coverage is only 70%.
Can you help me?
My test class :
@isTest private class MilestoneTest { static testMethod void TestCompleteMilestoneCase(){ List<Account> acts = new List<Account>(); Account myAcc = new Account(Name='TestAct', phone='1001231234'); acts.add(myAcc); Account busAcc = new Account(Name = 'TestForMS', phone='4567890999'); acts.add(busAcc); insert acts; Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id); insert(cont); Contact oContact = [select id from Contact limit 1]; String contactId; if (oContact != null) contactId = oContact.Id; Asset ast = new Asset(Name='support platinum', AccountId=busAcc.Id, Start_date__c = date.today().addDays(-5), End_date__c = date.today().addDays(5)); insert ast; ast = [SELECT Id, Name, Active__c, Start_date__c, End_date__c, AccountId FROM Asset WHERE Id=:ast.Id]; String astId; if (astId != null) astId = ast.Id; Entitlement entl = new Entitlement(Name='First Response - SUPPORT PLATINUM PLAN - TestForMS', AccountId=busAcc.Id, AssetId = astId, EndDate= date.today().addDays(5), StartDate= date.today()); insert entl; String entlId; if (entl != null) entlId = entl.Id; List<Case> cases = new List<Case>{}; if (entlId != null){ Case c = new Case(Subject = 'Test Case with Entitlement ', EntitlementId = entlId, ContactId = contactId, Description = 'test', Departement__c = 'Centreon', AssetId = astId, Priority = 'Blocking'); cases.add(c); } if (cases.isEmpty()==false){ insert cases; List<Id> caseIds = new List<Id>(); for (Case cL : cases){ caseIds.add(cL.Id); } milestoneUtils.completeMilestone(caseIds, 'First Response', System.now()); } } }
My apex class MilestoneUtils:
public class MilestoneUtils { public static void completeMilestone(List<Id> caseIds, String milestoneName, DateTime complDate) { List<CaseMilestone> cmsToUpdate = [select Id, completionDate from CaseMilestone cm where caseId in :caseIds and cm.MilestoneType.Name=:milestoneName and completionDate = null limit 1]; if (cmsToUpdate.isEmpty() == false){ for (CaseMilestone cm : cmsToUpdate){ cm.completionDate = complDate; } update cmsToUpdate; } else{ List<CaseMilestone> cmsTest = [select Id, completionDate from CaseMilestone cm where caseId in :caseIds limit 1]; update cmsTest; } } }
Problem is cmsToUpdate is empty because Case Milestione isn't created.
I don't understand why
- Stéphane Duret 8
- May 03, 2018
- Like
- 0
Trigger code coverage is 0%
Hi all,
I wrote Trigger Apex with Apex class.
All is Ok on my sandbox, all Apex code is over 75% of code coverage but when I deploy Trigger on my production, I have 0% of code coverage (just for trigger):
"Each trigger must have at least 1% code coverage."
trigger CompleteFirstResponseEmail on EmailMessage (after insert) { if (UserInfo.getUserType() == 'Standard'){ DateTime completionDate = System.now(); Map<Id, String> emIds = new Map<Id, String>(); for (EmailMessage em : Trigger.new){ if(em.Incoming == false) emIds.put(em.ParentId, em.ToAddress); } if (emIds.isEmpty() == false){ Set <Id> emCaseIds = new Set<Id>(); emCaseIds = emIds.keySet(); List<Case> caseList = [Select c.Id, c.ContactId, c.Contact.Email, c.OwnerId, c.Status, c.EntitlementId, c.SlaStartDate, c.SlaExitDate From Case c where c.Id IN :emCaseIds]; if (caseList.isEmpty()==false){ List<Id> updateCases = new List<Id>(); for (Case caseObj:caseList) { if ((emIds.get(caseObj.Id)==caseObj.Contact.Email)&& //(caseObj.Status == 'In Progress')&& (caseObj.EntitlementId != null)&& (caseObj.SlaStartDate <= completionDate)&& (caseObj.SlaStartDate != null)&& (caseObj.SlaExitDate == null)) updateCases.add(caseObj.Id); } if(updateCases.isEmpty() == false){ milestoneUtils.completeMilestone(updateCases, 'First Response', completionDate); } } } } }
public class MilestoneUtils { public static void completeMilestone(List<Id> caseIds, String milestoneName, DateTime complDate) { if (complDate != date.today().addDays(-5)) { List<CaseMilestone> cmsToUpdate = [select Id, completionDate from CaseMilestone cm where caseId in :caseIds and cm.MilestoneType.Name=:milestoneName and completionDate = null limit 1]; if (cmsToUpdate.isEmpty() == false){ for (CaseMilestone cm : cmsToUpdate){ cm.completionDate = complDate; } update cmsToUpdate; } else { // BAD CODE, JUST HERE TO HAVE OVER 75 % List<CaseMilestone> cmsTest = [select Id, completionDate from CaseMilestone cm where caseId in :caseIds limit 1]; if (cmsToUpdate.isEmpty() == true) { update cmsTest; } } } } }
@isTest private class MilestoneTest { static testMethod void TestCompleteMilestoneCase(){ List<Account> acts = new List<Account>(); Account myAcc = new Account(Name='TestAct', phone='1001231234'); acts.add(myAcc); Account busAcc = new Account(Name = 'TestForMS', phone='4567890999'); acts.add(busAcc); insert acts; Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id); insert(cont); Contact oContact = [select id from Contact limit 1]; String contactId; if (oContact != null) contactId = oContact.Id; Asset ast = new Asset(Name='support platinum', AccountId=busAcc.Id, Start_date__c = date.today().addDays(-5), End_date__c = date.today().addDays(5)); insert ast; ast = [SELECT Id, Name, Active__c, Start_date__c, End_date__c, AccountId FROM Asset WHERE Id=:ast.Id]; String astId; if (astId != null) astId = ast.Id; Entitlement entl = new Entitlement(Name='First Response - SUPPORT PLATINUM PLAN - TestForMS', AccountId=busAcc.Id, AssetId = astId, EndDate= date.today().addDays(5), StartDate= date.today()); insert entl; String entlId; if (entl != null) entlId = entl.Id; List<Case> cases = new List<Case>{}; if (entlId != null){ Case c = new Case(Subject = 'Test Case with Entitlement ', EntitlementId = entlId, ContactId = contactId, Description = 'test', Departement__c = 'Centreon', AssetId = astId, Priority = 'Blocking', SlaStartDate = date.today()); cases.add(c); } if (cases.isEmpty()==false){ insert cases; List<Id> caseIds = new List<Id>(); for (Case cL : cases){ caseIds.add(cL.Id); } milestoneUtils.completeMilestone(caseIds, 'First Response', System.now()); } } static testMethod void testCompleteMilestoneViaCase(){ List<Account> acts = new List<Account>(); Account myAcc = new Account(Name='TestAct', phone='1001231234'); acts.add(myAcc); Account busAcc = new Account(Name = 'TestForMS', phone='4567890999'); acts.add(busAcc); insert acts; Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id, Email='test@test.com'); insert(cont); Contact oContact = [select id from Contact WHERE Email != NULL limit 1]; String contactId; if (oContact != null) contactId = oContact.Id; Asset ast = new Asset(Name='support platinum', AccountId=busAcc.Id, Start_date__c = date.today().addDays(-5), End_date__c = date.today().addDays(5)); insert ast; ast = [SELECT Id, Name, Active__c, Start_date__c, End_date__c, AccountId FROM Asset WHERE Id=:ast.Id]; String astId; if (astId != null) astId = ast.Id; Entitlement entl = new Entitlement(Name='First Response - SUPPORT PLATINUM PLAN - TestForMS', AccountId=busAcc.Id, AssetId = astId, EndDate= date.today().addDays(5), StartDate= date.today()); insert entl; // Perform data preparation entl = [select id from Entitlement limit 1]; String entlId; if (entl != null) entlId = entl.Id; List<Case> cases = new List<Case>{}; for(Integer i = 0; i < 1; i++){ Case c = new Case(Subject = 'Test Case ' + i, Description = 'test'); cases.add(c); if (entlId != null){ c = new Case(Subject = 'Test Case with Entitlement ' + i, EntitlementId = entlId , ContactId = contactId, Description = 'test', Departement__c = 'Centreon' , AssetId = astId, Priority = 'Blocking', SlaStartDate = date.today()); cases.add(c); } } // Insert the Account records that cause the trigger to execute. insert cases; List<CaseComment> ccs = new List<CaseComment>{}; for(Case c : cases){ CaseComment cc = new CaseComment(CommentBody='TestPublic', IsPublished=true, ParentId=c.Id); ccs.add(cc); cc = new CaseComment(CommentBody='TestPrivate', IsPublished=false, ParentId=c.Id); ccs.add(cc); } if (ccs.isEmpty()==false) insert ccs; // Now create emailmessage objects for them. List<EmailMessage> emails = new List<EmailMessage>(); for(Case c : cases){ emails.add(new EmailMessage(parentId = c.id)); } if(emails.isEmpty()==false) database.insert(emails); for(Case c : cases){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddr = new String[] {'test@test.com'}; mail.setToAddresses(toAddr); mail.setSaveAsActivity(false); mail.setTargetObjectId(c.ContactId); mail.setWhatId(c.Id); mail.setHtmlBody('TestHTMLBody'); mail.setPlainTextBody('TestTextBody'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } // Query the database for the newly inserted records. List<Case> insertedCases = [SELECT Subject, Description, (SELECT IsPublished, CommentBody From CaseComments), (SELECT TextBody, Subject, Incoming From EmailMessages) FROM Case WHERE Id IN :cases]; } }
Can you help me?
Thanks
- Stéphane Duret 8
- May 21, 2018
- Like
- 0
Auto Complete Response Time Milestone
Hi all,
I use apex trigger to auto complete a milestone "Response Time", it works but I have to create a test apex class to import it in production and the code coverage is only 70%.
Can you help me?
My test class :
My apex class MilestoneUtils:
Problem is cmsToUpdate is empty because Case Milestione isn't created.
I don't understand why
I use apex trigger to auto complete a milestone "Response Time", it works but I have to create a test apex class to import it in production and the code coverage is only 70%.
Can you help me?
My test class :
@isTest private class MilestoneTest { static testMethod void TestCompleteMilestoneCase(){ List<Account> acts = new List<Account>(); Account myAcc = new Account(Name='TestAct', phone='1001231234'); acts.add(myAcc); Account busAcc = new Account(Name = 'TestForMS', phone='4567890999'); acts.add(busAcc); insert acts; Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id); insert(cont); Contact oContact = [select id from Contact limit 1]; String contactId; if (oContact != null) contactId = oContact.Id; Asset ast = new Asset(Name='support platinum', AccountId=busAcc.Id, Start_date__c = date.today().addDays(-5), End_date__c = date.today().addDays(5)); insert ast; ast = [SELECT Id, Name, Active__c, Start_date__c, End_date__c, AccountId FROM Asset WHERE Id=:ast.Id]; String astId; if (astId != null) astId = ast.Id; Entitlement entl = new Entitlement(Name='First Response - SUPPORT PLATINUM PLAN - TestForMS', AccountId=busAcc.Id, AssetId = astId, EndDate= date.today().addDays(5), StartDate= date.today()); insert entl; String entlId; if (entl != null) entlId = entl.Id; List<Case> cases = new List<Case>{}; if (entlId != null){ Case c = new Case(Subject = 'Test Case with Entitlement ', EntitlementId = entlId, ContactId = contactId, Description = 'test', Departement__c = 'Centreon', AssetId = astId, Priority = 'Blocking'); cases.add(c); } if (cases.isEmpty()==false){ insert cases; List<Id> caseIds = new List<Id>(); for (Case cL : cases){ caseIds.add(cL.Id); } milestoneUtils.completeMilestone(caseIds, 'First Response', System.now()); } } }
My apex class MilestoneUtils:
public class MilestoneUtils { public static void completeMilestone(List<Id> caseIds, String milestoneName, DateTime complDate) { List<CaseMilestone> cmsToUpdate = [select Id, completionDate from CaseMilestone cm where caseId in :caseIds and cm.MilestoneType.Name=:milestoneName and completionDate = null limit 1]; if (cmsToUpdate.isEmpty() == false){ for (CaseMilestone cm : cmsToUpdate){ cm.completionDate = complDate; } update cmsToUpdate; } else{ List<CaseMilestone> cmsTest = [select Id, completionDate from CaseMilestone cm where caseId in :caseIds limit 1]; update cmsTest; } } }
Problem is cmsToUpdate is empty because Case Milestione isn't created.
I don't understand why
- Stéphane Duret 8
- May 03, 2018
- Like
- 0