You need to sign in to do that
Don't have an account?
System.NullPointerException: Attempt to de-reference a null object
I am trying to insert some data into SalesForce and I am getting this error when running it. It seems that the OwnerId is null if anyone with more expertise in apex programming could give a clue on how to solve this or print the value that is on Owner ID line 33, do a check or something.
Error Message:
UpdateUserQuota: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.UpdateUserQuota: line 33, column 1";s:10:"statusCode";s:36:"CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY";}s:2:"id";N;s:7:"success";b:0;}}'
Trigger.UpdateUserQuota :
trigger UpdateUserQuota on Opportunity (before update, before insert) {
if(OpportunityTriggerController.testTrigger != null && OpportunityTriggerController.testTrigger != 'updateUserQuota')
return;
//if(OpportunityTriggerController.testTrigger == null)
//return;
QuotaManager quotaManager = new QuotaManager();
List<Id> ownerIds = new List<Id>();
for(Opportunity opp: trigger.new)
{
ownerIds.add(opp.OwnerId);
}
Map<Id, User> owners = new Map<Id, User>([SELECT Id, Name, Enable_Targets__c FROM User WHERE Id IN :ownerIds]);
List<User_Quota__c> allExistingQuotas = [
SELECT
Id,
Year__c,
Quarter__c,
User__c
FROM
User_Quota__c
];
//List<Opportunity> oppsToUpdate = new List<Opportunity>();
for(Opportunity opp: trigger.new)
{
if(!owners.get(opp.OwnerId).Enable_Targets__c) /* <- Line 33 */
continue;
integer minMonth = 1;
integer todayMonth = Date.today().month();
if(todayMonth >= 1 && todayMonth <= 3)
minMonth = 1;
else if(todayMonth >= 4 && todayMonth <= 6)
minMonth = 4;
else if(todayMonth >= 7 && todayMonth <= 9)
minMonth = 7;
else if(todayMonth >= 10 && todayMonth <= 12)
minMonth = 9;
Date minDate = Date.newInstance(Date.today().year(), minMonth, 1);
if(opp.CloseDate <= minDate && opp.User_Quota__c == null)
{
continue;
}
User_Quota__c quota = quotaManager.getQuota(opp.CloseDate, opp.OwnerId, opp.Owner.Name, allExistingQuotas);
if(quota != null)
{
if(opp.User_Quota__c == null || opp.User_Quota__c != quota.Id)
{
opp.User_Quota__c = quota.Id;
//oppsToUpdate.add(opp);
}
}
else
{
opp.User_Quota__c = null;
}
}
//update oppsToUpdate;
}
I am new to Apex and SalesForce so I am struggling to solve this, could be a stupid thing but I don't see to solve you guys from the community could help me I would be glad ! Thank you.
Hi,
My mistake, sorry about that.
I have modified code as below. Please use it.
Let me know if you still see any problem.
Happy to help you!
All Answers
Hi,
Could you please replace your line#33 code with following 2 lines of code:-
Let me know if you still see this problem.
Happy to help you!
Error: Compile Error: Condition expression must be of type Boolean at line 33 column 7
Thank you digamberlucky !
Hi,
My mistake, sorry about that.
I have modified code as below. Please use it.
Let me know if you still see any problem.
Happy to help you!
Hi now I am getting another error:
UpdateUserQuota: execution of BeforeInsert
caused by: System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: 005D0000003QtlG:
Class.QuotaManager.createQuota: line 65, column 1
Class.QuotaManager.getQuota: line 49, column 1
Trigger.UpdateUserQuota: line 55, column 1";s:10:"statusCode";s:36:"CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY";}s:2:"id";N;s:7:"success";b:0;}i:1;O:8:"stdClass":3:{s:6:"errors";O:8:"stdClass":2:{s:7:"message";s:387:"UpdateUserQuota: execution of BeforeInsert
and
If you know what is happening please give me a little help.
Thanks a lot !
Hi,
Looks like you can't assign this user to ownerId field, could you please hard code, say your UserId as owner of quote and see still gets same error.
The Owner Id was deleted I changed the owner id that was causing the error, so now it's inserting all opportunities.
Thank you very much for fixing the apex code and sorting out this for me, I really appreciate !!!