function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prerna Raj 7Prerna Raj 7 

my trigger unable to create a child case when case has sensitive information

I am learning on plural site from David Liu's course he gave this code to create a child case when ever the parent has sensitive information. the code worked for him but when i tried creating a case with the sensitive information in salesforce, the child case wasn't created.

pasting my code below...
trigger CheckSecretInformation on Case (after insert, before update) {
        
    String childcaseSubject = 'Warning: Parent may contain Secret Info';
    
    //Create a collection of each of our secret keywords
    Set<String> Keywords = new Set<string>();
            keywords.add('SSN');
            Keywords.add('Social Seccurity');
            keywords.add('Credit Card');
            keywords.add('Passport');
            Keywords.add('BodyWeight');
             
     
    //Checking if the case description contains any of the secret keywords
    List<Case> casesWithKeywords = new List<Case>();
    for (case myCase : Trigger.New){
        if (mycase.Subject != childcaseSubject){    
        for (string keyword : keywords){
            if (myCase.Description != null && myCase.Description.containsIgnoreCase(keyword)){
                casesWithKeywords.add(myCase);
               system.debug('Case' + myCase.Id + 'include keyword' + keyword);
                break;
              }
            
            }
               
        }
    }  
    
        //Create a child case, if the decriptio contains secret keywords
    for(case caseWithKeywords : casesWithKeywords){
        Case childCase             = new Case();
        childCase.subject         = childcaseSubject;
        childcase.ParentId        = caseWithKeywords.Id;
        childCase.isEscalated    = True;
        childCase.Priority      = 'High';
        childCase.Description     = 'Atlest one of the following keywords were found' + keywords;
        insert childCase;
            
    }
    }
 
Abdul KhatriAbdul Khatri
Hi,

I tweake a little bit since you are using DML inside the loop, I took it out and also before creating child I make sure I get something. Since you are doing String check make sure you get the results.
 
trigger CheckSecretInformation on Case (after insert, before update) {
        
      String childcaseSubject = 'Warning: Parent may contain Secret Info';

      //Create a collection of each of our secret keywords
      Set<String> Keywords = new Set<string>();
      keywords.add('SSN');
      Keywords.add('Social Seccurity');
      keywords.add('Credit Card');
      keywords.add('Passport');
      Keywords.add('BodyWeight');
             
     
      //Checking if the case description contains any of the secret keywords
      List<Case> casesWithKeywords = new List<Case>();
      for (case myCase : Trigger.New)
      {
            if (mycase.Subject. != childcaseSubject)
            {    
                  for (string keyword : keywords){
                        if (myCase.Description != null && myCase.Description.containsIgnoreCase(keyword)){
                              casesWithKeywords.add(myCase);
                              system.debug('Case' + myCase.Id + 'include keyword' + keyword);
                              break;
                        }
                  }               
            }
      }  

      if(casesWithKeywords.isEmpty()) return;
    
      //Create a child case, if the decriptio contains secret keywords
      List<Case> childCaseToInsertList = new List<Case>();
      for(case caseWithKeywords : casesWithKeywords)
      {
            Case childCase = new Case();
            childCase.subject = childcaseSubject;
            childcase.ParentId = caseWithKeywords.Id;
            childCase.isEscalated = True;
            childCase.Priority = 'High';
            childCase.Description = 'Atlest one of the following keywords were found' + keywords;

            childCaseToInsertList.add(childCase);
      }

      if(!childCaseToInsertList.isEmpty())
            Database.insert();
}