You can use composite api here.
In the below example, We have Account and Order__c and we create Junction object as Student_Orders__c
in the same request.
Use of POST
or PATCH
depends on you, but we used patch so that any other requests should always be updated using externalId passed.
{
"allOrNone": true,
"compositeRequest": [
{
"method": "PATCH",
"url": "/services/data/v48.0/sobjects/Account/Premium_ID__c/cghjmhuiokuhi456Auto",
"referenceId": "RefStudentcghjmhuiokuhi456Auto",
"body": {
"FirstName": "Automation cghjmhuiokuhi456Auto",
"LastName": "User cghjmhuiokuhi456Auto"
}
},
{
"method": "PATCH",
"url": "/services/data/v48.0/composite/sobjects/Order__c/Order_No__c",
"referenceId": "orderList",
"body": {
"allOrNone": true,
"records": [
{
"attributes": {
"type": "Order__c",
"referenceId": "order1"
},
"Order_No__c": "TestOrder",
"Name": "Order : K12 course-1st Order"
}
]
}
},
{
"method": "PATCH",
"url": "/services/data/v52.0/composite/sobjects/Student_Orders__c/Reference_Id__c",
"referenceId": "studentOrderList",
"body": {
"allOrNone": true,
"records": [
{
"attributes": {
"type": "Student_Orders__c",
"referenceId": "studentOrder1"
},
"Name": "Student Order : cghjmhuiokuhi456Auto 1",
"Order__c": "@{orderList[0].id}",
"Student__c": "@{RefStudentcghjmhuiokuhi456Auto.id}",
"Reference_Id__c": "@{orderList[0].id}-@{RefStudentcghjmhuiokuhi456Auto.id}"
}
]
}
}
]
}