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
MedhanieHabteMedhanieHabte 

Using Split string to determine which words are found in a set

Greetings all, I've created this apex trigger to create a child case record when a few words are found in the description of a parent case record such as social security, credit cards, etc. What I am trying to do, however, is have the child case record create with a description that only contains the words found in the parent case description. These words are captured in the secretKeyword set.

I tried using the .split method in my string, but get this error that such methods don't exist, any steps I should take.

triggers/CheckSecretInformation.trigger: Method does not exist or incorrect signature: void split(String) from the type Set<String> 

 
trigger CheckSecretInformation on case (after insert, before update) {
	
	String childCaseSubject = 'Waring: 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 Cards');
	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 words
	List<Case> casesWithSecretInfo = new List<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);
				System.debug('Case ' + myCase.Id + ' include secret keywords' + keyword);
				break;
				}
			}
		}
	}
	// Step 3: If our case contains a secret keyword, create a child case
	List<Case> casesToCreate = new List<Case>();
	  System.debug('Size of offending cases: ' + casesWithSecretInfo.size());


	for (Case casesWithSecretInfo : casesWithSecretInfo) {
		Case childCase        = new Case();
		childCase.Subject     = childCaseSubject;
		childCase.ParentId    = casesWithSecretInfo.Id;
		childCase.IsEscalated = true;
		childCase.Priority    = 'High';
		childCase.Description = 'At least one of the following keywords were found:' + secretKeywords.split(;);
		casesToCreate.add(childCase);
	}
	insert casesToCreate;
}



 
Best Answer chosen by MedhanieHabte
PawanKumarPawanKumar
Hi,

Please try below. I have highlighted the change in bold.

trigger CheckSecretInformation on case (after insert, before update) {
    
    String childCaseSubject = 'Waring: 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 Cards');
    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 words
    List<Case> casesWithSecretInfo = new List<Case>();
    Map<String,String> caseIdWithSecretWord = new Map<String,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);
                System.debug('Case ' + myCase.Id + ' include secret keywords' + keyword);
                caseIdWithSecretWord.put(myCase.Id,Keyword);
                break;
                }
            }
        }
    }
    // Step 3: If our case contains a secret keyword, create a child case
    List<Case> casesToCreate = new List<Case>();
      System.debug('Size of offending cases: ' + casesWithSecretInfo.size());


    for (Case casesWithSecretInfo : casesWithSecretInfo) {
        Case childCase        = new Case();
        childCase.Subject     = childCaseSubject;
        childCase.ParentId    = casesWithSecretInfo.Id;
        childCase.IsEscalated = true;
        childCase.Priority    = 'High';
        childCase.Description = 'At least one of the following keywords were found:' + caseIdWithSecretWord.get(casesWithSecretInfo.Id);
        casesToCreate.add(childCase);
    }
    insert casesToCreate;
}

All Answers

PawanKumarPawanKumar
Hi,

Please try below. I have highlighted the change in bold.

trigger CheckSecretInformation on case (after insert, before update) {
    
    String childCaseSubject = 'Waring: 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 Cards');
    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 words
    List<Case> casesWithSecretInfo = new List<Case>();
    Map<String,String> caseIdWithSecretWord = new Map<String,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);
                System.debug('Case ' + myCase.Id + ' include secret keywords' + keyword);
                caseIdWithSecretWord.put(myCase.Id,Keyword);
                break;
                }
            }
        }
    }
    // Step 3: If our case contains a secret keyword, create a child case
    List<Case> casesToCreate = new List<Case>();
      System.debug('Size of offending cases: ' + casesWithSecretInfo.size());


    for (Case casesWithSecretInfo : casesWithSecretInfo) {
        Case childCase        = new Case();
        childCase.Subject     = childCaseSubject;
        childCase.ParentId    = casesWithSecretInfo.Id;
        childCase.IsEscalated = true;
        childCase.Priority    = 'High';
        childCase.Description = 'At least one of the following keywords were found:' + caseIdWithSecretWord.get(casesWithSecretInfo.Id);
        casesToCreate.add(childCase);
    }
    insert casesToCreate;
}
This was selected as the best answer
Deepal DsilvaDeepal Dsilva
I was trying to solve this using something similar to split string. I found that the method 'String.join' works similar to split string.
Here's what I came up with.
 
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> keywordsinCase    = 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);
                     
                       //Find secret keywords in case and add them to keywordsinCase
                        for(String key : secretKeywords ){
                            if(myCase.Description.containsIgnoreCase(key)){
                                keywordsinCase.add(key);
                            }
                        }    
                        System.debug('Case ' + myCase.Id + ' includes secret keyword ' + keywordsinCase);
                        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 	= 'The following keywords were found ' + String.join(keywordsinCase, ', ');
        casesToCreate.add(childCase);
   	 }
       insert casesToCreate;
 }