ds


List<Map<String, String>> objectQueriesList = new List<Map<String, String>>{

    new Map<String, String>{'Account' => 'SELECT Id, Name FROM Account'},

    new Map<String, String>{'Contact' => 'SELECT Id, FirstName, LastName FROM Contact'},

    // Add more maps as needed

};


CustomerDeletionBatch initialBatch = new CustomerDeletionBatch(objectQueriesList);

Database.executeBatch(initialBatch);




 global class CustomerDeletionBatch implements Database.Batchable<sObject>, Database.Stateful {


    private List<Map<String, String>> objectQueriesList;

    private Integer currentIndex;


    global CustomerDeletionBatch(List<Map<String, String>> objectQueriesList) {

        this.objectQueriesList = objectQueriesList;

        this.currentIndex = 0;

    }


    global Database.QueryLocator start(Database.BatchableContext BC) {

        String query = getQueryFromList();


        if (query != null) {

            System.debug('query' + query);

            return Database.getQueryLocator(query);

        }


        return null;

    }


    global void execute(Database.BatchableContext BC, List<sObject> scope) {

        for (sObject record : scope) {

            System.debug('Record ID: ' + record.Id);

            // Add your custom logic here to process the records

        }

    }


    global void finish(Database.BatchableContext BC) {

        if (currentIndex < objectQueriesList.size()) {

            String nextQuery = getQueryFromList();


            if (nextQuery != null) {

                // Call the batch again with the next set of object queries

                CustomerDeletionBatch nextBatch = new CustomerDeletionBatch(objectQueriesList);

                Database.executeBatch(nextBatch);

                System.debug('nextBatch' + nextBatch);

            }

        }

    }


    private String getQueryFromList() {

        if (objectQueriesList != null && currentIndex < objectQueriesList.size()) {

            Map<String, String> currentObjectQueries = objectQueriesList[currentIndex];

            List<String> parentObjectApiNames = new List<String>(currentObjectQueries.keySet());


            if (parentObjectApiNames.size() > 0) {

                String parentObjectApiName = parentObjectApiNames[0];

                System.debug('parentObjectApiName' + parentObjectApiName);

                currentIndex++;

                return currentObjectQueries.get(parentObjectApiName);

            }

        }


        return null;

    }

}

=================================

global class DynamicBatchClass implements Database.Batchable<SObject>, Database.Stateful {
    private List<Type> objectTypes;
    private Integer currentObjectTypeIndex;
    private List<String> csvLines;

    global DynamicBatchClass(List<Type> objectTypes) {
        this.objectTypes = objectTypes;
        this.currentObjectTypeIndex = 0;
        this.csvLines = new List<String>();
    }

    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = getDynamicQuery();
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<SObject> scope) {
        // Process the current batch of records
        for (SObject record : scope) {
            // Your processing logic here

            // Example: Convert selected fields to a CSV line
            String csvLine = String.valueOf(record.get('Id')) + ',' + String.valueOf(record.get('Name'));
            csvLines.add(csvLine);
        }
    }

    global void finish(Database.BatchableContext BC) {
        // Prepare and save the CSV file in the finish method
        if (!csvLines.isEmpty()) {
            String csvContent = String.join(csvLines, '\n');
            Blob csvBlob = Blob.valueOf(csvContent);

            // Create a ContentVersion record to store the CSV file
            ContentVersion contentVersion = new ContentVersion(
                VersionData = csvBlob,
                Title = 'BatchOutput.csv',
                PathOnClient = 'BatchOutput.csv',
                FirstPublishLocationId = UserInfo.getUserId()
            );
            insert contentVersion;
        }

        // Continue to the next object type if available
        if (++currentObjectTypeIndex < objectTypes.size()) {
            DynamicBatchClass nextBatch = new DynamicBatchClass(objectTypes);
            Database.executeBatch(nextBatch);
        }
    }

    private String getDynamicQuery() {
        // Dynamically generate the query based on the current object type
        Type currentObjectType = objectTypes[currentObjectTypeIndex];
        String objectName = String.valueOf(currentObjectType);
        return 'SELECT Id, Name FROM ' + objectName + ' WHERE CreatedDate >= LAST_N_DAYS:7';
    }
}

Post a Comment

0 Comments