+ Start a Discussion
Zoom_VZoom_V 

Batch code not updating fields

I am attempting to populate the UPName__c field with the Parent ID of all of my Account records whose Parent has the X2_0_Ultimate_Parent_c field checked. I have put together this batch code : 

global class UltParentName implements Database.Batchable<sObject>{        
        //string query;
        string query = 'SELECT Id, Name,ParentID FROM Account WHERE Parent.X2_0_Ultimate_Parent__c = True';

        /*global UltParentName (string myQuery){      
            query = myQuery;        
        } */

        global Database.QueryLocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);
        }

        global void execute(Database.BatchableContext BC,List<Account> scope) {
             List<Account> accns = new List<Account>();
             for(Account a : scope){
                   //Account a = (Account)s;
                   if(a.Parent.X2_0_Ultimate_Parent__c==True){
                         a.UPName__c=a.ParentID;
                         accns.add(a);
                   }
             }                  
             update accns;          
        }
        global void finish(Database.BatchableContext BC)
        {     //System.debug(LoggingLevel.WARN,'Batch Job Complete');
        }       
    }

 

I am running the following code through an Execute Anonymous in the developer console : 

UltParentName getuprecs = new UltParentName();
Database.executeBatch(getuprecs);

 When I run this in Executable Anonymous it runs through but it doesn't populate the UPName__c field with the Parent.ID. 

Can anybody see anything wrong with this ? 

Thank you very much.

 

Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy
Try this.. 
global class UltParentName implements Database.Batchable<sObject>{        
        //string query;
        string query = 'SELECT Id, Name,ParentID FROM Account WHERE Parent.X2_0_Ultimate_Parent__c = True';
 
        /*global UltParentName (string myQuery){      
            query = myQuery;        
        } */
 
        global Database.QueryLocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);
        }
 
        global void execute(Database.BatchableContext BC,List<Account> scope) {
             List<Account> accns = new List<Account>();
             for(Account a : scope){
                   //Account a = (Account)s;
                   //if(a.Parent.X2_0_Ultimate_Parent__c==True){
                         a.UPName__c=a.ParentID;
                         accns.add(a);
                   //}
             }                  
             update accns;          
        }
        global void finish(Database.BatchableContext BC)
        {     //System.debug(LoggingLevel.WARN,'Batch Job Complete');
        }       
    }

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy
Try this.. 
global class UltParentName implements Database.Batchable<sObject>{        
        //string query;
        string query = 'SELECT Id, Name,ParentID FROM Account WHERE Parent.X2_0_Ultimate_Parent__c = True';
 
        /*global UltParentName (string myQuery){      
            query = myQuery;        
        } */
 
        global Database.QueryLocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);
        }
 
        global void execute(Database.BatchableContext BC,List<Account> scope) {
             List<Account> accns = new List<Account>();
             for(Account a : scope){
                   //Account a = (Account)s;
                   //if(a.Parent.X2_0_Ultimate_Parent__c==True){
                         a.UPName__c=a.ParentID;
                         accns.add(a);
                   //}
             }                  
             update accns;          
        }
        global void finish(Database.BatchableContext BC)
        {     //System.debug(LoggingLevel.WARN,'Batch Job Complete');
        }       
    }
This was selected as the best answer
Zoom_VZoom_V

Thanks Sam !