You need to sign in to do that
Don't have an account?
Victor Nguyen 13
ystem.NullPointerException: Attempt to de-reference a null object
Hi everyone,
I'm getting this error on line 58:
Below is the code. I'm planning to put this code on Process Builder. This code will transfer the opportunity to other representatives when the opportunity has been touched for 4 days. Before transferring away, it will create a record on Round Robin Record object. Record Distribution is the list of teams. Record Distribution Mapping is the list of Representatives with a lookup field to Record Distribution object.
I can't find the reason why the Number_of_Opp__c is empty, I have put 0 in that field for each record already.
Please help! Thanks in advance.
I'm getting this error on line 58:
ystem.NullPointerException: Attempt to de-reference a null object
Below is the code. I'm planning to put this code on Process Builder. This code will transfer the opportunity to other representatives when the opportunity has been touched for 4 days. Before transferring away, it will create a record on Round Robin Record object. Record Distribution is the list of teams. Record Distribution Mapping is the list of Representatives with a lookup field to Record Distribution object.
public class RoundRobinOpp { public class RRRequest { @InvocableVariable public String opportunityName; @InvocableVariable public String distributionName; @InvocableVariable public Id opportunityId; @InvocableVariable public Id ownerId; } public static List<Record_Distribution_Mapping__c> getRepsByDistribution(String distributionName) { return [select User__r.id, CreatedDate, Record_Distribution__c, Record_Distribution__r.Number_of_RR__c from Record_Distribution_Mapping__c where Record_Distribution__r.Name = :distributionName order by CreatedDate asc]; } @InvocableMethod public static void RoundRobinOpp(List<RRRequest> requests) { Set<String> distributionnames = new Set<String>(); Set<Id> username = new Set<Id>(); for (RRRequest request : requests) { distributionnames.add(request.distributionName); username.add(request.ownerId); } Map<String, List<Record_Distribution_Mapping__c>> distributionToMappings = new Map<String, List<Record_Distribution_Mapping__c>>(); Map<String, Record_Distribution__c> relevantDistributions = new Map<String, Record_Distribution__c>(); Map<String, Record_Distribution_Mapping__c> relevantUser = new Map<String, Record_Distribution_Mapping__c>(); for (Record_Distribution_Mapping__c users : [SELECT User__c, Opportunity_Name_1__c, Opportunity_Name_2__c, Opportunity_Name_3__c, Number_of_Opp__c FROM Record_Distribution_Mapping__c WHERE User__c = :username]) { relevantUser.put(users.User__c,users); } for (Record_Distribution__c distribution : [select Name, Number_of_RR__c from Record_Distribution__c where name in :distributionnames]) { relevantDistributions.put(distribution.name, distribution); } for (String distributionName : distributionnames) { distributionToMappings.put(distributionName, getRepsByDistribution(distributionName)); } List<Round_Robin_Record__c> lstRRrecord = new List<Round_Robin_Record__c>(); List<Opportunity> lstOppsToUpdate = new List<Opportunity>(); for (RRRequest request : requests) { Round_Robin_Record__c newRecord = new Round_Robin_Record__c(); newRecord.Opportunity_Name__c = request.opportunityName; newRecord.User__c = request.ownerId; Record_Distribution_Mapping__c updateRRnumber = relevantUser.get(request.ownerId); updateRRnumber.Number_of_Opp__c++; //Might add the opportunity names for reporting Opportunity reassignedOpp = new Opportunity(id = request.opportunityId); Record_Distribution__c TeamDistribution = relevantDistributions.get(request.distributionName); reassignedOpp.OwnerId = distributionToMappings.get(request.distributionName)[Math.mod((Integer) TeamDistribution.Number_of_RR__c, distributionToMappings.get(request.distributionName).size())].User__r.id; lstRRrecord.add(newRecord); TeamDistribution.Number_of_RR__c++; lstOppsToUpdate.add(reassignedOpp); } insert lstRRrecord; update lstOppsToUpdate; update relevantDistributions.values(); update relevantUser.values(); } }
I can't find the reason why the Number_of_Opp__c is empty, I have put 0 in that field for each record already.
Please help! Thanks in advance.
--
Magulan Duraipandian
www.infallibletechie.com
Thanks for your input. Is there a way to fix it? Sorry, I'm new to Apex.
Add the If Condition to avoid the NullPointerException like below:
public class RoundRobinOpp {
public class RRRequest {
@InvocableVariable
public String opportunityName;
@InvocableVariable
public String distributionName;
@InvocableVariable
public Id opportunityId;
@InvocableVariable
public Id ownerId;
}
public static List<Record_Distribution_Mapping__c> getRepsByDistribution(String distributionName) {
return [select User__r.id, CreatedDate, Record_Distribution__c, Record_Distribution__r.Number_of_RR__c
from Record_Distribution_Mapping__c
where Record_Distribution__r.Name = :distributionName
order by CreatedDate asc];
}
@InvocableMethod
public static void RoundRobinOpp(List<RRRequest> requests) {
Set<String> distributionnames = new Set<String>();
Set<Id> username = new Set<Id>();
for (RRRequest request : requests) {
distributionnames.add(request.distributionName);
username.add(request.ownerId);
}
Map<String, List<Record_Distribution_Mapping__c>> distributionToMappings = new Map<String, List<Record_Distribution_Mapping__c>>();
Map<String, Record_Distribution__c> relevantDistributions = new Map<String, Record_Distribution__c>();
Map<String, Record_Distribution_Mapping__c> relevantUser = new Map<String, Record_Distribution_Mapping__c>();
for (Record_Distribution_Mapping__c users : [SELECT User__c, Opportunity_Name_1__c, Opportunity_Name_2__c, Opportunity_Name_3__c, Number_of_Opp__c
FROM Record_Distribution_Mapping__c WHERE User__c = :username]) {
relevantUser.put(users.User__c,users);
}
for (Record_Distribution__c distribution : [select Name, Number_of_RR__c from Record_Distribution__c where name in :distributionnames]) {
relevantDistributions.put(distribution.name, distribution);
}
for (String distributionName : distributionnames) {
distributionToMappings.put(distributionName, getRepsByDistribution(distributionName));
}
List<Round_Robin_Record__c> lstRRrecord = new List<Round_Robin_Record__c>();
List<Opportunity> lstOppsToUpdate = new List<Opportunity>();
for (RRRequest request : requests) {
Round_Robin_Record__c newRecord = new Round_Robin_Record__c();
newRecord.Opportunity_Name__c = request.opportunityName;
newRecord.User__c = request.ownerId;
if(relevantUser.ContainsKey(request.ownerId))
{
Record_Distribution_Mapping__c updateRRnumber = relevantUser.get(request.ownerId);
updateRRnumber.Number_of_Opp__c++;
}
//Might add the opportunity names for reporting
Opportunity reassignedOpp = new Opportunity(id = request.opportunityId);
Record_Distribution__c TeamDistribution = relevantDistributions.get(request.distributionName);
reassignedOpp.OwnerId = distributionToMappings.get(request.distributionName)[Math.mod((Integer) TeamDistribution.Number_of_RR__c,
distributionToMappings.get(request.distributionName).size())].User__r.id;
lstRRrecord.add(newRecord);
TeamDistribution.Number_of_RR__c++;
lstOppsToUpdate.add(reassignedOpp);
}
insert lstRRrecord;
update lstOppsToUpdate;
update relevantDistributions.values();
update relevantUser.values();
}
}
Thanks.
Maharajan.C
Thanks for your input but it didn't work.
I initialized the
Then it worked.
I have put 0 in that field on the record, so why is it not working?
Thank you for your help.
Thanks everyone for helping.
I've gone through your requirement and the line number 57 is getting an error because your map is returning null for that user
means your ( request.OwnerId ) does not exist in the relevant user(i.e. Map) Ensure following things:
1.Record_Distribution_Mapping__c Users does not contain the user for the RRRequest OwnerId
2. You must create and analyze your debug logs that the map and List of RRequest contain the same users so you can get inside the loop.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com