Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
Error: 

Attachmentstest

Fail

System.NullPointerException: Argument 1 cannot be null

Class.sendEmailtoVolntr.start: line 18, column 1

Main Class:

 

global class sendEmailtoVolntr implements schedulable,Database.Batchable<sObject>,Database.Stateful

{

public sendEmailtoVolntr(String c) {

}

global String Query = null;

global sendEmailtoVolntr (){

Query='Select Id,Name,For_Centre__c,For_Date__c,Leap_Centre__c,Leap_Centre__r.Name,Send_Email__c,Short_Description__c,Uploaded_By__c from Leap_Weekly_Curriculum__c where Send_Email__c = FALSE and For_Date__c >= TODAY';

}

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

return Database.getQueryLocator(Query);

}

global void execute(SchedulableContext SC){

sendEmailtoVolntr sendEmail= new sendEmailtoVolntr ();

database.executebatch(sendEmail,1);

}

global void execute(Database.BatchableContext BC, List<sObject> scope){

List<Attachment> attachments = new List<Attachment> ();

Map<Id,Attachment> attachmentId = new Map<Id,Attachment> ();

Set<Id> LeapIds = new Set<Id> ();

List<Leap_Weekly_Curriculum__c> leapCurrilist = new List<Leap_Weekly_Curriculum__c> ();

List<Leap_Weekly_Curriculum__c> updateLeapList = new List<Leap_Weekly_Curriculum__c> ();

for(sObject so : scope){

Leap_Weekly_Curriculum__c leapCurri= (Leap_Weekly_Curriculum__c)so;

LeapIds.add(leapcurri.Id);

leapCurrilist.add(leapCurri);

}

attachments = [SELECT Id,ParentId, Name, Body, ContentType FROM Attachment WHERE Parentid IN :LeapIds];

//Assuming that there will be only one attachment for each Leap Curriculum record

for(Attachment att : attachments)

{

attachmentId.put(att.ParentId,att);

}

attachments.clear();

for(Leap_Weekly_Curriculum__c LWC: leapCurrilist)

{

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

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

List<Messaging.EmailFileAttachment> emailAttachments = new List<Messaging.EmailFileAttachment>();

Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();

List<String> toAddresses = new List<String> ();

String emailErrorReport = null;

for(Volunteers__c vol: [Select Email__c from Volunteers__c WHERE Centre_Allocation__c includes(:LWC.Leap_Centre__r.Name)])

toAddresses.add(vol.Email__c);

//List<Volunteers__c> volListTeamp = [Select Email__c from Volunteers__c WHERE Centre_Allocation__c includes(:LWC.Leap_Centre__r.Name)];

system.debug(toAddresses);

efa.setFileName(attachmentId.get(LWC.Id).Name);

efa.setBody(attachmentId.get(LWC.Id).Body);

efa.setContentType(attachmentId.get(LWC.Id).ContentType);

emailAttachments.add(efa);

if(emailAttachments.size() > 0)

{

email.setToAddresses(toAddresses);

email.setSubject('test');

email.setPlainTextBody('For Leap Curriculum : ID - ' + LWC.Id + ' Name - ' + LWC.Name + ' Short Description - ' + LWC.Short_Description__c + '.');

email.setFileAttachments(emailAttachments);

theEmails.add(email);

}

if(theEmails.size() > 0)

{

Messaging.SendEmailResult[] results = Messaging.sendEmail(theEmails,false);

Messaging.SendEmailError[] errors = new List<Messaging.SendEmailError>();

for( Messaging.SendEmailResult currentResult : results )

{

errors = currentResult.getErrors();

if( null != errors )

{

for( Messaging.SendEmailError currentError : errors )

{

emailErrorReport = emailErrorReport + '(' + currentError.getStatusCode() + ')' + currentError.getMessage() + '\r' ;

}

}

}

system.debug('error : ' + emailErrorReport);

}

System.debug('HEAP_SIZE Used:'+ Limits.getHeapSize());

if(emailErrorReport != null){

break;

}

else

{

LWC.send_EMail__c = TRUE;

updateLeapList.add(LWC);

}

if(updateLeapList.size()>0)

update updateLeapList;

if(emailErrorReport != null){

System.abortJob(BC.getJobId());

List<sObject> so = scope;

Leap_Weekly_Curriculum__c lwcr= (Leap_Weekly_Curriculum__c )so[0];

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

String[] toAddresseser = new String[] {'rahmeshms@outlook.com'};

mail.setToAddresses(toAddresseser);

mail.setSubject('Email Job is aborted');

mail.setPlainTextBody('The send email job is aborted due to Leap Curriculum : '+lwcr.Name+' '+lwcr.Short_Description__c+ ' '+lwcr.Id+ 'Please check the error report and take action accordingly.\n\n\t Error - '+emailErrorReport);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}

}

}

