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
Mark Z DeanMark Z Dean 

Adding List of Strings to List of Object

I am trying to write a trigger on Case object that will detect specific confidential terms in the Description field and create a new CHILD case with the the detected terms in the Description field of the child record. I wrote the code below but having difficulty figuring out how to get the description of the new child record updated (line 34) with the confidential terms from the parent record. 
trigger ConfidentialTerms on Case (after insert, before insert) {
	//insert all terms in list
	List<String> terms;
    Set<Case> NewChildCase = new Set<Case>();
    List<Case> NewCases;
	String t;
    List<String> LString = new List<String>();
    
    terms.add('Credit Card');
    terms.add('Passport');
    terms.add('License');
    
    
	//iterate through terms list and check each record's description
        for(Case c : trigger.new){
          	 for(String s : terms){
        		    if(c.description.Contains(s) && c.Description != null){
          		      t =+ s;
                      NewChildCase.add(c);  

        			    }				
             
        		}
         LString.add(t);      
   		}
    
    //create child record
    	for(Case cc : NewChildCase){
	
            Case childCase = new Case();
            childCase.parentid = cc.id;
            childCase.Subject = 'High Priority Case';
            
            childCase.Description = '';
            NewCases.add(childCase); 
        	}  
    
    insert NewCases;
}
I don't need the code but just hints on how this can be done. Much appreciated...
Raj VakatiRaj Vakati
try this 
trigger ConfidentialTerms on Case (after insert, before insert) {
//insert all terms in list
List<String> terms = new List<String>();
Map<Case,String> NewChildCase = new Map< Case,String>();
List<Case> NewCases;

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

terms.add('Credit Card');
terms.add('Passport');
terms.add('License');


//iterate through terms list and check each record's description
	for(Case c : trigger.new){
			String t ='' ;
		 for(String s : terms){
			if(c.description.Contains(s) && c.Description != null){
				  t =+ s;
			}				
		}
		NewChildCase.put(c,t); 
 	}

//create child record
	for(Case cc : NewChildCase.keySet()){

		Case childCase = new Case();
		childCase.parentid = cc.id;
		childCase.Subject = 'High Priority Case';
		childCase.Description = NewChildCase.get(cc);
		NewCases.add(childCase); 
		}  

insert NewCases;
}



 
Steven NsubugaSteven Nsubuga
  • First off, why have both before insert and after insert, and yet make no distinction on when each is to run. Get rid of the before insert.
  • Since you are creating additional Cases within a Case trigger, you need to prevent an endless loop due to recursion: your trigger will keep calling itself until it hits a limit.
  • Lastly, check out the code below, it is the most concise way I can think of as far as hints go. I am sure it is some what incomplete anyway.

Class to stop recursion.
public class CheckRecursive { 
   public static boolean firstRun = true; 
}


Trigger
trigger ConfidentialTerms on Case (after insert) {
	
	if(CheckRecursive.firstRun){ 
		CheckRecursive.firstRun = false;
		
		//insert all terms in list
		List<String> terms = new List<String>();
		Set<Case> NewChildCase = new Set<Case>();
		List<Case> NewCases;
		
		terms.add('Credit Card');
		terms.add('Passport');
		terms.add('License');    
    
	//iterate through terms list and check each record's description
        for(Case c : trigger.new){
			for(String s : terms){
				if(c.description.Contains(s) && c.Description != null){
					NewChildCase.add(new Case(parentid = cc.id, Description = s, Subject = 'High Priority Case'));  
				}           
			}
   		}
		if (NewChildCase.size() > 0) {
			NewCases.addAll(NewChildCase);
			insert NewCases;
		}
	}
}

 
Mark Z DeanMark Z Dean
Thanks Raj and Steven. A few questions for both of you:

@Raj - your code worked perfectly fine with some adjustments but how does the system know to fetch the description field from the record in line below? I could have had another text field as well in NewChildCase so how would I differentiate which one I want to use for description:
childCase.Description = NewChildCase.get(cc);

@Steve - I corrected the code to run only once to avoid recursion but how would it run if there were buld records? or is each record that needs processing run into its own execution context? Because if not, once the firstRun is set to false, it wouldn't run for the next record. 
Raj VakatiRaj Vakati
Based on the API Name .. Description is the standard Field and its API name is Description... If you wanted to use other custom description fields you need to use the correct api name like Description__c 
Mark Z DeanMark Z Dean
You mean this part of the code "NewChildCase.get(cc);" will automatically get the correct field based on the field that is on the left side of the assignment, "childCase.Description =" in my case?
Mark Z DeanMark Z Dean
Raj - I would like to mark the best answer on this one and hopefully you can help me with my quesiton above. Thanks...