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
Test User 15Test User 15 

Trigger to find values from Parent Case Description with given Keywords

Hi,

This is challenge from Apex Academy: Fundamental Salesforce Coding Techniques by David Liu.
I want one child case but this trigger is creating Child case depending on the keywords like if I enter 2 keywords in Parent description, it will create 2 child cases.

Here is the trigger code
trigger CheckSecretInformation on Case (after insert, before update) {

	String childCaseSubject = 'Warning: Parent case may contain secret info';

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

	//Step 2: Check to see if our case contains any of the secret keywords
	List<Case> casesWithSecretInfo = new List<Case>();
	List<String> SecretKeywordFound = 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);
					SecretKeywordFound.add(keyword);
					System.debug('Case ' + myCase.Id + ' include secret keyword ' + keyword);
				}
			}
		}	
	}

	// Step 3: If our case contains a secret keyword, create a child case
	List<Case> casesToCreate = new List<Case>();
	
	for(Case caseWithSecretInfo : casesWithSecretInfo) {
		Case childCase        = new Case();
		childCase.subject     = childCaseSubject;
		childCase.ParentId    = caseWithSecretInfo.Id;
		childCase.IsEscalated = true;
		childCase.Priority    = 'High';
		childCase.Description = 'At least one of the following keywords were found ' + SecretKeywordFound;
		casesToCreate.add(childCase);
	
	}
	insert casesToCreate;

}

If I use break; in for loop, It will create one child case but it shows only first keyword not all the keyword that should be displays in one Child case.
 
trigger CheckSecretInformation on Case (after insert, before update) {

	String childCaseSubject = 'Warning: Parent case may contain secret info';

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

	//Step 2: Check to see if our case contains any of the secret keywords
	List<Case> casesWithSecretInfo = new List<Case>();
	List<String> SecretKeywordFound = 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);
					SecretKeywordFound.add(keyword);
					System.debug('Case ' + myCase.Id + ' include secret keyword ' + keyword);
					break;
				}
			}
		}	
	}

	// Step 3: If our case contains a secret keyword, create a child case
	List<Case> casesToCreate = new List<Case>();
	
	for(Case caseWithSecretInfo : casesWithSecretInfo) {
		Case childCase        = new Case();
		childCase.subject     = childCaseSubject;
		childCase.ParentId    = caseWithSecretInfo.Id;
		childCase.IsEscalated = true;
		childCase.Priority    = 'High';
		childCase.Description = 'At least one of the following keywords were found ' + SecretKeywordFound;
		casesToCreate.add(childCase);
	
	}
	insert casesToCreate;

}

Note: Please Don't use Map collection or SOQL. If there is a possibility that anyone can achieve this without MAP! 

Thanks
Glyn Anderson 3Glyn Anderson 3
If we can't use a map, then we have to maintain two parallel arrays, with the keys (Cases) in one and the values (keywords) in the other:

<pre>
trigger CheckSecretInformation on Case (after insert, before update)
{
    String childCaseSubject = 'Warning: Parent case may contain secret info';
 
    //Step 1: Create a collection containing each of our secret keywords
    Set<String> secretKeywords = new Set<String>
    {   'Credit Card'
    ,   'Social Security'
    ,   'SSN'
    ,   'Passport'
    ,   'Bodyweight'
    };
 
    //Step 2: Check to see if our case contains any of the secret keywords
    List<Case> casesWithSecretInfo = new List<Case>();
    List<List<String>> secretKeywordsFound = new List<List<String>>();
    for ( Case myCase : Trigger.new )
    {
        if  (   myCase.Subject == childCaseSubject
            ||  String.isBlank( myCase.Description )
            ) continue;

            List<String> keywordsFound = null;
            for ( String keyword : secretKeywords )
            {
                if ( !myCase.Description.containsIgnoreCase( keyword ) ) continue;

                if ( keywordsFound == null )
                {
                    casesWithSecretInfo.add( myCase );
                    keywordsFound = new List<String>();
                    secretKeywordsFound.add( keywordsFound );
                }
                keywordsFound.add( keyword );
            }
        }  
    }
 
    // Step 3: If our case contains a secret keyword, create a child case
    List<Case> casesToCreate = new List<Case>();
    for ( Integer index = 0; index < casesWithSecretInfo.size(); index++ )
    {
        Case caseWithSecretInfo = casesWithSecretInfo[ index ];
        String keywordsFound = String.join( secretKeywordsFound[ index ], ', ' );
        casesToCreate.add
        (   new Case
            (   Subject     = childCaseSubject
            ,   ParentId    = caseWithSecretInfo.Id
            ,   IsEscalated = true
            ,   Priority    = 'High'
            ,   Description = 'The following keywords were found: ' + keywordsFound
            )
        );
    }
    insert casesToCreate;
}
</pre>
Dana Karaffa 9Dana Karaffa 9
I put the break into Step 3 instead of Step 2 and it didn't create a bunch of cases anymore. 
Ryan DempseyRyan Dempsey
Thanks @Dana! i was placing that break outside that block of code and it wasn't working but now it is. Thank you! 
Arunkumar@16Arunkumar@16
I have introduced new bool variable by using that we can reduce child case creation for each secret word.

trigger CheckSecretInformation on Case (after insert,before update) {

    String childCaseSubject = 'Warning :Parent case may contian secret info';      
    
    //Step 1: Create a collection containing each of our secret keywords
    Set<String> secretKeywords = new Set<String>();
    secretKeywords.add('Credit Card');
    secretKeywords.add('Social Security');
    secretKeywords.add('SSN');
    secretKeywords.add('Passport');
    secretKeywords.add('Bodyweight');
    
    //Step 2: Check to see if our case contains any of the secret keywords
    List<Case> casesWithSecretInfo = new List <Case>();
    List<String> keywords = new List <String>();
    Boolean Val = false;
    for( Case myCase : Trigger.new) {
       if(myCase.Subject != childCaseSubject) {
           for(String keyword : secretKeywords) {
                if(myCase.Description != null && myCase.Description.containsIgnoreCase(keyword)) {
                    if (Val == false) {
                        casesWithSecretInfo.add(myCase);
                        val = true;
                    }    
                    keywords.add(keyword);
                    System.debug('Case' +myCase.id+ ' include secret keyword' + keyword);
                }
            }    
        }
    }
    
    // Step 3: If our case contains a secret keyword, create a child case
    for (case caseWithSecretInfo : casesWithSecretInfo) {
        Case childCase = new Case();
        childCase.Subject = childCaseSubject;
        childCase.ParentId = caseWithSecretInfo.Id;
        childCase.IsEscalated = true;
        childCase.Priority = 'High';
        System.debug('Hword' +keywords);
        childCase.Description = 'At least one of the following keywords were found' + keywords;
        insert childCase;
    }

}