• Koustubh Masurkar
  • 20 Points
  • Member since 2019

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 2
  • 0
I have written a trigger to count open cases of contacts. I wrote a test class for the trigger, but the code coverage remains 0%. What am I doing wrong here? Any help is appriciated.

This is my trigger:
trigger CountOpenCases on case (after insert, after update) { 	//Event after insert and after update
  List<Case> caseList = trigger.new;							//Assign cases to a list
  set<Id> contactIdSet = new set<Id>();							//Create set to store contact id
  for(Case cs: caseList ){										//Iterate over cases
     contactIdSet.add(cs.contactId);							//Add contact id from cases to set
  if(contactIdSet.size() > 0){									//If set has elements
    List<contact> lstcontact = [select id, (select id from Cases where status!= 'Closed') from contact where id IN: contactIdSet ];	//Create list and query closed cases id and account id
       if(lstcontact.size() > 0){								//If query returns results
          for(contact acc: lstcontact)							//Iterate over result list
             acc.Open_Cases__c = acc.cases.size(); 				//Assign size of list to variable which counts open cases
          update lstcontact;									//Update the contact

This is my test class:
public class CountOpenCasesTestClass {
    static testmethod void myTest(){
        Account acc = new Account(Name = 'TestAccount1');
        insert acc;
        Id accid = acc.Id; //get account id
        Contact con = new Contact();
        con.LastName = 'TestContact1';
        con.AccountId = accid; //insert account id
        insert con;
        Case c = new Case(subject='Case of Test Class');
        Id cid = c.Id; //get case id
        c.Status = 'New';
        c.ContactId = cid; //insert contact id
        insert c;

I wrote a trigger to count number of open cases for contacts. 
To reduce complexity and obey governor limits, how can I use Map instead of inner for loop in this case? 
trigger CountOpenCases on case (after insert, after update) {
  List<Case> caseList = trigger.new;
  set<Id> contactIdSet = new set<Id>();
  for(Case cs: caseList ){
     contactIdSet .add(cs.contactId);
  if(contactIdSet.size() > 0){
    List<contact> lstcontact = [select id, (select id, status from Cases)from contact where id IN: contactIdSet ];
       if(lstcontact.size() > 0){
          for(contact acc: lstcontact)
             Integer openCases = 0;
             for(Case c : acc.cases){
                 if(c.Status != 'Closed')
             acc.Open_Cases__c = openCases; 
          update lstcontact;

I created a trigger on case. Member_ID__c is a auto-number field on contact object. I am trying to find out repeat cases logged by a contact using custom checkbox Repeat_Caller__c on case. There are many other conditions, but I removed them in this code for simplicity. I am not able to match Member_ID__c field in cases even though multiple cases are created by the same contact. In other words, I am not able to enter the IF condition. I have pasted the trigger below.

Please guide me as to where the problem might be. Any help is appreciated.

trigger repeatCase1 on Case (before insert,before update) {
    List<Case> caseList = trigger.new;   
    Set<id> memberIdSet = new set<id>();
    for(case c: caseList){
      List<Case> allcase = [select Contact.Member_ID__c from case where contact.Member_ID__c IN: memberIdSet];               
      for(Case cs: caseList ){  
        for(Case all: allcase ){ 
            if(cs.contact.Member_ID__c == all.contact.Member_ID__c ){