-1

this is my class

global class SFA_UpdateAccountBatch implements Database.Batchable<sObject>{
    String strParameter;
    boolean isUpdAccountOwnerbatch;
    String query;
    Boolean isWeeklyBatch;


    public SFA_UpdateAccountBatch(String filterStr){
        strParameter=filterStr;
        isUpdAccountOwnerbatch=true;
        query=SFA_KSUtil.getAccountQueryString(filterStr);
        system.debug('query:'+query);
    }

    public SFA_UpdateAccountBatch(String filterStr,Boolean isWeekly,Boolean isNotrunbatch){
        strParameter=filterStr;
        isWeeklyBatch = isWeekly;  
        isUpdAccountOwnerbatch=isNotrunbatch;      
        query=SFA_KSUtil.getAccountQueryString(filterStr);
        system.debug('query:'+query);
    }

    public SFA_UpdateAccountBatch(String filterStr,boolean isNotrunbatch){
        isUpdAccountOwnerbatch=isNotrunbatch;
        query=SFA_KSUtil.getAccountQueryString(filterStr);
        system.debug('query:'+query+'isUpdAccountOwnerbatch:'+isUpdAccountOwnerbatch);
    }

    global Database.querylocator start(Database.BatchableContext BC){

        return Database.getQueryLocator(query); 
    }

    global void execute(Database.BatchableContext BC, List<account> scope){
       List<Account> accList = new List<Account>();

       for(account acc : scope){
           acc.Named_Account_Name__c='';
           acc.Named_City__c='';
           acc.ZIP_Code_Group__c='';
           accList.add(acc); 
       }

       //update accList;
       Database.update(accList,false);
    }


    global void finish(Database.BatchableContext BC){
         AsyncApexJob a = [SELECT Id, 
                            Status, 
                            NumberOfErrors, 
                            JobItemsProcessed,
                            TotalJobItems, 
                            CreatedBy.Email
                        FROM 
                            AsyncApexJob 
                        WHERE 
                            Id = :BC.getJobId()];                         

       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       String[] toAddresses = new String[] {a.CreatedBy.Email};
       mail.setToAddresses(toAddresses);
       mail.setSubject('Orgnization Name:'+UserInfo.getOrganizationName()+' Orgnization Id :'+UserInfo.getOrganizationId()+' UpdateAccountOwnerBatch batch Status : ' + a.Status);

       mail.setPlainTextBody('UpdateAccountOwnerBatch batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.');
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

       String updAccOwnrBatFilterStr = strParameter;
       if(!isWeeklyBatch){
           strParameter += ' ' +LABEL.SFA_ACCOUNT_BATCH_YESTERDAY_FILTER;
       }

       /*Invoke SFA_UpdateAccountOwner_Batch  */
        if(isUpdAccountOwnerbatch){
            Database.executeBatch(new SFA_UpdateAccountOwner_Batch(strParameter),10); 
        }
    }
}

constructor part is covering but not covering database.querylocator method,execute and finish

This is my test class code

@isTest
public class SFA_UpdateAccountBatch_Test {

    static testMethod void unitTestBatch(){
        String str = 'test';
        User u = SFA_TestFactory_Helper.createTestUser('Sales Engineer');
        test.startTest();
        system.runAs(u) {
            SFA_UpdateAccountBatch objAccbatch = new SFA_UpdateAccountBatch(str);
            SFA_UpdateAccountBatch objAccbatch_two = new SFA_UpdateAccountBatch(str,true);
            SFA_UpdateAccountBatch objAccbatch_three = new SFA_UpdateAccountBatch(str,true,true);

        }
         test.stopTest();
    }
}
0

You need to run your batch to cover the line

test.startTest();
        system.runAs(u) {
            SFA_UpdateAccountBatch objAccbatch = new SFA_UpdateAccountBatch(str);
            SFA_UpdateAccountBatch objAccbatch_two = new SFA_UpdateAccountBatch(str,true);
            SFA_UpdateAccountBatch objAccbatch_three = new SFA_UpdateAccountBatch(str,true,true);
           Database.executeBatch(objAccbatch ); //run the batch for first instance for remaining two create separate method.


        }
         test.stopTest();

But due to standard limit you need to create three separate method to run this batch three times other wise you will get an exception.

  • it give an error message System.QueryException: unexpected token: '&lt;EOF&gt;' – cloudZigZag Oct 21 '16 at 9:26
  • @cloudZigZag thats related to your query. You need to fix that. – Tushar Sharma Oct 21 '16 at 9:27
  • no @tushar_briskmind not solved yet – cloudZigZag Oct 21 '16 at 12:03
  • @cloudZigZag your original issue is related to run the batch which I think thats fixed. After running the batch if you get another error then a new question will be good in that case. – Tushar Sharma Oct 21 '16 at 12:34

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.