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
Pooja Joshi 38Pooja Joshi 38 

How to populate values in a Junction object

I am trying to acheive this by Batch apex.

I have an external object in Salesforce which is getting data from an external system. The external object has multiple rows for each student to represent the multiple courses he/she is taking.

For each insert, update and delete I want to update Student object, Course boject and a junction object which will hold many to many relationship between Students and Courses 

For eg. if student Peter is taking two courses Math and English, that would be two rows in External object.
After the batch runs one row should be inserted into the Student object and two rows into the Course object and two rows into the Juction object.

Thanks so much.
Pooja
Abhishek BansalAbhishek Bansal
Hi Pooja,

You can run a batch and query all the records from external object that are newly created in the system in the start method.
In the execute method you can first insert all the students, than all the courses. At the end you can insert the records in the junction object.

Let me know if you need any help with the code.

Thanks,
Abhishek Bansal.