[SalesForce] Batch code doesn’t update records

EDIT : I have updated my code in here.

I am attempting to see if an Account's parent has the X2_0_Ultimate_Parent_c field populated. If it does then I would like to populate the UPName_c field in the child with the parent ID field.

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 from an Execute Anonymous window in Dev Console :

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

When I run this from Execute Anonymous it runs successfully but it does not make the updates. Is that not possible when running the code through Execute Anonymous ? If not, how can I properly run the batch class ? Do you notice anything wrong with the code ?

Thank you for any help you can give. I really appreciate it.

Best Answer

Your class actually looks pretty good. You need to add a finish method. The finish method is called once the batches have all been processed. The finish method is used to send confirmation emails or execute post-processing operations. Here, I have just put in a system.debug() message.

Try something like this

global class UltParentName implements Database.Batchable<sObject>{        
        global final string query;

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

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

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

In Execute anon

UltParentName getuprecs = new UltParentName();
ID batchprocessid = Database.executeBatch('SELECT Id, Name, Parent.Name FROM Account WHERE Parent.UltimateParentYes__c = 'Yes');