Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.4 kB
1
Indexable
Never
trigger autoTaskCreation on Account (after insert, after update) {
   // AutoTaskCreation.handleTrigger();
    
    Map<Id, String[]> parentsMap = new Map<Id, String[]>();
    
 	list<String> arrTest = new list<String>();
    // Only care about changes to this specific field
    for (Account a : Trigger.new) {
        if ((Trigger.oldMap == null && a.description != null)|| (Trigger.oldMap != null && a.description 
                                      != Trigger.oldMap.get(a.Id).description)){
            arrTest = a.description.split(',');
            parentsMap.put(a.Id, arrTest);
        }
    }
	System.debug('a.description '+parentsMap);
    if (parentsMap.size() > 0) {

        Account[] updates = new Account[] {};

        // Query the children
        for (Account child : [
            select Id,Name, ParentId, description
            from Account
            where name in :arrTest
        ]) {
            for(Id pId : parentsMap.keySet()){
                List<String> tmpList  = parentsMap.get(pId);
                if(tmpList.contains( child.Name)){
                      child.ParentId = pId;
                }
                updates.add(child);
            }
        }
          System.debug('a.updates '+updates);
    	  if(!updates.isEmpty() && checkRecursive.runOnce())
          update updates;
        }

       
    
}