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
Ross Gilbert 31Ross Gilbert 31 

Creating multiple records with trigger

I'm working on a trigger that should create multiple juntion object records when a record is inserted.  The trigger below works in that it inserts 1 juntion object record.  The problem is I need it to create multiple juntion object records when the trigger fires, and it's not happening.  Not sure why.

In words what this trigger SHOULD do: when case is inserted, create juntion object record on every PS_Project__c record that belongs to the case's account.  In other words: say I create a case on an account that already has 2 PS_Project__c object records on it.  Upon me creating the case, the trigger should fire, and create 2 juntion object records: one on each PS_Project__c object record for that account:
 
trigger CaseTrigger on Case (after insert) {
    

 Map<Id,PS_Project__c> specialMap = new Map<Id,PS_Project__c>();
            Set<Id> accSet = new Set<Id>();
            List<Case_PS_Project_Junction__c> cpsjList = new List<Case_PS_Project_Junction__c>();
           
            for(Case c: trigger.new){
                accSet.add(c.AccountId);
            }
            
            for(PS_Project__c psp : [Select Id, Account__r.Id from PS_Project__c where Account__r.Id = :accSet]){
                specialMap.put(psp.Account__r.Id,psp);
            }            
            
            for(Case c: trigger.new){       
                if(specialMap.containsKey(c.AccountId)){
           
                    Case_PS_Project_Junction__c cpsj = new Case_PS_Project_Junction__c();
                    cpsj.Case__c = c.Id;
                    cpsj.PSProject__c = specialMap.get(c.AccountId).Id;
                    cpsjList.add(cpsj);
                }
            }
                insert cpsjList;

}

 
Head In CloudHead In Cloud
Hi Ross,

The problem with your trigger is that you are using the "specialMap" to keep 1 PS_Project__c per account..You should change this map to this:
Map<Id, list<PS_Project__c>> specialMap = new Map<Id, list<PS_Project__c>>();

Your new trigger should look like this:
trigger CaseTrigger on Case (after insert) {
    

 Map<Id,list<PS_Project__c>> specialMap = new Map<Id,list<PS_Project__c>>();
            Set<Id> accSet = new Set<Id>();
            List<Case_PS_Project_Junction__c> cpsjList = new List<Case_PS_Project_Junction__c>();
           
            for(Case c: trigger.new){
                accSet.add(c.AccountId);
            }
            
            for(PS_Project__c psp : [Select Id, Account__r.Id from PS_Project__c where Account__r.Id = :accSet]){
                if(!specialMap.containsKey(psp.Account__r.Id)){
                    specialMap.put(psp.Account__r.Id, new list<PS_Project__c>{psp});
                }else{
                    List<PS_Project__c> tempList = specialMap.get(psp.Account__r.Id);
                    tempList.add(psp);
                    specialMap.put(psp.Account__r.Id,tempList);
                }
                
            }            
            
            for(Case c: trigger.new){       
                if(specialMap.containsKey(c.AccountId)){
                    for(PS_Project__c psp : specialMap.get(c.AccountId)){
                        Case_PS_Project_Junction__c cpsj = new Case_PS_Project_Junction__c();
                        cpsj.Case__c = c.Id;
                        cpsj.PSProject__c = psp.Id;
                        cpsjList.add(cpsj);
                    }
                }
            }
                insert cpsjList;

}

Let me know if this helps. Thanks