+ Start a Discussion
Jeremiah CochranJeremiah Cochran 

Prevent trigger from creating more than 1 record

Hi, I'm new to writing triggers and need some assistance with the following code, I have it work for the most part, what is giving me trouble is trying to figure out how to prevent it from creating multiple child cases for the same parent case. For example, if the parent case that fires the trigger has 2 secret keywords, then 2 child cases are created when I only want 1. Thanks in advance.

Here is the trigger code:

trigger CheckSecretInformation on Case (after insert, before update) 
{
    String childCaseSubject = 'Warning this case may contain secret info';

//Step 1: Create a collection containing each secret keyword
    Set<String> secretKeywords = new Set<String>();
    secretKeywords.add('Credit Card');
    secretKeywords.add('Social Security');
    secretKeywords.add('SSN');
    secretKeywords.add('Passport');
    secretKeywords.add('Body Weight');

    //Step 2: Check to see if a case contains any of the secret keywords
    List<Case> casesWithSecretInfo = new List<Case>();
    List<String> secretKeywordsFound = new List<String>();
    
    for (Case myCase : Trigger.new) 
    {
        if (myCase.Subject != childCaseSubject) 
        {
            for (String keyword : secretKeywords) 
            {
                 if (myCase.Description !=null && myCase.Description.containsIgnoreCase(keyword)) 
                {
                    casesWithSecretInfo.add(myCase);
                    secretKeywordsFound.add(keyword);
                    
                    System.debug ('Case ' +myCase.ID + ' includes secret keyword ' +keyword);
                  }
            }
        }

    }
        //Step 3: If a case contains secret keywords, create a child case
        List<Case> caseToCreate = new List<Case>();
        for (Case caseWithSecretInfo : casesWithSecretInfo) 
        {
            if (caseWithSecretInfo.Cases.Size()<1) 
             {
                       
            Case childCase = new Case();
            childCase.Subject = childCaseSubject;
            childCase.ParentId = caseWithSecretInfo.Id;
            childCase.Priority = 'High';
            childCase.IsEscalated = true;
            childCase.Description = 'At least one of the following keywords were found ' + secretKeywordsfound;
            caseToCreate.add(childCase);
             }
        }

        insert caseToCreate;
}
Best Answer chosen by Jeremiah Cochran
Amit Singh 1Amit Singh 1
Use Below Code,
 
trigger CheckSecretInformation on Case (after insert, before update) 
{
    String childCaseSubject = 'Warning this case may contain secret info';

//Step 1: Create a collection containing each secret keyword
    Set<String> secretKeywords = new Set<String>();
    secretKeywords.add('Credit Card');
    secretKeywords.add('Social Security');
    secretKeywords.add('SSN');
    secretKeywords.add('Passport');
    secretKeywords.add('Body Weight');

    //Step 2: Check to see if a case contains any of the secret keywords
    List<Case> casesWithSecretInfo = new List<Case>();
    List<String> secretKeywordsFound = new List<String>();
    Map<Id, case> caseMap = new Map<Id, case>();
    for (Case myCase : Trigger.new) 
    {
        if (myCase.Subject != childCaseSubject) 
        {
            for (String keyword : secretKeywords) 
            {
                 if (myCase.Description !=null && myCase.Description.containsIgnoreCase(keyword)) 
                {
                    //casesWithSecretInfo.add(myCase);
                    secretKeywordsFound.add(keyword);
                    if(!caseMap.containsKey(myCase.Id)){
						caseMap.put(myCase.Id, myCase);
					}
                    System.debug ('Case ' +myCase.ID + ' includes secret keyword ' +keyword);
                  }
            }
        }

    }
        //Step 3: If a case contains secret keywords, create a child case
        List<Case> caseToCreate = new List<Case>();
        for (Case caseWithSecretInfo : caseMap.values()) 
        {
            if (caseWithSecretInfo.Cases.Size()<1) 
             {
                       
            Case childCase = new Case();
            childCase.Subject = childCaseSubject;
            childCase.ParentId = caseWithSecretInfo.Id;
            childCase.Priority = 'High';
            childCase.IsEscalated = true;
            childCase.Description = 'At least one of the following keywords were found ' + secretKeywordsfound;
            caseToCreate.add(childCase);
             }
        }

        insert caseToCreate;
}

Let me know if this helps :)

Thanks,
Amit
 

All Answers

Amit Singh 1Amit Singh 1
Use Below Code,
 
trigger CheckSecretInformation on Case (after insert, before update) 
{
    String childCaseSubject = 'Warning this case may contain secret info';

//Step 1: Create a collection containing each secret keyword
    Set<String> secretKeywords = new Set<String>();
    secretKeywords.add('Credit Card');
    secretKeywords.add('Social Security');
    secretKeywords.add('SSN');
    secretKeywords.add('Passport');
    secretKeywords.add('Body Weight');

    //Step 2: Check to see if a case contains any of the secret keywords
    List<Case> casesWithSecretInfo = new List<Case>();
    List<String> secretKeywordsFound = new List<String>();
    Map<Id, case> caseMap = new Map<Id, case>();
    for (Case myCase : Trigger.new) 
    {
        if (myCase.Subject != childCaseSubject) 
        {
            for (String keyword : secretKeywords) 
            {
                 if (myCase.Description !=null && myCase.Description.containsIgnoreCase(keyword)) 
                {
                    //casesWithSecretInfo.add(myCase);
                    secretKeywordsFound.add(keyword);
                    if(!caseMap.containsKey(myCase.Id)){
						caseMap.put(myCase.Id, myCase);
					}
                    System.debug ('Case ' +myCase.ID + ' includes secret keyword ' +keyword);
                  }
            }
        }

    }
        //Step 3: If a case contains secret keywords, create a child case
        List<Case> caseToCreate = new List<Case>();
        for (Case caseWithSecretInfo : caseMap.values()) 
        {
            if (caseWithSecretInfo.Cases.Size()<1) 
             {
                       
            Case childCase = new Case();
            childCase.Subject = childCaseSubject;
            childCase.ParentId = caseWithSecretInfo.Id;
            childCase.Priority = 'High';
            childCase.IsEscalated = true;
            childCase.Description = 'At least one of the following keywords were found ' + secretKeywordsfound;
            caseToCreate.add(childCase);
             }
        }

        insert caseToCreate;
}

Let me know if this helps :)

Thanks,
Amit
 
This was selected as the best answer
Jeremiah CochranJeremiah Cochran
Thank you Amit, that did it.