+ Start a Discussion
Isha Saxena 9Isha Saxena 9 

I am writing batch which states that is in a Account Allocation rule object "account Manager" field is changed then for every matching value of "look up parameter field" on the object which matches an account then the account owner should be updated.

global class Batch_Account_Manager_change_update implements Database.Batchable<sobject>
{
      List<Account_Allocation_Rules__c> acc_rule = new List<Account_Allocation_Rules__c>();
      string rid = [SELECT id from RecordType where Name ='Tyro Customer'].Id;
      string dynamicQuery = null;
      boolean flag = false;
      Integer num = 0;
      string sg = null;
      Set<ID> setAccRuleId = new Set<ID>();
      List<Account> upacc = new List<Account>();
      List<Account> acct = new List<Account>();
      Set<string> paramlist = new Set<string>();
      Map<String,account> accmap = new map<String,account>();
      
     global Database.QueryLocator start(Database.BatchableContext bc)
    {
        return Database.getQueryLocator('Select Id,Name,RecordType.Id,Active__c,Account_Manager__c,Active_User__c,Industry__c,Integration_Product__c,Lookup_Parameters__c,Rule_Allocation_Name__c,Segment__c from Account_Allocation_Rules__c where LastModifiedDate = Today');
        
    }
    global void execute(Database.BatchableContext bc,List<Account_Allocation_Rules__c> sobj)
    {
             System.debug('Account Allocation Rule'+sobj);
        
           for(Account_Allocation_Rules__c alc : sobj) 
           {
            if(alc.Lookup_Parameters__c != null)
            {
                    paramlist.add(alc.Lookup_Parameters__c);
            }

           }
          acct = [Select Id,OwnerId,Segment__c,Account_Allocation_Parameters__c,Corporate_Group__c from Account where  RecordType.Id =: rid and Corporate_Group__c = null and Key_Account__c = false and Merchant_Group_Member__c = 0 and Account_Allocation_Parameters__c in : paramlist];    
          //acct = Database.query(dynamicQuery);
         
           if(acct.size()>0)
           {        
              for (Account at : acct)
              {
                   accmap.put(at.id, at);
              }
            }

          System.debug('#######Account Map'+accmap);
          
                  System.debug('testing time');        
                    
                    for(account act : accmap.values())
                     {
                        for (Account_Allocation_Rules__c alc : sobj) 
                       {
                           
                           System.debug('########DrillDown');
                           if (act.Account_Allocation_Parameters__c == alc.Lookup_Parameters__c)
                            {
                                 System.debug('########DrillDown111111');
                                  system.debug(LoggingLevel.INFO, alc.Lookup_Parameters__c + ' exist');
                                  System.debug('Account Owner Id####'+act.OwnerId);
                                  System.debug('Allocation Account Manager'+alc.Account_Manager__c);
                                  act.OwnerId = alc.Account_Manager__c ;
                                  upacc.add(act);
             
                              }
                              else
                              {
                                      system.debug(LoggingLevel.INFO, alc.Lookup_Parameters__c + ' does not exist');
                              }
                 }
          }        

         System.debug('Updated Account List*******'+upacc);
         
         
          
                 
        
    }
    
    global void finish(Database.BatchableContext bc)
    {
           
            update upacc;
    }

}
Isha Saxena 9Isha Saxena 9
Please help me it's really urgent
sukanya banekarsukanya banekar
What is the error you are getting? Can you post it.

 
Glyn Anderson 3Glyn Anderson 3
How does your code fail to work?  What should it do that it does not do?
Isha Saxena 9Isha Saxena 9
no error is thrown...it's just showing account list empty... I don't know why....can you help me please...There is no look up or master detail between the two only one field on each should be matched for update.. any help is appreciated
sukanya banekarsukanya banekar
Hi 
use TODAY instead of Today in getquerylocator. and let me know if this work also while executing this batch you should have records in your or where lastmodified date = TODAY

Let me know if this works

