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
K_devK_dev 

Convert trigger to calling a batch class

Hi 

 

Can anyone help me in converting this trigger into calling a batch class.

 

As it is not sufficient for our bulk loading of 4 millions data.

 

Here is the trigger:

 

trigger createacc on Contact (after insert) {
list<Contact > con=[select id,firstname ,lastname from contact where id in:Trigger.newMap.keySet()];
list<account >acc =new list<account>();
list<contact>cc1 =new list<contact>();
for(contact c : con)
{
account a =new account ();

a.name =  c.FirstName != null ? c.FirstName + ' ' + c.LastName : c.LastName;
acc.add(a);

}
insert acc;
for(account a1:acc){
system.debug('---------------------this is id --------------------'+a1.id);
for(contact c : con){
c.accountid=a1.id;
cc1.add(c);
}
}
update cc1;
}

 

 

I have a format please update me exact correct code works for my scenario:

 

trigger insertAccount on Contact (after insert) {
Map<id, Contact> contacts = new Map<id, Contact>();
for (Integer i=0;i<Trigger.new.size();i++) {

contacts.put(Trigger.new[i].Id, Trigger.new[i]);

}
// You can execute batch apex using trigger using below codes
if (contacts.size() > 0) {
Database.executeBatch(new InsertAccounts(contacts));
}
}

 

global class InsertAccounts implements Database.Batchable<sObject> {
//map of contactid - contact
Map<Id, Contact> contactMap = new Map<Id, Contact>();
global InsertAccounts(Map<Id, Contact> contacts) {
ContactMap = contacts;
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return DataBase.getQueryLocator([SELECT Id,FirstName, LastName,AccountId FROM Contact WHERE accountID IN : contactMap.keySet()]);
}
global void execute(Database.BatchableContext BC,List<Account> scopeAcc) {

for (Integer i=0;i<scopeAcc.size();i++){
scopeAcc.get(i).Area__c=ownerMap.get(scopeAcc.get(i).OwnerId).Team__c;
}
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[] {'info@theblogreaders.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Batch Job is done');
mail.setPlainTextBody('The batch Apex Job Processed Successfully');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Subhash GarhwalSubhash Garhwal

try this one 

 

trigger insertAccount on Contact (after insert) {
Map<id, Contact> contacts = new Map<id, Contact>([Select Id, FirstName, LastName, Name From Contact Where Id IN : Trigger.newMap.keySet()]);


// You can execute batch apex using trigger using below codes
if (contacts.size() > 0)
Database.executeBatch(new InsertAccounts(contacts));
}

 

global class InsertAccounts implements Database.Batchable<sObject> {

//map of contactid - contact

Map<Id, Contact> contactMap = new Map<Id, Contact>();

global InsertAccounts(Map<Id, Contact> contacts) {
ContactMap = contacts;
}

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

return DataBase.getQueryLocator([SELECT Id,FirstName, LastName,AccountId, Name FROM Contact WHERE AccountId = null AND Id IN : contactMap.keySet()]);
}
global void execute(Database.BatchableContext BC,List<Contact> scope) {

List<Account> accounts = new List<Account>();

Map<String,Contact> mapContacts = new Map<String,Contact>();

for (Contact con : scope){

Account acc = new Account();
acc.Name = con.Name;

accounts.add(acc);

mapContacts.put(con.Name, con);
}

if(accounts.size() > 0)
insert accounts;


List<Contact> conts = new List<Contact>();

for(Account acc : [Select Id, Name From Account Where Name IN : mapContacts.keySet()]) {

if(mapContacts.containsKey(acc.Name)) {

mapContacts.get(acc.Name).AccountId = acc.Id;
conts.add(mapContacts.get(acc.Name));
}
}

if(conts.size() > 0)
update conts;

}
global void finish(Database.BatchableContext BC) {

}
}

 

use Email in finish method if you want send mail to any one

 

Thanks

 

Hit the Kudos button (star)  and Mark as solution if it post helps you 

All Answers

Subhash GarhwalSubhash Garhwal

try this one 

 

trigger insertAccount on Contact (after insert) {
Map<id, Contact> contacts = new Map<id, Contact>([Select Id, FirstName, LastName, Name From Contact Where Id IN : Trigger.newMap.keySet()]);


// You can execute batch apex using trigger using below codes
if (contacts.size() > 0)
Database.executeBatch(new InsertAccounts(contacts));
}

 

global class InsertAccounts implements Database.Batchable<sObject> {

//map of contactid - contact

Map<Id, Contact> contactMap = new Map<Id, Contact>();

global InsertAccounts(Map<Id, Contact> contacts) {
ContactMap = contacts;
}

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

return DataBase.getQueryLocator([SELECT Id,FirstName, LastName,AccountId, Name FROM Contact WHERE AccountId = null AND Id IN : contactMap.keySet()]);
}
global void execute(Database.BatchableContext BC,List<Contact> scope) {

List<Account> accounts = new List<Account>();

Map<String,Contact> mapContacts = new Map<String,Contact>();

for (Contact con : scope){

Account acc = new Account();
acc.Name = con.Name;

accounts.add(acc);

mapContacts.put(con.Name, con);
}

if(accounts.size() > 0)
insert accounts;


List<Contact> conts = new List<Contact>();

for(Account acc : [Select Id, Name From Account Where Name IN : mapContacts.keySet()]) {

if(mapContacts.containsKey(acc.Name)) {

mapContacts.get(acc.Name).AccountId = acc.Id;
conts.add(mapContacts.get(acc.Name));
}
}

if(conts.size() > 0)
update conts;

}
global void finish(Database.BatchableContext BC) {

}
}

 

use Email in finish method if you want send mail to any one

 

Thanks

 

Hit the Kudos button (star)  and Mark as solution if it post helps you 

This was selected as the best answer
K_devK_dev

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY:insertAccount: System.LimitException: Attempted to schedule too many concurrent batch jobs in this org (limit is 5).

 

Iam trying to insert 20K records facing this error only 800 got inserted?