You need to add a null check to your guard condition.
if (accountRecordTypes == null || accountRecordTypes.isEmpty()) {
The corresponding member that automagically exists behind the property won't be initialised until you call accountRecordTypes = new Map<String, String>();. So the original call to .isEmpty() would have been on null.
As Jesse Altman commented, the null check alone is probably preferable here. If there are no RecordType records that match the SOQL query you don't want to be running the query code again every time the property is called.
Answer from Daniel Ballinger on Stack ExchangeYou need to add a null check to your guard condition.
if (accountRecordTypes == null || accountRecordTypes.isEmpty()) {
The corresponding member that automagically exists behind the property won't be initialised until you call accountRecordTypes = new Map<String, String>();. So the original call to .isEmpty() would have been on null.
As Jesse Altman commented, the null check alone is probably preferable here. If there are no RecordType records that match the SOQL query you don't want to be running the query code again every time the property is called.
Change this:
if (accountRecordTypes.isEmpty()){
by
if (accountRecordTypes == null){
java - What is the optimal way to check if a map contains any empty values or keys? - Stack Overflow
How to check check Map is Empty or null in Visualforce page dynamically in <apex:repeat> - Salesforce Stack Exchange
apex - How do I check if a list-value from my Map is empty? - Salesforce Stack Exchange
Feed Item Detail | Salesforce Trailblazer Community
You could try below
questionsAndAnswers().ifPresent(qna -> {
if (qna.isEmpty() || qna.containsKey("") || qna.containsValue("")) {
throw new IllegalArgumentException("Error Message Comes Here");
}
});
questionsAndAnswers().ifPresent(ConcurrentHashMap::new); throws a NullPointerException if there is any null key/value
I see that the batch query does not have the filtering condition 'Contact__c != null'. So, it's possible that one of the benefit records is missing value in the 'Contact__c' field and you wouldn't find it in the map. You can solve this in two ways:
- Add 'Contact__c != null' to the selector query if you don't care about those records.
(Or)
Check for 'null' value in the for loop as below:
if(!ConMap.isEmpty()) { for(Benefit__c ben : ConMap.values()) { if(String.isBlank(ben.Contact__c)){ /* continue; or throw exception() */ } Task taskObj = new Task(); taskObj.OwnerId = ConMap.get(ben.contact__c).contact__r.OwnerId;
I re wrote the code to resolve this nd optimize the code as below.
public with sharing class MyBatch implements Database.Batchable<AggregateResult>{
public Iterable<AggregateResult> start(Database.BatchableContext context)
{
return [
SELECT Contact__c, Contact__r.OwnerId owner FROM Benefit__c
WHERE Contact__c != null AND ...
GROUP BY Contact__c, Contact__r.OwnerId
];
}
public void execute(Database.BatchableContext context, List<AggregateResult> scope)
{
Set<Id> contactIds = new Set<Id>();
List<Task> tasks = new List<Task>();
for (AggregateResult aggregate : scope)
{
Id ownerId = (Id)aggregate.get('owner');
Id contactId = (Id)aggregate.get('Contact__c');
contactIds.add(contactId);
tasks.add(new Task(OwnerId=ownerId, /*other fields*/));
}
insert tasks;
List<Benefit__c> benefits = [
SELECT Id FROM Benefit__c WHERE Contact__c IN :contactIds
];
for (Benefit__c benefit : benefits)
benefit.Task_Created__c = true;
update benefits;
}
public void finish(Database.BatchableContext context) { /*additional logic*/ }}
A better approach would be to overload methods. You can create 2 methods with different parameters for your use case as below:
public class classC {
public static Object meth1(List<String> lstStr, Boolean bool, Map<integer,string> testMap) {
// logic
}
public static Object meth1(List<String> lstStr, Boolean bool) {
// logic
}
}
Depending on the parameters you pass, appropriate method will be invoked.
Pass the empty map as a parameter and check if map.isEmpty() Map documentaion
class c{
static void method(List<String> lstStr, Boolean bool, Map<integer,string> testMap){
if(!testMap.isEmpty()){
//logic
}
else{
}
}
}
class B {
//pass the empty map in method
c.method(lstStr,true,new Map<integer,string>());
}
class A{
//pass the data is map
c.method(lstStr,true,testMap)
}
