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
Vida YirenkyiVida Yirenkyi 

Help fix this code

Hello Experts,

I need help with this code to show only the words in the parent case description in the child case. At the moment the child case description lists all the keywords.
trigger CheckSecretInfo 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');


List<Case> casesWithSecretInfo = new List<Case>();
Set<String> KeyWords = new Set<String>();
for (Case myCase : Trigger.new){
if(myCase.Subject != childCaseSubject){

// Step 2 Loop through secretkeywords and add all offending words to a Set

for(String KeyWord: secretKeywords){
if(myCase.Description != null && myCase.Description.containsIgnoreCase(KeyWord)){
	secretKeywords.add(KeyWord);
    break;
    system.debug('Keywords are: ' + keyword);
}
}

system.debug('cases to create' + casesWithSecretInfo.size());
	//Loop through secretkeywords and if there is any secret keywords add case to be created

List<Case> casesToCreate = new List<Case>();
for(Case caseWithSecretInfo : casesWithSecretInfo){
    Case childCase = new Case();
    childCase.Subject = 'Warning: Parent case may contain secret info';
    childCase.ParentId = caseWithSecretInfo.Id;
    childCase.IsEscalated = true;
    childCase.Priority = 'High';
    childCase.Description = 'The following secret keywords were found: ' + KeyWords;
    casesToCreate.add(childCase);
}
    insert casesToCreate;
}
}

}
Thanks 
 
Best Answer chosen by Vida Yirenkyi
Austin MuellerAustin Mueller
I made a minor change from the previous code I posted as an enhancement, along with the comments.
 
trigger CheckSecretInfo on Case (after insert, before update) {

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

    // 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');

    // Will hold all child cases that are already linked to the parent.
    // Will help prevent duplicate child cases for the same parent.
    Map < Id, Id > existingChildCases = new Map < Id, Id >();

    // Check if trigger was launched from an update
    if( Trigger.isUpdate ) {

        // Query child cases linked to all of the parent case IDs
        for ( Case myCase : [ SELECT Id, ParentId FROM Case WHERE ParentId IN :Trigger.newMap.keySet() ] ) {

            // Add the parent case ID as the primary key and the child case ID as the value of the primary key.
            existingChildCases.put( myCase.ParentId, myCase.Id );

        }

    }

    // Will hold cases that need to be updated / created
    List<Case> casesToCreate = new List<Case>();

    // Loop through cases being updated or inserted
    for ( Case myCase : Trigger.new ) {

        // Will hold keywords found in the description
        // This will be reset for every case that it processes
        List<String> keywords = new List<String>();

        // Check if case subject is not equal to the static child case subject & case description is not null
        if( myCase.Subject != childCaseSubject && myCase.Description != null ) {

            // Loop over the secret keywords in the set
            for( String k : secretKeywords ) {
                    
                // Check if description contains the keyword in the current iteration
                if( myCase.Description.containsIgnoreCase( k ) ) {

                    // Add keyword to the list
                    keywords.add( k );

                }

            }

            // Check if any keywords were found on the current case
            if( !keywords.isEmpty() ) {

                system.debug( 'Id: ' + myCase.Id + ', keywords are:' + String.join( keywords, ', ') );

                // Create child case record needed to be updated / inserted
                Case childCase = new Case(
                    Subject = 'Warning: Parent case may contain secret info',
                    IsEscalated = true,
                    Priority = 'High',
                    Description = 'The following secret keywords were found: ' + String.join( keywords, ', ')
                );

                // Check if the current case in the iteration already has a child case
                if( existingChildCases.get( myCase.Id ) != null ) {

                    // Set the child case ID to be updated, this only happens if the child case has to be updated.
                    childCase.Id = existingChildCases.get( myCase.Id );

                } else {

                    // Link the child case to the parent case, this only happens if the child case has to be created.
                    childCase.ParentId = myCase.Id;

                }

                // Add to the list of cases to be created / updated
                casesToCreate.add( childCase );

            }

        }
        
    }
    
    // Check if any cases have to be created / updated & execute DML
    if( !casesToCreate.isEmpty() ) upsert casesToCreate;

}

 

All Answers

Karan ShahKaran Shah
Hi,

To begin with, your List variable "casesWithSecretInfo"  will not hold any value as this is not effectively used. Infact, its never used.
2nd, the break statement will exit the For Loop.

I have tried to re-write a part of the code w/ Comments to help you.
 
trigger KS_CheckSecretInfo on Case (after insert, before update) 
{
	Set<Case> CaseId = new Set<Case>(); // To capture the Case Ids of the Updated/Created Cases 
    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');

	Map<Id, String> KeyWords = new Map<Id, String>(); // Map of case Id and their keywords.
	String Final_Keyword = '';

	for (Case myCase : Trigger.new)
	{
		if(myCase.Subject != childCaseSubject)
		{
			// Step 2 Loop through secretkeywords and add all offending words to a Set
			for(String KeyWord: secretKeywords)
			{
				if(myCase.Description != null && myCase.Description.containsIgnoreCase(KeyWord))
				{
				    Final_Keyword =  Final_Keyword + KeyWord +  ',';
				}
			}
			if (Final_Keyword != '') // Needed only if the keyword is found in Desc.
			{
				Final_Keyword = Final_Keyword.removeEnd(',');
			}
			KeyWords.put(myCase.Id, Final_Keyword);
			CaseId.add(myCase);
		}
	}
	List<Case> casesToCreate = new List<Case>();
	for(Case caseWithSecretInfo : CaseId) //Query for Updated/Inserted cases
	{
	    Case childCase = new Case();
		childCase.Subject = 'Warning: Parent case may contain secret info';
	    childCase.ParentId = caseWithSecretInfo.Id;
	    childCase.IsEscalated = true;
	    childCase.Priority = 'High';
	    childCase.Description = 'The following secret keywords were found: ' + KeyWords.get(caseWithSecretInfo.id);
	    casesToCreate.add(childCase);
	}
	insert casesToCreate;
}

 
Austin MuellerAustin Mueller
There are a couple ways to do this, here is another version as well.
 
