+ Start a Discussion
Andrew Morales 1Andrew Morales 1 

Write trigger to Create Junction Records After Upload

Creating Many to Many Relationships from a CSV
Hello!

I created the schema below with (two) Master Detail Fields on the Junction-Object-AB pointing to both A and B. I believe the schema is sound for my application since this is a Many to Many relationship.

Schema:
Standard Object A
Custom Object B
Junction Object AB

My question is related to uploading the data via csv. The data is coming from an internal database and each record has a unique ID (unique to the table) already so I dont have to worry about pulling back the SFDC ID after upload from the success file.

Where I am stuck is - on object B, within one record/line I have multiple record ID's in one cell pointing to the data in Object A. So an example would be:

Object A - Developer Contact
-- Tom Jones - ID - 1
-- Johnny Depp - ID - 2
-- Christina Aguilera - ID - 3

Object B - Applications
-- Angry Birds - Dev ID - 1,2,3
-- Tetris - Dev ID - 2,3

So I would need to create 3 junction records for Angry Birds and 2 junction records for Tetris based on the Developers that worked on them.
QUESTIONS
How can I create the junction records if all the ID's are in one cell?
Do all the ID's have to be loaded in a custom field and it has to be created via a trigger? (If so, some help articles or specific search topics would be appreciated. I am not a developer by trade)

I will be working with about 30K records for Object A and 30K for object B so this cannot be done manually. I would appreciate any suggestions and/or pointing me to some help articles so I can work through it.

THANK YOU!!!
Mahesh DMahesh D
Hi Andrew,

Please find the below trigger:

Here What I did is,

Standard Object - Account
Custom OBject - ObjectB
Junction Object - AccountObjBJunc

Created fields in Junction object:

User-added image

Implemented the below trigger on ObjectB and considered an insert scenario.
trigger ObjectBTrigger on ObjectB__c (after insert) {
    List<AccountObjBJunc__c> aOBList = new List<AccountObjBJunc__c>();
    
    
    Set<String> accIdSet = new Set<String>();
    
    for(ObjectB__c obj: Trigger.new) {
        if(obj.AccountId_Values__c != null && obj.AccountId_Values__c != '') {
            List<String> accIdList = obj.AccountId_Values__c.split(',');
            accIdSet.addAll(accIdList);
        }
    }
    
    if(!accIdSet.isEmpty()) {
        Map<Id, Account> accMap = new Map<Id, Account>([Select Id, Name from Account where Id IN: accIdSet]);
        for(ObjectB__c obj: Trigger.new) {
            if(obj.AccountId_Values__c != null && obj.AccountId_Values__c != '') {
                List<String> accIdList = obj.AccountId_Values__c.split(',');
                
                for(String accId: accIdList)  {
                    if(accMap.get(accId) != null) {
                        Account acc = accMap.get(accId);
                        AccountObjBJunc__c aOB = new AccountObjBJunc__c();
                        aOB.Name = obj.Name+' '+acc.Name;
                        aOB.ObjectB__c = obj.Id;
                        aOB.Account__c = acc.Id; 
                        aOBList.add(aOB);
                    }
                }
            }
        }
        
        if(!aOBList.isEmpty()) {
            insert aOBList;
        }
    }
}

Also tested the above trigger in my DE environment:

Results is:

User-added image


Please do let me know if it helps you.

Regards,
Mahesh
Andrew Morales 1Andrew Morales 1
Hi Mahesh. I have a question about your trigger, Im trying to get it to work for my application.

What was the purpose of the AccountId_Values__c field and what object was it created on? Thank you very much for all the help.
Andrew Morales 1Andrew Morales 1
Hi Mahesh, apologies, I see that it is on ObjectB based on your image. Ill try to get it to work. Thx
Andrew Morales 1Andrew Morales 1
Hi Mahesh. I am getting the following error:
ERROR: AddOnTrigger: execution of AfterInsertcaused by: System.StringException: Invalid id: 9External entry point
Any thoughts? Code is below. You can see my Objects are Contact, AddOn, Dev_AddOn(Junction)
trigger AddOnTrigger on AddOn__c (after insert) {
    List<Dev_AddOn__c> aOBList = new List<Dev_AddOn__c>();
    
    
    Set<String> accIdSet = new Set<String>();
    
    for(AddOn__c obj: Trigger.new) {
        if(obj.ContactID_Values__c != null && obj.ContactID_Values__c != '') {
            List<String> accIdList = obj.ContactID_Values__c.split(',');
            accIdSet.addAll(accIdList);
        }
    }
    
    if(!accIdSet.isEmpty()) {
        Map<Id, Contact> accMap = new Map<Id, Contact>([Select Id, Name from Contact where Id IN: accIdSet]);
        for(AddOn__c obj: Trigger.new) {
            if(obj.ContactID_Values__c != null && obj.ContactID_Values__c != '') {
                List<String> accIdList = obj.ContactID_Values__c.split(',');
                
                for(String accId: accIdList)  {
                    if(accMap.get(accId) != null) {
                        Contact acc = accMap.get(accId);
                        Dev_AddOn__c aOB = new Dev_AddOn__c();
                        aOB.Name = obj.Name+' '+acc.Name;
                        aOB.AddOn__c = obj.Id;
                        aOB.Contact__c = acc.Id; 
                        aOBList.add(aOB);
                    }
                }
            }
        }
        
        if(!aOBList.isEmpty()) {
            insert aOBList;
        }
    }
}

 
Kancharla ChakravarthyKancharla Chakravarthy
The below should work
 
Trigger TriggerName on YourObject__c (after insert) {

    if(Trigger.isafter && Trigger.isinsert){
        Object_Trigger_Handler.inserJunctionRecords(Trigger.new);
    }
}
 
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;
        }

    }
    
}