When I checked below code in my dev. org. It was almost cover 97% code coverage but when I applied same code in my sandbox it was showing only 10% of code coverage. Strange! Please find below batch class and test class:

global class SendEmailToDueDateTask implements Database.Batchable<sObject>  {
    map<string,list<task>> userEmailTasklistmap = new map<string,list<task>>();

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator([SELECT ID,createddate,what.id,Owner.Email,OwnerId,owner.name,Status,ActivityDate,Subject from Task WHERE Status != 'Completed' and owner.isactive = true and owner.profile.id = '00eD0000001Qs3R']);

    global void execute(Database.BatchableContext BC, List<Task> scope){
        for(Task Tsk : scope){
                userEmailTasklistmap.put(tsk.owner.email, new list<task>());

        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

            for(string email : userEmailTasklistmap.keyset()){

                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                list<string> toAddresses = new list<string>();
                mail.setSubject('Details of tasks due for today');                
                String username = userEmailTasklistmap.get(email)[0].owner.name;
                String htmlBody = '';

                htmlBody = '<table width="100%" border="0" cellspacing="0" cellpadding="8" align="center" bgcolor="#F7F7F7">'+
                              +'<td style="font-size: 14px; font-weight: normal; font-family:Calibri;line-height: 18px; color: #333;"><br />'+
                                   +'<br />'+
                                    +'Dear '+username+',</td>'+
                                +'<td style="font-size: 14px; font-weight: normal; font-family:Calibri; line-height: 18px; color: #333;">Following tasks are pending. Please update the status.</td>'+

                htmlBody +=  '<table border="1" style="border-collapse: collapse"><tr><th>Related To</th><th>Subject</th><th>Created Date</th><th> Due Date</th></tr>';
                for(task tsk : userEmailTasklistmap.get(email)){

                    String duedate = '';
                    if (tsk.ActivityDate != null)
                        duedate = tsk.ActivityDate.format();                    
                        duedate = '';
                    String Subject = tsk.subject;
                    datetime dt = tsk.createddate;
                    string createddate = dt.format('M/d/yyyy');
                    //string what = https://ap2.salesforce.com/tsk.what.id;
                    string View = URL.getSalesforceBaseUrl().toExternalForm()+'/'+ tsk.id;
                    string Clickhere = View;  
                    htmlBody += '<tr><td>' + Clickhere + '</td><td>' + Subject + '</td><td>' + createddate + '</td><td>' + duedate + '</td></tr>';                    
                 htmlBody += '</table><br>';
    global void finish(Database.BatchableContext BC){        

Test Class:


public class SendEmailToDueDateTaskTest { 

static testMethod void testMethod1(){ 

        Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User'];  
        User usr = new User(Alias = 'standt', Email='standarduser@tt.com',  
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',  
        LocaleSidKey='en_US', ProfileId = pro.Id,  
        TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@tt.com'); 

        System.runAs(usr) { 
            Account acc = new Account(); 
                acc.Name = 'Test Account'; 
                insert acc; 
                acc=[SELECT id,Name FROM account WHERE id=:acc.Id]; 
                System.assertEquals(acc.Name,'Test Account'); 
            Task tsk = new Task(); 
                tsk.whatId = acc.Id;  
                tsk.Subject = 'Testing'; 
                tsk.Status = 'In Progress'; 
                tsk.Priority = 'Normal'; 
                tsk.ActivityDate = System.today(); 
                insert tsk ; 
                tsk=[SELECT id,Status FROM Task WHERE id=:tsk.Id]; 
                System.assertEquals(tsk.Status ,'In Progress'); 
                Database.executeBatch (new SendEmailToDueDateTask (),200); 
  • 5
    in your sandbox profile is present or not owner.profile.id = '00eD0000001Qs3R' ? I suggest query the profile Id and use in query . – Ratan Paul Jan 13 '16 at 13:06
  • Yes it is present. I did query the profile ID and tried but still 10% code coverage. – Russel baker 1 Jan 14 '16 at 8:35
  • 1
    Can you just run you batch inside System.runAs(usr) { and insert your usr – Ratan Paul Jan 14 '16 at 9:12
  • Hi, can you please help me to run this batch insight System.runAs (usr) – Russel baker 1 Jan 14 '16 at 9:37
  • 1
    Place Test.StartTest(); Database.executeBatch (new SendEmailToDueDateTask (),200); Test.StopTest(); code inside System.runAs(usr) {} – Ratan Paul Jan 14 '16 at 9:38

First point

in your sandbox profile is present or not owner.profile.id = '00eD0000001Qs3R' ? I suggest query the profile Id and use in query

Second point

Execute your batch inside system run as..

If you execute inside then newly created user have access to your created records

System.runAs(usr) {
Database.executeBatch (new SendEmailToDueDateTask (),200); 
//put system asserts here
| improve this answer | |

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.