You need to sign in to do that
Don't have an account?

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:
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; }
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:
Your new trigger should look like this:
Let me know if this helps. Thanks