You need to sign in to do that
Don't have an account?

How to use a batch apex
Hi,
I created a Batch Apex but I guess im using it wrong because I get the error:
System.Exception: Attempted to schedule too many concurrent batch jobs in this org
I have a trigger that when the field Team__c in Users is modified, I have to retrieve all the accounts from this user and update a field Area__c in the account.
This is the trigger code where I check if the field Team__c has changed and if changed, send it the the apex batch to update all the accounts:
trigger UpdateKAMandCommAreawhenModUser on User (after update) { Set<id> ownerIds = new Set<id>(); Map<id, User> owners = new Map<id, User>(); for (Integer i=0;i<Trigger.new.size();i++){ if (Trigger.new[i].Team__c!=Trigger.old[i].Team__c){ Database.executeBatch(new AccountAreaReassignment(Trigger.new[i].Team__c,Trigger.new[i].Id)); } }
And this is my batch:
global class AccountAreaReassignment implements Database.Batchable<sObject>{ //Receiving Area and Id of the user String Area{get;set;} Id UserId{get;set;} global AccountareaReassignment(String Area,Id UserId){ this.Area=Area; this.UserId=UserId; }
global Database.QueryLocator start(Database.BatchableContext BC) { return DataBase.getQueryLocator([SELECT Id,Area__c FROM account WHERE OwnerId=:UserId]); } global void execute(Database.BatchableContext BC,List<Account> scopeAcc) { for (Integer i=0;i<scopeAcc.size();i++){ scopeAcc.get(i).Area__c=Area; } update scopeAcc; } global void finish(Database.BatchableContext BC) { //Send an email to the User after your batch completes Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {'a.tejado@hotelbeds.com'}; mail.setToAddresses(toAddresses); mail.setSubject('Apex Batch Job is done'); mail.setPlainTextBody('The batch Apex job processed '); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } }
I want to change my code in order to send to the Batch the list of User Id's that have been modified. But then I dont know how to work with that list in the batch, could someone help please?
Thanks a lot!
Antonio
You are close, just need to put evertyhing into the lists like this:
Good luck!
All Answers
You are close, just need to put evertyhing into the lists like this:
Good luck!
Thank you so much!!