+ Start a Discussion
Wes McCarthyWes McCarthy 

Adding multiple junction records at the same time

Hi there,

I have a custom object called Skill Set.  Within each Skill Set there are 3 junction objects.
1. Expert At
2. Intermediate At
3. Novice At

The junction records are used to link Contact records to the Skill Set, and gives us visibility over which contacts have a speicfic level of skill. (Juntion objects have 2 lookup fields, 1 to Skill Set, 1 to Contact).

The only issue im finding with this is that I cant add more than 1 record at a time.  E.g. If i had 10 contacts that were an Expert At Skill_A, i would need to create 10 records, one at a time.  Also, if i have a Contact that has 10 skills, i have to create 10 junction records, one at a time.  This can be quite time consuming.  

Im wondering if its possible to create multiple junction records at the same time.  
E.g. 1
Im in a Contact record and want to record that the person is an Expert At 10 skills.  
I scroll to the "Expert At" reated list on the Contact and select Add New.
This returns a lookup window listing all Skills, with a checkbox next to each one, allowing me to select multiple skills.
I select 10 skills, and press OK.
This returns me to the Contact record, where i can see 10 junction Expert At related records, one for each Skill.

E.g. 2
Im in a Skill Set record, and want to add many contacts to the Skill as Experts At.
I scroll to the Expert At related list and select "Add New".
This allows me to search for and Add the 10 contacts.
I press Ok.
This returns me to the Skill Set record, where i can see 10 junction "Expert At" related records, one for each Contact.

Is this achieveable?  If so, can anyone tell me how?
 

Many thanks.  Much appreciated.

Wai Ming Fong 30Wai Ming Fong 30
Was just trying to figure this out myself and came across the answer in another discussion here:

https://developer.salesforce.com/forums/?id=906F0000000DDQvIAO

Hope that helps.
Kancharla ChakravarthyKancharla Chakravarthy

The below should work
 
Trigger:
Trigger TriggerName on YourObject__c (after insert) {
      if(Trigger.isafter && Trigger.isinsert){ 
          Object_Trigger_Handler.inserJunctionRecords(Trigger.new);
      }
 }


 //Helper Class:
public class Object_Trigger_Handler{ 
    public static void inserJunctionRecords(list<YourObject__c > programsList){ 
               List<Account> accountList = [Select id,Recordtypeid from Account where Recordtype.Name in('Dealer','Customer')]; 
                List<Junctionobject__c > programLOEToInsert = new List<Junctionobject__c >(); 
                for(Account acc: accountList){
                        for(YourObject__c programID: programsList){ 
                                 Junctionobject__c newLOE = new Junctionobject__c (); 
                                 newLOE.AccountName_del__c = acc.Id; 
                                 newLOE.ProgramName__c = YourObject__c.Id; 
                                 programLOEToInsert.add(newLOE);
                         } 
                 } 
                if(programLOEToInsert.size()>0){ 
                    insert programLOEToInsert; 
                 }
     } 
}