Skip to content

Instantly share code, notes, and snippets.

View pgajek2's full-sized avatar

Piotr Gajek pgajek2

View GitHub Profile
@zanstaszek9
zanstaszek9 / ParentWithNestedChildrenUtility.cls
Last active December 23, 2025 14:25
Apex ParentWithNestedChildrenUtility test class. Creates an in-memory SObject with nested related (child) records using JSON serialization and deserialization, without DMLs.
@IsTest
public class ParentWithNestedChildrenUtility {
/**
* @description Creates an in-memory SObject with nested related (child) records using JSON serialization and deserialization, without DMLs.
* @param parentRecord The parent SObject record to receive children.
* @param childrenRecords SObject records to be nested under the parent.
* @param relationshipFieldName The relationship field name on Parent ('Contacts', 'Opportunities', 'Equipment_Maintenance_Items__r'). Using String, as "SObjectField" type cannot be used due to relationship fields on parent-side are not available that way.
* @param populateFakeIds If true, fake Ids will be generated and assigned to parent and child records when their Ids are null.
* @return The deserialized parent SObject with nested children records.