You need to sign in to do that
Don't have an account?
Padmini S 26
System.NullPointerException: Attempt to de-reference a null object in salesforce test class
Hi All,
I am getting System.NullPointerException: Attempt to de-reference a null object error in salesforce test class.
Apex Class:
Test Class
Getting error in openpopup() method in Apex class. Because passing the recordId.startsWith('00Q') in openpopup. Not getting how to pass in Test class. Could any one help on this.
Thanks in Advance.
I am getting System.NullPointerException: Attempt to de-reference a null object error in salesforce test class.
Apex Class:
global class planvisitcontroller { public String RequestorEmployee { get; set; } public id leadid{get;set;} public string userid{get;set;} public string SelectedProduct{get;set;} public boolean showReqPopup { get; set; } public list<lead> leadlist{get;set;} public boolean serachdone{get;set;} public list<user> userList{get;set;} public String routeAccMap { get; set; } public String taskSubject{get;set;} public DateTime startDateTime {get;set;} public DateTime endDateTime {get;set;} public String errorMessage {get;set;} public list<Account> lstAccount {get;set;} public boolean isError { get; set; } public List<WrapperLead> wrappers {get; set;} public List<String> selectedDays {get;set;} public String recordName { get; set; } public List<SelectOption> days { get; set; } public Set<String> plannedDays; public String currentMonth { get; set; } public planvisitcontroller () { showReqPopup = false; wrappers = new List<WrapperLead>(); SelectedDays = new List<String>(); days = new List<SelectOption>(); mapUsersByID = new Map<Id, User>([SELECT Id, Name From User WHERE Manager.Id=:UserInfo.getUserId() AND UserRole.Name='Sales Executive']); leadlist=[select id, name, Status, Requested_Visit_Time__c,OwnerId,Sales_Executive__c,Address,city from lead WHERE OwnerID=:mapUsersByID.keySet() and isConverted=false and Sales_Executive__c!=null order by Requested_Visit_Time__c]; for(Lead l : leadlist){ wrappers.add(new WrapperLead(l, new List<Integer>{1,2})); } lstAccount = [SELECT Id, Name, Onboarding_Status__c, BillingCity, OwnerId,Sales_Executive__c From Account WHERE Sales_Executive__c=:mapUsersByID.keySet()]; } public PageReference closetable() { showReqPopup=false; return null; } public PageReference SelectedEmployee() { return null; } public PageReference closePopup() { PageReference ref = new PageReference('/apex/D2R_PlanVisit'); ref.setRedirect(true); return ref; } public void FilterEmployee() { userList=mapUsersByID.values(); serachdone=true; openpopup(); //return null; } public PageReference CloseWindow() { showReqPopup=false; selectedDays = new List<String>(); plannedDays = new Set<String>(); taskSubject = ''; return null; } public PageReference openpopup() { showReqPopup=true; String recordId = leadid; List<Task> existingTasks = new List<Task>(); plannedDays = new Set<String>(); if(recordId.startsWith('00Q')) { Lead l = [SELECT Sales_Executive__r.name FROM Lead WHERE Id=:recordId]; if(RequestorEmployee=='' || RequestorEmployee == null) RequestorEmployee = l.Sales_Executive__r.name; existingTasks = [Select Id, subject, ActivityDate from Task Where WhoId=:recordId AND Type='Visit']; }else { Account a = [Select Sales_Executive__r.name From Account Where id=:recordId]; if(RequestorEmployee=='' || RequestorEmployee == null) RequestorEmployee = a.Sales_Executive__r.name; existingTasks = [Select Id, subject, ActivityDate from Task Where WhatId=:recordId AND Type='Visit']; } days = getDaysOfCurrentMonth(); for (Task t : existingTasks){ String taskDay = String.valueOf(t.ActivityDate); } selectedDays = new List<String>(); for (Task t : existingTasks){ String taskDay = String.valueOf(t.ActivityDate); System.debug('!!: ' + taskDay); if(String.isNotBlank(taskDay) ){ List<String> i = taskDay.split('-'); if(Date.Today().Year() == Integer.valueOf(i[0]) && Date.Today().Month() == Integer.valueOf(i[1])) if(!plannedDays.contains(''+i[2])) { selectedDays.add(''+Integer.valueOf(i[2])); plannedDays.add(''+Integer.valueOf(i[2])); } } } System.debug(' selectedDays !!: ' + selectedDays); System.debug('plannedDays !!: ' + plannedDays); return null; } public PageReference saveplaning() { showReqPopup=false; String recordId = leadid; Lead leadplan = new Lead(); Account acc = new Account(); List<Task> tasksToInsert = new List<Task>(); System.debug('selected Days: ' + selectedDays ); if(recordId.startsWith('00Q')){ leadplan = [select id, Requested_Visit_Time__c, Plan__c, Name, OwnerId, Sales_Executive__c from Lead where id = :recordId ]; } else { acc = [SELECT Id, Name, BillingCity, OwnerId,Sales_Executive__c From Account WHERE Id=:recordId]; } for(String str : selectedDays) { system.debug('Loop for : ' + str); Id visitRT = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Plan-Visit Task').getRecordTypeId(); if(!plannedDays.contains(str)) { system.debug('task to be inserted for : ' + str); Integer day = Integer.valueOf(str); Date dueDate = date.newInstance(Date.Today().Year(), Date.Today().Month(), day); Task task = new Task(); task.subject = taskSubject; if(String.isNotBlank(userid)){ task.OwnerId = userid; } else { if(recordId.startsWith('00Q')) task.OwnerId = leadplan.Sales_Executive__c; else task.OwnerId = acc.Sales_Executive__c; } if(recordId.startsWith('00Q')) { task.whoId = recordId; } else { task.whatId = recordId; } task.ActivityDate = dueDate; task.RecordTypeId = visitRT; task.Type = 'Visit'; tasksToInsert.add(task); } } if(tasksToInsert.size()>0) { insert tasksToInsert; } PageReference ref = new PageReference('/apex/D2R_PlanVisit'); ref.setRedirect(true); return ref; } public List<SelectOption> getDaysOfCurrentMonth() { Map<Integer,String> mapMonthsByIndex = new Map<Integer,String>{ 1 => 'January', 2 => 'February'}; currentMonth = mapMonthsByIndex.get(Date.Today().Month()) + ' , ' + Date.Today().Year(); Date firstDayOfMonth = System.today().toStartOfMonth(); Date lastDayOfMonth = firstDayOfMonth.addDays(Date.daysInMonth(firstDayOfMonth.year(), firstDayOfMonth.month()) - 1); System.debug('!!!!: ' + lastDayOfMonth); List<String> days = new List<String>(); for(Integer i=1; i<=lastDayOfMonth.day() ; i++ ){ days.add('' + i); System.debug('!!!!: ' +i); } List<SelectOption> Options = new List<SelectOption>(); for(String str : days){ Options.add(new SelectOption(str,str)); } return Options; } }
Test Class
@isTest private class planvisitcontrollerTest { @testSetup static void setup() { Profile p = [SELECT Id FROM Profile WHERE Name='Sales Executive']; User u2 = new User(Alias = 'standt1',Country='United Kingdom',Email='demo1@randomdemodomain.com',EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='dprobertdemo1@camfed.org'); insert u2; Lead l = new Lead(); l.LastName = 'Salesforce.com'; l.Company = 'KPMG'; l.Status = 'New'; l.Email = 'abc@gmail.com'; l.Phone = '8938493'; l.Industry = 'Web'; l.City=' bangalore'; l.PostalCode ='788889'; l.State = 'Karnataka'; l.Country = 'India'; insert l; Task t = new Task(); t.Subject= 'test task'; t.ActivityDate = System.Today(); t.WhoId = l.Id; t.Type = 'Visit'; insert t; l = [SELECT Id FROM Lead WHERE Id = :l.Id]; } @isTest static void testMethod1() { planvisitcontroller pc = new planvisitcontroller(); Test.startTest(); pc.SelectedEmployee(); pc.closePopup(); pc.FilterEmployee(); pc.CloseWindow(); pc.saveplaning(); pc.getDaysOfCurrentMonth(); Test.stopTest(); System.assertNotEquals(null, pc.openpopup()); } }
Getting error in openpopup() method in Apex class. Because passing the recordId.startsWith('00Q') in openpopup. Not getting how to pass in Test class. Could any one help on this.
Thanks in Advance.
See below code: If this resolves, kindly mark this as the best answer.
All Answers
See below code: If this resolves, kindly mark this as the best answer.
Thank you for your help. It works. But code coverage is only 65%.