Apex - SoqlTableParser / SoqlMultiTableParser - Example Query

PHOTO EMBED

Mon Aug 12 2024 21:47:43 GMT+0000 (Coordinated Universal Time)

Saved by @Justus

/**
 * /**
 * @author      Justus van den Berg (jfwberg@gmail.com)
 * @date        August 2024
 * @copyright   (c) 2024 Justus van den Berg
 * @license     MIT (See LICENSE file in the project root)
 * @description Test Data generator for SoqlTableParser & SoqlMultiTableParser
 * @note        Create the test data, ideally run 1 by 1 to prevent govenor limits
 *              Run accounts and contacts first. Cases and opportunities require both
 *
 * - https://medium.com/@justusvandenberg/dynamically-handle-salesforce-soql-subquery-response-data-using-apex-8130bd0622aa
 * - https://www.thiscodeworks.com/apex-soqltableparser-soql-subqueries-to-a-single-flat-table/66ba81bc2e62a20014b9150c
 * - https://www.thiscodeworks.com/apex-soqlmultitableparser-soql-subqueries-to-multiple-individual-tables/66ba816f2e62a20014b913aa
 * - https://www.thiscodeworks.com/apex-soqltableparser-soqlmultitableparser-test-data-generator/66ba8543f1197800148899c2
 */
SObject[] parentRecords = [
    SELECT 
        Id, Name,
        (
            SELECT 
                Id, CreatedDate, LastModifiedDate,
                Owner.Name, Owner.Profile.Name, 
                AccountId, FirstName, LastName
            FROM
                Contacts
            WHERE Name LIKE 'LWT - Test - %' ORDER BY Name ASC LIMIT 10
        ),
        (
            SELECT
                Id, CreatedDate, LastModifiedDate,
                Owner.Name, Owner.Profile.Name,
                CaseNumber, Subject, Status 
            FROM 
                Cases
            WHERE Subject LIKE 'LWT - Test - %' ORDER BY Subject ASC LIMIT 10
        ),
        (
            SELECT
                Id, CreatedDate, LastModifiedDate,
                Owner.Name, Owner.Profile.Name,
                Name, StageName, CloseDate,
                (
                    SELECT
                        Id, ContactId , OpportunityId, Opportunity.Account.Id 
                    FROM 
                        OpportunityContactRoles
                    LIMIT 10
                )
            FROM 
                Opportunities
            WHERE Name LIKE 'LWT - Test - %' ORDER BY Name ASC LIMIT 10
        )
    FROM 
        Account 
    WHERE Name LIKE 'LWT - Test - %' ORDER BY Name ASC LIMIT 10
];


// Example for single table (pretty slow 7 seconds for 1000 records)
List<Map<String,Object>> flatTableData = SoqlTableParser.create(
    (Object[]) JSON.deserializeUntyped(JSON.serialize(parentRecords))
);


// Example for multiple tables (Much faster: 2,8 seconds for 1000 records)
Map<String,List<Map<String,Object>>> multiTableData = SoqlMultiTableParser.create(
    (Object[]) JSON.deserializeUntyped(JSON.serialize(parentRecords))
);
content_copyCOPY