You can create a record of parent and child object records in one go by using Reference and External Id.
- Create a String field(External) on case object for example:
- Query Case Object record with Workstream Child object records
- Loop on query
- Create a new instance reference on case object and assign caseId+'abc'
- A loop on child object records(Workstream)
- Clone the child object record by using a clone method
- Set the parent field equal to the reference of case instance
- Add into the list
- Once the child object list is prepared, create clone instance of case and set same external id field and value = caseId+'abc'
- Add into list
- Add both lists into one list
- insert lists and delete the above-selected case list records
Here is a reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_foreign_keys.htm
Here is an example of code, I have done for other objects
String recordId = 'somerecord'; // if we want for individual record
Datetime yesterdayDateTime = Datetime.now().addDays(-1);
String childObjectName = 'LiveChatTranscriptEvent';
Set<String> childFieldApiList = Schema.getGlobalDescribe().get(childObjectName).getDescribe().fields.getMap().keySet();
String chileQuery = 'SELECT ' + String.join(new List<String>(childFieldApiList), ',');
chileQuery += ' FROM LiveChatTranscriptEvents';
String objectName = 'LiveChatTranscript';
Set<String> fieldApiList = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().keySet();
fieldApiList.remove('chatkey');
String query = 'SELECT ' + String.join(new List<String>(fieldApiList), ',');
query += ', (' + chileQuery + ')';
query += ' FROM ' + objectName;
if (String.isNotEmpty(recordId)) {
query += ' Where Id = :recordId AND Status = \'InProgress\' AND';
} else {
query += ' Where Status = \'InProgress\' AND';
}
query += ' CreatedDate < :yesterdayDateTime';
List<LiveChatTranscript> chatTranscriptList = Database.query(query);
List<SObject> cloneChatTranscriptList = new List<SObject>();
List<SObject> cloneChatTranscriptEventList = new List<SObject>();
LiveChatTranscript liveChatTranscript = new LiveChatTranscript();
LiveChatTranscriptEvent liveChatTranscriptEvent = new LiveChatTranscriptEvent();
for (LiveChatTranscript liveChatTranscriptObj : chatTranscriptList) {
String externalId = liveChatTranscriptObj.Id + '_yg';
LiveChatTranscript liveChatTranscriptExternalIdRef = new LiveChatTranscript();
liveChatTranscriptExternalIdRef.External_Id__c = externalId;
for (LiveChatTranscriptEvent liveChatTranscriptEventObj : liveChatTranscriptObj.LiveChatTranscriptEvents) {
Map<String, Object> liveChatTranscriptEventMap = (Map<String, Object>) JSON.deserializeUntyped(JSON.serialize(liveChatTranscriptEventObj));
liveChatTranscriptEventMap.remove('LiveChatTranscriptId');
liveChatTranscriptEventObj = (LiveChatTranscriptEvent) JSON.deserialize(JSON.serialize(liveChatTranscriptEventMap), LiveChatTranscriptEvent.class);
liveChatTranscriptEvent = liveChatTranscriptEventObj.clone(false, false, false, false);
liveChatTranscriptEvent.LiveChatTranscript = liveChatTranscriptExternalIdRef;
cloneChatTranscriptEventList.add(liveChatTranscriptEvent);
}
liveChatTranscript = liveChatTranscriptObj.clone(false, false, false, false);
liveChatTranscript.ReportCreateDate__c = liveChatTranscriptObj.CreatedDate;
liveChatTranscript.External_Id__c = externalId;
liveChatTranscript.Status = 'Completed';
cloneChatTranscriptList.add(liveChatTranscript);
}
cloneChatTranscriptList.addAll(cloneChatTranscriptEventList);
if (cloneChatTranscriptList.size() > 0) {
insert cloneChatTranscriptList;
delete chatTranscriptList;
return cloneChatTranscriptList[0].Id;
}