+ Start a Discussion
Soubhagya Ranjan 2Soubhagya Ranjan 2 

batch apex excecuted successfully but field is not updating

requirement is when we insert record into account object then status field will be success and if it find duplicate then status will be error .
below is my code . it is executong but fields are not updating .

global class insertrecord implements Database.Batchable<sObject>, Database.Stateful {
String Query = 'select name , phone, fax, BillingStreet from Account ';
global Database.QueryLocator start(Database.BatchableContext BC)  {
      return Database.getQueryLocator(query);
      }
global void execute(Database.BatchableContext BC, List<sObject> scope) {
       List<Account > AccList = [Select name,BillingStreet,fax from Account ];
         Set<string> name= new Set<string>();
         Set<string> BillingStreet= new Set<string>();
         Set<string> fax= new Set<string>();
        for(Account Acc : AccList)
         {
           name.add(Acc.name );
           BillingStreet.add(Acc.BillingStreet);
           fax.add(Acc.fax);
         } 
       List<Account > duplicateAccountList = [Select name,BillingStreet from Account where name=:name and BillingStreet=:BillingStreet];
       List<Account > duplicateAccountList1 = [Select name,fax from Account where name=:name and fax=:fax];
       Set<string > duplicateAccountIds = new Set<string >();
       for(Account dup: duplicateAccountList )
       {
         duplicateAccountIds.add(dup.name);
         duplicateAccountIds.add(dup.BillingStreet);  
       }
       for(Account a : duplicateAccountList )
       {
            if(a.name!=null & a.BillingStreet!=null )
            {
               if(duplicateAccountIds.contains(a.name) & duplicateAccountIds.contains(a.BillingStreet))
               {
                 a.status1__c = 'Error';
               }
               else{
               a.status1__c = 'Success';
               }
            }
       }
             for(Account dup: duplicateAccountList1 )
       {
         duplicateAccountIds.add(dup.name);
         duplicateAccountIds.add(dup.fax);  
       }
       for(Account a1 : duplicateAccountList1 )
       {
            if(a1.name!=null & a1.fax!=null )
            {
               if(duplicateAccountIds.contains(a1.name) & duplicateAccountIds.contains(a1.fax))
               {
                 a1.status1__c = 'Duplicate';
               }
            }
       }       
}
global void finish(Database.BatchableContext BC)
    {

    }

}




TRIGGER :

trigger testbatch on Account (before insert) {

Database.executeBatch(new insertrecord());


}