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
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Batch apex is not updating values

Hi, 

  I wrote a batch apex to update custom field in account for some reasons its not working please suggest me what is the issue in this batch apex
 
global class batchAccountUpdate implements Database.Batchable<sObject> {

       public List<Account> parentIds = [SELECT id FROM account where parentid = ''];
       
       public List<Account> results = [SELECT id FROM account  WHERE parentid IN :parentIds];
       
       public List<aggregateResult> minopp = [SELECT id,accountid,min(closedate) closedates from opportunity 
                                             where accountid in (SELECT id FROM account where parentid = '' )
                                             group by id,accountid ]; 


    global Database.QueryLocator start(Database.BatchableContext BC) {
       
       
       return Database.getQueryLocator([SELECT id FROM account  WHERE parentid IN :parentIds]);
     
    }
   
    global void execute(Database.BatchableContext BC, List<Account> scope) 
    {
    
    for (AggregateResult ar : minopp ) 
     {     
   
         for(Account act : scope)
         { 
            
            act.Logo_Since__c   = Date.Valueof(ar.get('closedates')); 
            update act;          
         }
         update scope;
         
     }   
     
    } 
    
    global void finish(Database.BatchableContext BC) {
    }
}


Thanks

Sudhir

Amit Chaudhary 8Amit Chaudhary 8
Please write your logic inside execute method and start method.
global class batchAccountUpdate implements Database.Batchable<sObject> 
{


    global Database.QueryLocator start(Database.BatchableContext BC) 
	{
       public List<Account> parentIds = [SELECT id FROM account where parentid = ''];
       return Database.getQueryLocator([SELECT id FROM account  WHERE parentid IN :parentIds]);
     
    }
   
    global void execute(Database.BatchableContext BC, List<Account> scope) 
    {
        public List<aggregateResult> minopp = [SELECT id,accountid,min(closedate) closedates from opportunity 
                                             where accountid in (SELECT id FROM account where parentid = '' )
                                             group by id,accountid ]; 
    
		for (AggregateResult ar : minopp ) 
		{     
			for(Account act : scope)
			{ 
				act.Logo_Since__c   = Date.Valueof(ar.get('closedates')); 
				update act;          
			}
			update scope;
		}   
     
    } 
    
    global void finish(Database.BatchableContext BC) {
    }
}

 
Waqar Hussain SFWaqar Hussain SF
Try this code
global class batchAccountUpdate implements Database.Batchable<sObject> {
       public List<Account> parentIds = [SELECT id FROM account where parentid = ''];
       public List<Account> results = [SELECT id FROM account  WHERE parentid IN :parentIds];
       public List<aggregateResult> minopp = [SELECT id,accountid,min(closedate) closedates from opportunity 
                                             where accountid in (SELECT id FROM account where parentid = '' )
                                             group by id,accountid ]; 
	List<Account> AccToUpdate = new List<Account>();
    global Database.QueryLocator start(Database.BatchableContext BC) {
       
       return Database.getQueryLocator([SELECT id FROM account  WHERE parentid IN :parentIds]);
     
    }
   
    global void execute(Database.BatchableContext BC, List<Account> scope) 
    {
    
    for (AggregateResult ar : minopp ) 
     {     
   
         for(Account act : scope)
         { 
            
            act.Logo_Since__c   = Date.Valueof(ar.get('closedates')); 
            AccToUpdate.add(act);          
         }
         
     }   
     
    } 
    
    global void finish(Database.BatchableContext BC) {
		if(AccToUpdate.size()>0){
			update AccToUpdate;
		}
    }
}

 
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks for your reply I tried you batch apex for some reasons its not updating the value. I have written a similar logic using trigger its working can you please check and suggest me how to modify the same 

 
trigger update_formula on Account (before insert, before  update) 
{
  
 try 
 {
   Set<Id> accountId = new Set<Id>();
   Datetime temp;
 
   for (Account A : Trigger.New) 
   {     
      accountId.add(A.id);
      }

   //List<Account> parentid = [SELECT parentid FROM account where id IN :accountId];
   
   List<Account> resaccountid = [SELECT id FROM account where id IN :trigger.newmap.keyset() or parentid IN :trigger.newmap.keyset()];
   
   List<aggregateResult> minopp = [SELECT min(closedate) closedates from opportunity  WHERE accountid in :resaccountid];
 
   for (AggregateResult ar : minopp ) 
   {     
   
    
    for (Account Act : Trigger.New) 
     {
       act.Logo_Since__c  =  Date.Valueof(ar.get('closedates')) + 1;
      }
   }
   
   
  }
  
catch(Exception ex){
 /*  for (Account Act : Trigger.New) 
     {
       act.Logo_Since__c  = System.Today();
      } */
 
}  
}

Thanks
Sudhir
Peter Kalina IBMPeter Kalina IBM
Hi Sudhir,

you was updating value that was not selected in scope query. I am potinting out to field Logo_Since__c.

Please past here your last batch class.

Thank you

Peter