trigger CheckSecretInfo 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');

    Map < Id, Id > existingChildCases = new Map < Id, Id >();

    if( Trigger.isUpdate ) {

        for ( Case myCase : [ SELECT Id, ParentId FROM Case WHERE ParentId IN :Trigger.newMap.keySet() ] ) {

            existingChildCases.put( myCase.ParentId, myCase.Id );

        }

    }

    List<Case> casesToCreate = new List<Case>();

    for ( Case myCase : Trigger.new ) {

        List<String> keywords = new List<String>();

        if( myCase.Subject != childCaseSubject ) {

            // Step 2 Loop through secretkeywords and add all offending words to a Set
            for( String k : secretKeywords ) {
                
                if( myCase.Description != null && myCase.Description.containsIgnoreCase( k ) ) {

                    keywords.add( k );
                    
                    // Break will exit the entire loop
                    // break;

                }

            }

            if( !keywords.isEmpty() ) {

                system.debug( 'Id: ' + myCase.Id + ', keywords are:' + String.join( keywords, ', ') );

                Case childCase = new Case(
                    Subject = 'Warning: Parent case may contain secret info',
                    IsEscalated = true,
                    Priority = 'High',
                    Description = 'The following secret keywords were found: ' + String.join( keywords, ', ')
                );

                if( existingChildCases.get( myCase.Id ) != null ) {

                    childCase.Id = existingChildCases.get( myCase.Id );

                } else {

                    childCase.ParentId = myCase.Id;

                }

                casesToCreate.add( childCase );

            }

        }
        
    }
    
    if( !casesToCreate.isEmpty() ) upsert casesToCreate;

}

 
Vida YirenkyiVida Yirenkyi
Hi Karan Shah,
Hi Austin Mueller,
Thank you for your kind response to my quetion, It is much appreciated. I will give it a try..  Could you kindly give me detailed  comments or explanations of your code please.

best regards

Vida
Austin MuellerAustin Mueller
I made a minor change from the previous code I posted as an enhancement, along with the comments.
 
trigger CheckSecretInfo on Case (after insert, before update) {

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

    // 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');

    // Will hold all child cases that are already linked to the parent.
    // Will help prevent duplicate child cases for the same parent.
    Map < Id, Id > existingChildCases = new Map < Id, Id >();

    // Check if trigger was launched from an update
    if( Trigger.isUpdate ) {

        // Query child cases linked to all of the parent case IDs
        for ( Case myCase : [ SELECT Id, ParentId FROM Case WHERE ParentId IN :Trigger.newMap.keySet() ] ) {

            // Add the parent case ID as the primary key and the child case ID as the value of the primary key.
            existingChildCases.put( myCase.ParentId, myCase.Id );

        }

    }

    // Will hold cases that need to be updated / created
    List<Case> casesToCreate = new List<Case>();

    // Loop through cases being updated or inserted
    for ( Case myCase : Trigger.new ) {

        // Will hold keywords found in the description
        // This will be reset for every case that it processes
        List<String> keywords = new List<String>();

        // Check if case subject is not equal to the static child case subject & case description is not null
        if( myCase.Subject != childCaseSubject && myCase.Description != null ) {

            // Loop over the secret keywords in the set
            for( String k : secretKeywords ) {
                    
                // Check if description contains the keyword in the current iteration
                if( myCase.Description.containsIgnoreCase( k ) ) {

                    // Add keyword to the list
                    keywords.add( k );

                }

            }

            // Check if any keywords were found on the current case
            if( !keywords.isEmpty() ) {

                system.debug( 'Id: ' + myCase.Id + ', keywords are:' + String.join( keywords, ', ') );

                // Create child case record needed to be updated / inserted
                Case childCase = new Case(
                    Subject = 'Warning: Parent case may contain secret info',
                    IsEscalated = true,
                    Priority = 'High',
                    Description = 'The following secret keywords were found: ' + String.join( keywords, ', ')
                );

                // Check if the current case in the iteration already has a child case
                if( existingChildCases.get( myCase.Id ) != null ) {

                    // Set the child case ID to be updated, this only happens if the child case has to be updated.
                    childCase.Id = existingChildCases.get( myCase.Id );

                } else {

                    // Link the child case to the parent case, this only happens if the child case has to be created.
                    childCase.ParentId = myCase.Id;

                }

                // Add to the list of cases to be created / updated
                casesToCreate.add( childCase );

            }

        }
        
    }
    
    // Check if any cases have to be created / updated & execute DML
    if( !casesToCreate.isEmpty() ) upsert casesToCreate;

}

 
This was selected as the best answer
Vida YirenkyiVida Yirenkyi
Hell Austin, Thank you so much, that is very kind of you to add comments to each line of code for me. I have taken two days off work to give this code all my attention. I must know what each element is doing! With best regards Vida
Vida YirenkyiVida Yirenkyi
Hello Austin, sorry I missed o from hello, long day..Your help is very much appreciated.  I shall endeavour to understand all of it.