You need to sign in to do that
Don't have an account?
MedhanieHabte
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>
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; }
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
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;
}
Here's what I came up with.