You need to sign in to do that
Don't have an account?
Test 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
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.
Note: Please Don't use Map collection or SOQL. If there is a possibility that anyone can achieve this without MAP!
Thanks
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
<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>
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;
}
}