4

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.

0
5

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');
4
  • When calling the batch class, make sure you query for the Parent.Name field, otherwise it will probably come through as null. – Jeremy Nottingham Sep 26 '13 at 14:15
  • Thank you so much for your input Dunc44 ! You are always so much help. I have taken your code and made some changes. I've updated my original post to show the current batch code and the executable I am using. The code seems to be running properly when I execute anonymously but it is not updating the field. Also - I am now passing the Parent.ID, not the Parent.Name. I just wanted to mention that since @JeremyNottingham mentioned using the Parent.Name. Do either one of you guys have an idea why that is not updating the UPName field ? It's just a text field. – Zoom_v Sep 26 '13 at 18:33
  • it looks like you have this line commented out: //Account a = (Account)s; That should not be commented out. – Chris Duncombe Sep 26 '13 at 18:37
  • Oh yes, and your for loop is incorrect. You are looking for Accounts in the scope, the scope is not comprised of Accounts but sObjects. Your foor loop should look like this for(Sobject s : scope){, and hten uncomment the line i indicated above – Chris Duncombe Sep 26 '13 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.