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
SF DEVSF DEV 

batch not executing, called from trigger

I want check for duplicate id getting inserted in account, I have called batch class from trigger.

 

trigger id with its name need to pass to batch class, and chek against all account if the record with name already existing.

 

But my batch not executing, below is my code 

 

 

global class DuplicateAccount implements Database.Batchable<sObject>
{
Public integer leadcount{get;set;}
global  String Query;
set<id> ACCTIDS=new set<id>();
global account []A {get;set;}
list<String> MainAccounts = new list<String >();
 global DuplicateAccount (list<string> listAccounts)
    {   
    system.debug('daaaaaa'+listAccounts);
        MainAccounts = listAccounts;
         system.debug('mmmmmmmmmaaaaaaaaaaaaa'+MainAccounts );
    }

global Database.QueryLocator start(Database.BatchableContext BC)
{ system.debug('ffffffffffffff');
            query='SELECT Id FROM account WHERE name IN: MainAccounts';
            system.debug('mmmmmmmmmmmmmmmmmmmm'+MainAccounts);
            return Database.getQueryLocator(query);

}


global void execute(Database.BatchableContext BC, List<Account> scope)
{system.debug('aaaaaaaaaaaaaaaaaaaa');
 Account []aname=[select name from account where name in: MainAccounts ];
 for(account ac: scope){
    if(ac.name==aname[0].name){system.debug('dssdcsdc');}
 }
 }

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[] {'sforce2009@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Batch Job is done');
mail.setPlainTextBody('The batch Apex job processed ');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

Rahul SharmaRahul Sharma
Check if you getting any exception in your batch class or whether you have called the batch class properly.
And also ensure your trigger is active.
SF DEVSF DEV

No error. 

 

Just execute method is not calling.

Trigger is active.

 

 

Rahul SharmaRahul Sharma
Great, That could mean that you have not called the batch class properly.
Could you post the triggers code from where you are calling this batch?
SF DEVSF DEV
trigger DuplicateAccount on Account (after insert) {
     Set<Id> OppIds1= new Set<Id>();
     List <string> OppIds=new List <string>();
     if(Trigger.isInsert){
        for(Account o:Trigger.New){
              OppIds.add(o.name);
         }
         system.debug('iiiiiiiiiii'+OppIds);
           //DuplicateAccount PSobj = new DuplicateAccount();
           Database.executeBatch(new DuplicateAccount(OppIds));
           //Integer validationId=PSobj.deleteAccounts(OppIds);
}
}
 
I was doing wrkg as above...
Rahul SharmaRahul Sharma
You are passing, Set of String from Trigger to List of String in the Batch. Try again with matching up the datatypes of both.