title: Salesforce Trigger - Prevent Parent Case Closure if Parent Case has open Child Cases
body: <p>I have the following Apex trigger which should prevent a Parent Case from closing if the parent case has open child cases.  Kindly assist with trouble shooting as the Apex Trigger is not firing.</p>
  <pre><code>trigger CaseTriggerCloseChild on Case ( before update ) {
      
      Set &lt; Id &gt; setCaseIds = new Set &lt; Id &gt;();
      Map &lt; Id, Integer &gt; mapOpenCaseCount = new Map &lt; Id, Integer &gt;();
      
      for ( Case objCase : trigger.new ) {
          
          if ( objCase.Status == 'Closed' ) {
              setCaseIds.add( objCase.Id );
          }
      }
      
      for ( Case objCase : [ SELECT ParentId FROM Case WHERE ParentId IN: setCaseIds AND IsClosed = false ] ) {
      
          if ( mapOpenCaseCount.containsKey( objCase.ParentId ) ) {
          
              mapOpenCaseCount.put( objCase.ParentId, mapOpenCaseCount.get( objCase.ParentId ) + 1 );
          
          } else {
              mapOpenCaseCount.put( objCase.ParentId, 1 );
          }
      }    
      
      
      for ( Case objCase : trigger.new ) {
      
          if ( objCase.Status == 'Closed' ) {
          
              if ( mapOpenCaseCount.containsKey( objCase.Id ) ) {
          
                  objCase.addError( 'You cannot close this Case. It has ' + mapOpenCaseCount.get( objCase.Id ) + ' open Child Cases.' );
              }
          }
      }
  }
  </code></pre>