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
Lakshmi SLakshmi S 

Batch Apex Mass Upload Issue ?

Hi Team,

Batch Apex Mass Upload Issue ?
Requirement : Need to Update Account fileds from Custom object based on condition.For 1 record we need to update around 5000 Account records with some value.
How can we achieve this request ?
I have tried with trigger and batch apex, for 1 record it is working. But mass upload it is not working.

Please advise any one.

Thanks,
Lakshmi S.
GauravendraGauravendra
Hi Lakshmi,

Can you check whether you are passing entire list of records from trigger i.e.(Trigger.New)
Pass this list of records to the batch where you are performing the update.

Hope that helps!
Lakshmi SLakshmi S
Hi Gauravendra

Thanks for your reply.
I am passing the list of records from Trigger.New,
how to pass the records directly with out using the trigger ?

Thanks,
Lakshmi S
GauravendraGauravendra
Hi Lakshmi,

You can try fetching the required account you need to update in Batch class start() method by query.
Use that list in execute() to perform desired requirement.
Can you provide more information about requirement.

Hope that helps!
Lakshmi SLakshmi S
Hi Gauravendra,

I have a custom object "A", if i did any CRUD operation on this object i have to update the Account records based on condition.But there is no relation ship for these two objects. Present i am using trigger for updating the Account records whenever CRUD operation done on Custom Object "A".
In this custom object "A" we have a multipicklist fields called Name & Division .
In Account object also we have a Division field. If we create or update or delete record from custom object "A", we need to check the account object if any division matches in account we need to update the "username" field in all those accounts.(we have min 4k records).
How can we achieve this requirement.

Thanks,
Lakshmi S.
GauravendraGauravendra
Hi Lakshmi,

One way to achieve this is with trigger, in this case with every CRUD operation of object "A" syncronously Account will get updated.
From the trigger of object A, instantiate the batch class constructor by passing the Trigger.newMap. 

Define a batch class with constructor that will instantiate the Map<Id,ObjectA__c> variable, which would later used in the start() to fetch the required object A records.
Trigger will look something like this:
ExapleBatch expBatch = new ExapleBatch(Trigger.newMap);
Id batchId = Database.executeBatch(expBatch);
Batch class would look something like this:
global class ExapleBatch implements Database.Batchable<sObject>,Database.Stateful {

Map<Id,ObjectA__c> mapRecordA = new Map<Id,ObjectA__c>();

global ExapleBatch(Map<Id,ObjectA__c> trigMapRecord) {
mapRecordA = trigMapRecord;
}

global Database.QueryLocator start(Database.BatchableContext bc) {    
return Database.getquerylocator([SELECT Id,Name FROM ObjectA__c WHERE Id IN: mapRecordA.keySet()]);

}
global void execute(Database.BatchableContext bc, List<ObjectA__c> records){	
//match the records of objectA with Account
}
}
In the start(), fetch the records which just got created/updated and pass it to execute().
In the execute(), get the desired account records and update it.
And second way is if you want to avoid the trigger on Object A, is run a batch class that will fetch all the records that got created or updated in a day and run this batch class on a daily basis.

Hope this helps!
Lakshmi SLakshmi S
Hi Gauravendra,

Thanks for your reply.
It is very helpful.

Thanks,
Lakshmi S.