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
Andrew Morales 1Andrew Morales 1 

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?
Can I manipulate the data within CSV and then upload?
Do all the ID's have to be loaded in a custiom field and it has to be done Programatically?

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!!!
Best Answer chosen by Andrew Morales 1
Mahesh DMahesh D
Hi Andrew,

Please use the below trigger:
 
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;
        }
    }
}

Please follow the solution provided in other post:

https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=ALLQUESTIONS&id=906F0000000DDQvIAO


Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Andrew,

You can achieve this by using the trigger on second object and create the junction object records accordingly based on the comma separed values to iterate and prepare the combinations and insert into Junction object.

Please do let me know if it helps you.

Regards,
Mahesh
Andrew Morales 1Andrew Morales 1
Hi @Mahesh D, Thanks for the response. That sounds good in theory. Do you know of any help articles, or specific searches I can perform in the Help or Forum section? I am looking for specifics/articles on how to execute the solution. Thanks.
Mahesh DMahesh D
Hi Andrew,

Please use the below trigger:
 
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;
        }
    }
}

Please follow the solution provided in other post:

https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=ALLQUESTIONS&id=906F0000000DDQvIAO


Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer