-2

We made a very minor change to the apex class where we included a field in the Email mathod in the class, however when we tried to deploy we get attempt to de-reference null object error.

Test class:

public static testMethod void testUpdateRegistration2(){

Course__c newCourse = new Course__c();
newCourse.Total_Sessions__c = 3;
newCourse.Session_No__c = 001;
newCourse.Registration_Time__c = '8:45AM';
newCourse.Number_of_Days__c = 1;
newCourse.Name = 'Apex Test Course';
newCourse.Minimum_Attendee__c = 5;
newCourse.Maximum_Attendee__c = 10;
newCourse.GST__c = 10;
newCourse.GST_Exempt_Course__c = false;
newCourse.Course_Type__c = 'Face to Face';
newCourse.Course_Start_Time__c = '9:00AM';
newCourse.Course_Number__c = 'C001';
newCourse.Course_End_Time__c = '5:00PM';
newCourse.Course_Classification__c = 'Public';
newCourse.ChargeID__c = 'PRC_999';
newCourse.Category__c = 'Food';
newCourse.Base_Price__c = 999;
insert newCourse;

datetime t = System.now();
date today = Date.newInstance(t.year(),t.month(),t.day());

Class__c newClass = new Class__c();
newClass.Name = 'Apex Test Class';
newClass.Minimim_Attendee__c = 9;
newClass.Maximum_Attendee__c = 99;
newClass.Course_Name__c = newCourse.Id;
newClass.Class_Location__c = 'Sydney';
newClass.Class_Begin_Date__c = today;
newClass.Class_End_Date__c = today;
newClass.Number_of_Confirmed_Attendees__c = 10;
newClass.Class_Status__c = 'Open';
insert newClass;

Account newAccount = new Account();
newAccount.Same_As_Address__c = 'true';
newAccount.Name = 'Apex Test';
//newAccount.ParentId = newAccountParent.Id;
newAccount.Mailing_Zip_Postal_Code__c = '2009'; 
newAccount.Mailing_Street__c = 'Fake Mailing Street';
newAccount.Mailing_State_Province__c = 'NSW';
newAccount.Mailing_Country__c = 'AUSTRALIA';
newAccount.Mailing_City__c = 'Sydney';
newAccount.CurrencyIsoCode = 'AUD';
newAccount.Business_Zip_Postal_Code__c = '1000'; 
newAccount.Business_Street__c = 'Fake Business Street';
newAccount.Business_State_Province__c = 'NSW';
newAccount.Business_Country__c = 'AUSTRALIA';
newAccount.Business_City__c = 'Sydney';
newAccount.Account_Classification__c = 'Business';
insert newAccount;

Account newVenueAccount = new Account();
newVenueAccount.Same_As_Address__c = 'true';
newVenueAccount.Name = 'Apex Venue Test';
newVenueAccount.Mailing_Zip_Postal_Code__c = '2009'; 
newVenueAccount.Mailing_Street__c = 'Fake Mailing Street';
newVenueAccount.Mailing_State_Province__c = 'NSW';
newVenueAccount.Mailing_Country__c = 'AUSTRALIA';
newVenueAccount.Mailing_City__c = 'Sydney';
newVenueAccount.CurrencyIsoCode = 'AUD';
newVenueAccount.Business_Zip_Postal_Code__c = '1000'; 
newVenueAccount.Business_Street__c = 'Fake Business Street';
newVenueAccount.Business_State_Province__c = 'NSW';
newVenueAccount.Business_Country__c = 'AUSTRALIA';
newVenueAccount.Business_City__c = 'Sydney';
newVenueAccount.Account_Classification__c = 'Business';
newVenueAccount.Type = 'Venue';
insert newVenueAccount;

Contact newContact = new Contact();
newContact.FirstName='John';
newContact.LastName='Doe';
newContact.Phone='0299998888';
newContact.Email = 'john.doe@fake.com';
newContact.AccountId = newAccount.Id;
newContact.Dietary_Requirements__c = 'ABC';
insert newContact;

Contact newContact1 = new Contact();
newContact1.LastName='Master';
newContact1.Email = 'a.master@fake.com';
newContact1.Phone = '0288889999';
newContact1.AccountId = newVenueAccount.Id;
insert newContact1;

Contact newContact2 = new Contact();
newContact2.Job_Title__c = 'National Manager';
newContact2.LastName='Master';
//newContact2.Email = 'a.master@fake.com';
newContact2.AccountId = newAccount.Id;
newContact2.MobilePhone = '0499999999';
insert newContact2;

Trainer_Approved_Course__c tac = new Trainer_Approved_Course__c();
tac.Name = 'Apex Trainer Approved Course';
tac.Trainer__c = newContact.Id;
tac.Expiration_Date__c = today + 100000;
tac.Course__c = newCourse.Id;
tac.Approved__c = true;
tac.CurrencyIsoCode = 'AUD';
insert tac;

Trainer_Approved_Course__c tac2 = new Trainer_Approved_Course__c();
tac2.Name = 'Apex Trainer Approved Course 2';
tac2.Trainer__c = newContact2.Id;
tac2.Expiration_Date__c = today + 100000;
tac2.Course__c = newCourse.Id;
tac2.Approved__c = true;
tac2.CurrencyIsoCode = 'AUD';
insert tac2;


Class__c newClass1 = new Class__c();
newClass1.Name = 'Apex Test Class';
newClass1.Minimim_Attendee__c = 9;
newClass1.Maximum_Attendee__c = 99;
newClass1.Course_Name__c = newCourse.Id;
newClass1.Class_Location__c = 'Sydney';
newClass1.Class_Begin_Date__c = today;
newClass1.Class_End_Date__c = today;
newClass1.Number_of_Confirmed_Attendees__c = 10;
newClass1.Class_Status__c = 'Open';
newClass1.Trainer_1__c = newContact.Id;
newClass1.Trainer_2__c = newContact1.Id;
newClass1.Venue__c = newVenueAccount.Id;
newClass1.Num_Material_Ordered__c = 10;
newClass1.Date_Material_Ordered__c = today;
newClass1.Num_in_Stock__c = 10;
newClass1.Num_Material_Printed__c = 5;
newClass1.Num_Standards_Out__c = 10;
newClass1.Num_Standards_In__c = 10;
newClass1.Boxes__c = 2;
newClass1.Job_Reference__c = 'N/A';
newClass1.Date_Material_Sent__c = today;
newClass1.Comments_Materials__c = 'N/A';
insert newClass1;

Id[] originalRegIds = new Id[0];
Registration__c newAttendee = new Registration__c();
newAttendee.Course_Name__c = newCourse.Id;
newAttendee.Attendee__c = newContact.Id;
newAttendee.Class_Name__c = newClass.Id;
newAttendee.Total_Amount__c = 999;
newAttendee.Status__c = 'Confirmed';
insert newAttendee;
originalRegIds.add(newAttendee.Id);

Registration__c newAttendee1 = new Registration__c();
newAttendee1.Course_Name__c = newCourse.Id;
newAttendee1.Attendee__c = newContact.Id;
insert newAttendee1;
originalRegIds.add(newAttendee1.Id);

Registration__c newAttendee2 = new Registration__c();
newAttendee2.Course_Name__c = newCourse.Id;
newAttendee2.Class_Name__c = newClass.Id;
newAttendee2.Attendee__c = newContact.Id;
newAttendee2.Status__c = 'Pended-SAI Cancellation';
insert newAttendee2;
originalRegIds.add(newAttendee2.Id);

Registration__c newAttendee3 = new Registration__c();
newAttendee3.Course_Name__c = newCourse.Id;
newAttendee3.Class_Name__c = newClass1.Id;
newAttendee3.Attendee__c = newContact1.Id;
newAttendee3.Status__c = 'Confirmed';
insert newAttendee3;

Registration__c newAttendee4 = new Registration__c();
newAttendee4.Course_Name__c = newCourse.Id;
newAttendee4.Class_Name__c = newClass1.Id;
newAttendee4.Attendee__c = newContact2.Id;
newAttendee4.Status__c = 'Confirmed';
insert newAttendee4;

Certificate_Template__c newCertificateTemplate = new Certificate_Template__c();
newCertificateTemplate.Name = 'Apex Test Certificate Template';
newCertificateTemplate.CurrencyIsoCode = 'AUD';
insert newCertificateTemplate;

Certificate_Type__c newCertificateType = new Certificate_Type__c();
newCertificateType.Name = 'Statement of Attendance';
newCertificateType.Course_Name__c = newCourse.Id;
newCertificateType.Certificate_Title__c = 'Statement of Attendance';
newCertificateType.Certificate_Template__c = newCertificateTemplate.Id;
newCertificateType.Certificate_Options__c = 'Always Issued';
insert newCertificateType;

// reset governor limit
Test.startTest();

Assessment__c newAssessment = new Assessment__c();
newAssessment.Name = 'Statement of Attendance';
newAssessment.Assessment_Title__c = newCertificateType.Id;
newAssessment.Assessment_Status__c = 'Competent';
newAssessment.Attendee_ID__c = newAttendee.Id;
newAssessment.Assessor__c = newContact.Id;
newAssessment.Received_from_Assessor__c = today;
insert newAssessment;

Assessment_Competency__c newAssessmentCompetency = new Assessment_Competency__c();
newAssessmentCompetency.Name = 'Apex Test Assessment Competency';
newAssessmentCompetency.CurrencyIsoCode = 'AUD';
newAssessmentCompetency.Competency_Code__c = 'ApexTest_9999';
newAssessmentCompetency.Assessment__c = newAssessment.Id;
insert newAssessmentCompetency;

Invoice_ENT__c newInvoiceENT = new Invoice_ENT__c();
newInvoiceENT.Registration__c = newAttendee.Id;
newInvoiceENT.CurrencyIsoCode = 'AUD';
newInvoiceENT.Base_Price__c = 998;
newInvoiceENT.Payment_Status__c = 'c';
newInvoiceENT.Invoice_Type__c = 'ACR';
newInvoiceENT.Bill_Type__c = 'EAR';
insert newInvoiceENT;

Invoice_ENT__c newInvoiceENT1 = new Invoice_ENT__c();
newInvoiceENT1.Registration__c = newAttendee.Id;
newInvoiceENT1.CurrencyIsoCode = 'AUD';
newInvoiceENT1.Base_Price__c = 998;
newInvoiceENT1.Payment_Status__c = 'c';
newInvoiceENT1.Invoice_Type__c = 'REG';
newInvoiceENT1.Bill_Type__c = 'PUB';
insert newInvoiceENT1;

 try{
    Integer processType = 1;
    ClassObj.classcancellation(newClass.Id, processType);
    ClassObj.sendAttendanceList(newClass1.Id);
    ClassObj.sendAttendanceList_TIS(newClass1.Id);
    //System.assertEquals(0, classObj.checkPendingRegos(newClass.Id));
} catch (System.EmailException e){
    System.debug('we caught a email exception: ' + e.getDmlMessage(0));
}

Test.stopTest();

The ** marked line is the error line 429

Please find the image below enter image description here

Please advise.

WebService static Id[] sendAttendanceList_TIS(Id ClassId) {

//Modified by sneh@sqwarepeg.com to include more venue and class material details
Class__c classObj = [Select Trainer_1__c, Trainer_1__r.Id, Trainer_1__r.Email,Trainer_1__r.EmailBouncedDate, 
Trainer_1__r.EmailBouncedReason,Trainer2__c, Trainer2__r.Id, Trainer2__r.Email, Trainer2__r.EmailBouncedDate, 
Trainer2__r.EmailBouncedReason, Trainer_2__c, Trainer_2__r.Id, Trainer_2__r.EmailBouncedDate, 
Trainer_2__r.EmailBouncedReason,Trainer_2__r.Email, Id, Venue__r.Id, Venue__r.Name, Venue__r.Business_Street__c, 
Venue__r.Business_City__c, Venue__r.Business_State_Province__c, Venue__r.Business_Zip_Postal_Code__c, 
Venue__r.Business_Country__c, Venue__r.Website, Venue__r.Phone, Venue__r.Internet_Email__c, Venue__r.Comments__c, 
Course_Name__r.Name, Class_Location__c, Class_End_Date__c, Class_Begin_Date__c, Num_in_Stock__c, 
Num_Standards_Out__c, Num_Standards_In__c, Num_Material_Printed__c, Num_Material_Ordered__c, Job_Reference__c, 
Date_Material_Sent__c, Date_Material_Ordered__c, Comments_Materials__c, Boxes__c 
From Class__c where Id =:ClassId];

//Modified by suzan@sqwarepeg.com, validation of the bounced emails
//Modified by sneh@sqwarepeg.com on 12/02/2009 to incorporate the Primary_Venue_Contact__c functionality
Contact[] conObj = [Select Id, Email, AccountId,EmailBouncedDate,EmailBouncedReason From Contact where AccountId =:classObj.Venue__r.Id and Primary_Venue_Contact__c = true];

//Contact[] conObj = [Select Id, Email, AccountId From Contact where AccountId =:classObj.Venue__r.Id];
//Query course field to add them in the Email template.
Course__c crs = [select id,Current_Revision__c from Course__c limit 1];
//if (classObj.Trainer_1__r.Email != null || classObj.Trainer_2__r.Email != null || conObj.Email != null) {
String jobTitle;
String email;
String phone;
String mobile;
String dietaryNeeds;
String accessNeeds;
String HTMLBody;

List<Id> RegIds = new List<Id>();
RegIds = getConfirmedIds(ClassId);

HTMLBody = 'Hi,<br><br>';
HTMLBody = HTMLBody + 'Please find attached the Attendance List for SAI Global\'s course.<br><br>';
HTMLBody = HTMLBody + 'Please print off a copy and hand to the Trainer on the morning of the course.<br><br>';
HTMLBody = HTMLBody + 'If you have any questions please don\'t hesitate to contact our Customer Service Team on 1-800-374-3818.<br><br>';
HTMLBody = HTMLBody + 'Kind Regards,<br>Training and Improvement Solutions<br><br>';
HTMLBody = HTMLBody + '<b><u>Course Information</u></b><br><br>';
**if(classObj.Class_End_Date__c != null){
    HTMLBody = HTMLBody + 'Course Name: ' + classObj.Course_Name__r.Name + '<br>' + 'Current Revision:' + crs.Current_Revision__c.format() + '<br>' + 'Class Dates: ' + classObj.Class_Begin_Date__c.day() + '/' + classObj.Class_Begin_Date__c.month() + '/' + classObj.Class_Begin_Date__c.year() + ' - ' + classObj.Class_End_Date__c.day() + '/' + classObj.Class_End_Date__c.month() + '/' + classObj.Class_End_Date__c.year() + '<br>';
} else {
    HTMLBody = HTMLBody + 'Course Name: ' + classObj.Course_Name__r.Name + '<br>' + 'Current Revision:' + crs.Current_Revision__c.format() + '<br>' + 'Class Dates: ' + classObj.Class_Begin_Date__c.day() + '/' + classObj.Class_Begin_Date__c.month() + '/' + classObj.Class_Begin_Date__c.year() + ' - N/A<br>';
}**
HTMLBody = HTMLBody + 'Class Location: ' + classObj.Class_Location__c + '<br><br>';

From the above class the ** marked code is where I have included the crs.Current_Revision__c.format() inorder to display the formated date field in the email. It works fine in the sandbox, however it is not while deploying to production.

Thanks,

1

It seems clear that the proximate cause of this exception is that crs.Current_Revision__c is null, resulting in a NullPointerException when you call crs.Current_Revision__c.format().

It appears to me that your unit test should not pass, because you do not populate this field on your test-context Course__c record. Of course, it's possible that some other automation populates this field when you create a related Class__c, and this allows your unit test to pass.

If that's the case - your unit test passes, but the class fails in Production - the ultimate problem is likely this query:

Course__c crs = [select id,Current_Revision__c from Course__c limit 1];

The query includes no restrictions on which Course__c record will be returned. You should, presumably, be filtering on the value of Class__c.Course_Name__c.

Assuming, again, that your unit test passes but the code fails in Production use, I suspect what's happening is that you have some other Course__c record in Production whose Current_Revision__c field is blank, and, since you've run a query that doesn't filter to identify the right Course__c, you're getting back that one.

In addition to fixing this issue, you should add more sophisticated null checking in your email generation code to avoid having this issue recur.

  • I have tried giving valued for the field in the test method , But did not worked. I am trying to filter the query like Course__c crs = [select id,Current_Revision__c from Course__c where Id=:Class__c.Course_Name__c]; , this one throw me "Invalid bind expression type of Schema.SObjectField for column of type Id" – Samuel Robert Jan 28 at 4:48
  • My mistake I changed the query to Course__c crs = [select id,Current_Revision__c from Course__c where Id=:classObj.Course_Name__r.Id]; and this fixed the issue and I deployed the code to production. Thanks for you help @David Reed. – Samuel Robert Jan 28 at 5:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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