Thanks,
Sukanya Banekar

 
Isha Saxena 9Isha Saxena 9
That is not working.. Thanks a lot for your valuable time
sukanya banekarsukanya banekar
Let me know where are you getting list null?(name of the list)
Isha Saxena 9Isha Saxena 9
update account list is null.. I don't know why
sukanya banekarsukanya banekar
Try with below code. Also keep on checking the debug log whether maps are getting null. If maps and update ac list is null then you need to check with data for which you are processing this batch
global class Batch_Account_Manager_change_update implements Database.Batchable<sobject>
{
      List<Account_Allocation_Rules__c> acc_rule = new List<Account_Allocation_Rules__c>();
      string rid = [SELECT id from RecordType where Name ='Tyro Customer'].Id;
      string dynamicQuery = null;
      boolean flag = false;
      Integer num = 0;
      string sg = null;
      Set<ID> setAccRuleId = new Set<ID>();
      List<Account> upacc = new List<Account>();
      List<Account> acct = new List<Account>();
      //Set<string> paramlist = new Set<string>();
      Map<String,account> accmap = new map<String,account>();
      map<String,Id> mapAloocation_ownerId = new map<String,Id>();
     global Database.QueryLocator start(Database.BatchableContext bc)
    {
        return Database.getQueryLocator('Select Id,Name,RecordType.Id,Active__c,Account_Manager__c,Active_User__c,Industry__c,Integration_Product__c,Lookup_Parameters__c,Rule_Allocation_Name__c,Segment__c from Account_Allocation_Rules__c where LastModifiedDate = TODAY AND Lookup_Parameters__c <> NULL' );
        
    }
    global void execute(Database.BatchableContext bc,List<Account_Allocation_Rules__c> sobj)
    {
           System.debug('Account Allocation Rule'+sobj);
           for(Account_Allocation_Rules__c alc : sobj) {
				if(alc.Lookup_Parameters__c != null){
					mapAloocation_ownerId.add(alc.Lookup_Parameters__c,alc.Account_Manager__c);
				}
           }
           if(mapAloocation_ownerId.size()>0)
           {        
              for (Account at : [Select Id,
										OwnerId,Segment__c,Account_Allocation_Parameters__c,Corporate_Group__c from Account where  RecordType.Id =: rid and Corporate_Group__c = null and Key_Account__c = false and Merchant_Group_Member__c = 0 and Account_Allocation_Parameters__c in : mapAloocation_ownerId.keySet()]){
                   accmap.put(at.Account_Allocation_Parameters__c, at);
              }
            }
		System.debug('#######mapAloocation_ownerId'+mapAloocation_ownerId);
          System.debug('#######Account Map'+accmap);
          
                  System.debug('testing time');        
                    
                    for(account act : accmap.values())
                     {
                         
                           System.debug('########DrillDown');
                           if (mapAloocation_ownerId.get(act.Account_Allocation_Parameters__c) == act.Account_Allocation_Parameters__c)
                            {
                                 System.debug('########DrillDown111111');
                                  system.debug(LoggingLevel.INFO, alc.Lookup_Parameters__c + ' exist');
                                  System.debug('Account Owner Id####'+act.OwnerId);
                                  System.debug('Allocation Account Manager'+alc.Account_Manager__c);
                                  act.OwnerId = mapAloocation_ownerId.get(act.Account_Allocation_Parameters__c) ;
                                  upacc.add(act);
             
                              }
                              else
                              {
                                      system.debug(LoggingLevel.INFO, alc.Lookup_Parameters__c + ' does not exist');
                              }
				   }        

         System.debug('Updated Account List*******'+upacc);
         if(upacc.size() >0){
			update upacc;
		 }
         
          
                 
        
    }
    
    global void finish(Database.BatchableContext bc)
    {
           
            
    }

}

let me know if this works.
Isha Saxena 9Isha Saxena 9
@sukanya banekar : Thanks a lot for all your help and valuable efforts... but it still didn't work...:( I am unable to update my accounts