I have created two custom objects (A and B)and a junction object(AB). i.e. Both A and B have Many to Many relationship. Now I need to insert 5000 records. How can I automatically populate the relation in Junction object. DO I have to write any trigger or Do I have to do Manually mapping the records?

  • Are you inserting 5000 junction objects? Or 5000 of the other objects? The relation in the junction object is just a couple of ids. Since a junction object contains real information - which object is linked to which, you will need to use some sort of external id to match objects A and B. However, I can't really give you a real answer, as you have not provided enough information. – Caspar Harmer Apr 6 '15 at 21:01
  • How are you planning on inserting these records? Writing a trigger is probably not the solution you are looking for. – CyberJus Apr 6 '15 at 21:06
  • Hello.. @CaspNZ I am inserting A and B records. Initially before inserting I have created the Junction object AB and the two master details relationships. Now I am stuck at a situation where I have to populate the junction object records after inserting A and B records. – SFDC_BigDog Apr 6 '15 at 22:18
  • @CyberJus I am inserting A and B records using Apex dataloader – SFDC_BigDog Apr 6 '15 at 22:19
  • Manually is probably going to be the easiest way to do it, and by manually I mean get the ids after the data loader insert, put them in another csv file and import that to your junction object. – CyberJus Apr 6 '15 at 23:54

The best way to achieve this is by using the data loader. I would recommend using www.dataloader.io since it is infinitely easier to use and it contains a nifty feature that will make your life easier.

Dataloader.io allows you to "lookup" related records based on a certain field, so for example:

Say you wanted to create an account via dataloader and wanted to set the OwnerId field, but you only have the email of the user you want to be set as the owner. You can configure Dataloader.io to actually do a lookup of the user id based on the email, saving you the extra step of massaging the CSV.

To be more specific to your example, I would:

  1. Make sure that Object A and Object B contain an external Id that identifies them from the old system.
  2. Do a dump of the "AB" table from the old system into a CSV. The format needs to be like this: A_OldSystemId, B_OldSystemId
  3. Use dataloader.io (Jitterbit also has this feature but I prefer dataloader.io) to load the data into the new AB Salesforce object. Make sure that you use the "Lookup" feature.

You can read about how to use the lookup feature here: https://dataloader.io/using-import-lookups

Hope this helps!

| improve this answer | |

I am currently having the same issue. I am writing a script to run on a schedule to automatically import data from our accounting software into salesforce using their API. I cannot find a good way to upsert objects with many to many relationships. Right now I have to upsert the related objects separately and then clear all the join objects and create new ones. My goal is to be able to upload the first related objects and then upload the second related objects with their relationships to the first objects.

A middle ground workaround I am doing that might also work for you is to setup an external id field on your join object. For the external id you would use text made up of the combined external id from the first object and the id from the second object. With that you could now upsert the join objects for the relationship rather than deleting them all then creating or checking if it exists and creating if not for each.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.