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?
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:
- Make sure that Object A and Object B contain an external Id that identifies them from the old system.
- 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
- 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!
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.