function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Salesforce Dev199Salesforce Dev199 

Pass 10000 records from apex class to batch apex

How can we pass 10000 records from Apex class to batch class and update 10000 records of Sobect type lead & contact Opps.

I'm trying to change the owner of 10000 records. so I've created a lwc, where I'll take the user from 1st dropdown  of which user record will need to transfer, 2nd drop down will be to which user it should transfer.

Let me know how i can achieve this
abel reegan 28abel reegan 28
* Don't try to pass the records from the apex class to the batch class.
* Use the apex class to initiate the batch class and set params.
* Inside the batch handle the logic.

The below example will help

public class MyBatchClass implements Database.Batchable<SObject> {
    private String user1;
    private String user2;

    public MyBatchClass(String param1, String param2) {
        this.user1 = param1;
        this.user2 = param2;
    }

    public Database.QueryLocator start(Database.BatchableContext batchableContext) {
        String query = 'SELECT Id, Name FROM Opportunity WHERE ownerid in:user1';
        return Database.getQueryLocator(query);
    }

    public void execute(Database.BatchableContext batchableContext, List<SObject> scope) {
        // transfer the record to 2nd user
    }

    public void finish(Database.BatchableContext batchableContext) {
        // Your logic after the batch execution is complete
    }
}

public class apexClass {
    public static void callBatchWithParams() {
        String myParam1 = 1st user's id;
        String myParam2 = 2nd user's id;

        MyBatchClass batchInstance = new MyBatchClass(myParam1, myParam2);
        Integer batchSize = 200;

        // Start the batch job
        Database.executeBatch(batchInstance, batchSize);
    }
}
 
SubratSubrat (Salesforce Developers) 
Hello ,

To update 10,000 records of different object types (Lead, Contact, Opportunity) and transfer ownership from one user to another, you can follow these steps:

Create a Lightning Web Component (LWC) that allows users to select the source user and target user to transfer ownership.
Use Apex to query the records that need to be updated. Depending on your use case, you might need to query Leads, Contacts, and Opportunities separately based on the selected users.
Pass the queried records to a Batch Apex class. To do this, you can create a custom Apex class that implements the Database.Batchable interface. The Batch Apex class will process the records in batches, allowing you to handle large volumes of data efficiently.
Here's a general outline of how to proceed:

LWC HTML (transferOwnershipLWC.html):
<template>
    <lightning-combobox label="Select Source User" options={sourceUsers} onchange={handleSourceUserChange}></lightning-combobox>
    <lightning-combobox label="Select Target User" options={targetUsers} onchange={handleTargetUserChange}></lightning-combobox>
    <lightning-button label="Transfer Ownership" onclick={transferOwnership}></lightning-button>
</template>
LWC JavaScript (transferOwnershipLWC.js):
import { LightningElement, track } from 'lwc';
import getUsers from '@salesforce/apex/TransferOwnershipController.getUsers';
import transferOwnershipBatch from '@salesforce/apex/TransferOwnershipController.transferOwnershipBatch';

export default class TransferOwnershipLWC extends LightningElement {
    @track sourceUsers = [];
    @track targetUsers = [];
    selectedSourceUser;
    selectedTargetUser;

    connectedCallback() {
        getUsers()
            .then(data => {
                this.sourceUsers = data;
                this.targetUsers = data;
            })
            .catch(error => {
                console.error('Error fetching users: ', error);
            });
    }

    handleSourceUserChange(event) {
        this.selectedSourceUser = event.detail.value;
    }

    handleTargetUserChange(event) {
        this.selectedTargetUser = event.detail.value;
    }

    transferOwnership() {
        if (!this.selectedSourceUser || !this.selectedTargetUser) {
            // Show an error message or validation to select both users
            return;
        }

        transferOwnershipBatch({ sourceUser: this.selectedSourceUser, targetUser: this.selectedTargetUser })
            .then(result => {
                // Handle success
                console.log('Ownership transferred successfully.');
            })
            .catch(error => {
                // Handle error
                console.error('Error transferring ownership: ', error);
            });
    }
}
Apex (TransferOwnershipController.cls):
public class TransferOwnershipController {
    @AuraEnabled(cacheable=true)
    public static List<User> getUsers() {
        return [SELECT Id, Name FROM User];
    }

    @AuraEnabled
    public static void transferOwnershipBatch(Id sourceUser, Id targetUser) {
        // Query the records that need to be updated (Leads, Contacts, Opportunities)
        // Perform updates here and update the owner field
        // You can use Database.update to perform the updates
        // Consider implementing Database.Batchable and Database.Stateful interfaces for the batch class
    }
}

Hope this helps !
Thank you.
Salesforce Dev199Salesforce Dev199
Hey @abel reegan 28

Thank you for your response, but in my case, there are multiple subjects I want to transfer, (lead contact oppo and task event ), so can we query in the start of batch?