global void finish(Database.BatchableContext BC){

}

}

Test Class: 

 

@isTest

public with sharing class Attachmentstest{

public static testmethod void Attachmentstest()

{

Leap_Weekly_Curriculum__c record = new Leap_Weekly_Curriculum__c();

Attachment attachment1 = new Attachment();

Leap_Centre__c lcr = new Leap_Centre__c();

lcr.Name = 'Anaikkum Karangal';

lcr.Location__c = 'test location';

lcr.Primary_Representative__c = 'Avinash';

lcr.Primary_Rep_s_Mobile__c = '9176473115';

lcr.Primary_Rep_s_Email__c = 'asd@asd.com';

insert lcr;

record.Short_Description__c = 'test record';

record.For_Centre__c = 'Anaikkum Karangal';

record.For_Date__c = Date.Today() + 1;

record.Send_Email__c = FALSE;

record.Uploaded_By__c = 'Avinash';

record.Leap_Centre__c = lcr.Id;

// Insert the record

insert record;

system.debug(record);

// Insert Attachments;

attachment1.Name='Unit Test Attachment';

attachment1.ParentId = record.Id;

attachment1.body=Blob.valueOf('Unit Test Attachment Body');

insert attachment1;

String Query='Select Id,Name,For_Centre__c,For_Date__c,Leap_Centre__c,Leap_Centre__r.Name,Send_Email__c,Short_Description__c,Uploaded_By__c from Leap_Weekly_Curriculum__c where Send_Email__c = FALSE and For_Date__c >= TODAY';

Test.startTest();

sendEmailtoVolntr c = new sendEmailtoVolntr(query);

Database.executeBatch(c);

Test.stopTest();

}

}

Don't understand what's going wrong.. please help out.
3 answers
  1. Mar 15, 2016, 5:22 AM
    Hi Amit and Lokesh,

    Thanks a lot for your elaborate responses. I'm not able to cover lines 109 - 122 on the test class. Not sure how I can bring out an error sending the email to go into that part of code. 

    Any ideas? Get what I mean?
  2. Mar 14, 2016, 9:12 AM

    Try below Test class.

    @isTest

    public with sharing class Attachmentstest{

    public static testmethod void Attachmentstest()

    {

    Leap_Weekly_Curriculum__c record = new Leap_Weekly_Curriculum__c();

    Attachment attachment1 = new Attachment();

    Leap_Centre__c lcr = new Leap_Centre__c();

    lcr.Name = 'Anaikkum Karangal';

    lcr.Location__c = 'test location';

    lcr.Primary_Representative__c = 'Avinash';

    lcr.Primary_Rep_s_Mobile__c = '9176473115';

    lcr.Primary_Rep_s_Email__c = 'asd@asd.com';

    insert lcr;

    record.Short_Description__c = 'test record';

    record.For_Centre__c = 'Anaikkum Karangal';

    record.For_Date__c = Date.Today() + 1;

    record.Send_Email__c = FALSE;

    record.Uploaded_By__c = 'Avinash';

    record.Leap_Centre__c = lcr.Id;

    // Insert the record

    insert record;

    system.debug(record);

    // Insert Attachments;

    attachment1.Name='Unit Test Attachment';

    attachment1.ParentId = record.Id;

    attachment1.body=Blob.valueOf('Unit Test Attachment Body');

    insert attachment1;

    String Query= 'Select Id,Name,For_Centre__c,For_Date__c,Leap_Centre__c,Leap_Centre__r.Name,Send_Email__c,Short_Description__c,Uploaded_By__c from Leap_Weekly_Curriculum__c where Send_Email__c = FALSE and For_Date__c >= TODAY';

    Test.startTest();

    sendEmailtoVolntr c = new sendEmailtoVolntr();

    c.Query = 'Select Id,Name,For_Centre__c,For_Date__c,Leap_Centre__c,Leap_Centre__r.Name,Send_Email__c,Short_Description__c,Uploaded_By__c from Leap_Weekly_Curriculum__c where Send_Email__c = FALSE ';

    Database.executeBatch(c);

    Test.stopTest();

    }

    }

    Let us know if this will help you

     
  3. Mar 14, 2016, 8:54 AM
    Hi Avinash,

    Try to modify your test class as shown here

    Test.startTest();

    sendEmailtoVolntr c = new sendEmailtoVolntr();

    Database.executeBatch(c);

    Test.stopTest();
0/9000