You need to sign in to do that
Don't have an account?
alx
Null Pointer in Trigger
Hey all,
I have a NullPointerException in my trigger. The purpose of the trigger is to use Snapshot to find its parent Opportunity's Owner Id, then apply this Id to Snapshot.
trigger Insert_RelatedOwnerID_as_SnapShotPipeForecast_by_Opp on SnapShot_Pipe_Forecast_by_Opp__c (before insert) { Map<Id, SnapShot_Pipe_Forecast_by_Opp__c> smap = new Map<Id, SnapShot_Pipe_Forecast_by_Opp__c>([select id, Opportunity_lookup__r.OwnerId from SnapShot_Pipe_Forecast_by_Opp__c where id in :Trigger.newMap.keySet()]); for(SnapShot_Pipe_Forecast_by_Opp__c s: Trigger.new){ s.OwnerId = smap.get(s.Id).Opportunity_lookup__r.OwnerId; } }
The exception is thrown on line 2, column 191. I'm sure it's an easy fix, but I can't see it.
Any help is appreciated.
Try the following and post your debug log...
I added a debug line on the first iteration to build the lookup. Is it possible that the Opportunity_Lookup__c field is blank?
All Answers
You want to make this an after insert trigger for 2 reasons.
1. You won't be able to reference newMap in a before insert scenario.
2. Your for ... loop would also fail as s.Id would be null.
Ids won't yet be assigned during a before insert operation.
[edit below]
Alternatively - seeing that you're trying to assign the owner and might not be able to update from within an after operation you could amend your code as such to work as-is.
I have tried after insert, but i get a Read only error:
: execution of AfterInsert caused by: System FinalException: Record is read-only: Trigger.Insert_RelatedOwnerID_as_SnapShotPipeForecast_by_Opp: line 5, column 15
I have all read, write, execute rights to everything.. So I don't really understand what's going on here...
You can't do a direct assignment in an after trigger. see my edit above - it will take you to the promised land.
I had already crafted the original response before i noticed that you were trying to update a field on the record.
gtindu,
So i tried your code, it compiles and runs in sandbox, but when creating a new Snapshot object, it does not change the Snapshot Owner Id to the Opp ID. Instead the Owner Id is still me(the creator).
I appreciate your help!
alx,
Try adding a few System.Debug line in the for loop - and check the debug logs to see if the opportunity is being found and/or the ownerid is different.
gtindu,
Here is my debug log:
The line "Opportunity Map: {}". The Map is empty, so thats why i keep getting this Null Pointer stuff, right?
Try the following and post your debug log...
I added a debug line on the first iteration to build the lookup. Is it possible that the Opportunity_Lookup__c field is blank?
gtindu,
thank you for sticking with me dude.
Opportunity Relationship is returning null.
At the moment the trigger compiles and runs, but still does not change Snapshot Owner to Opp owner.
I dont understand why adding ss.Opportunity_lookup__c would return null, the code seems sound.
Which leads me to believe the custom object reference is at fault. When i use schema to try and run:
"Select s.Opportunity_lookup__c From SnapShot_Pipe_Forecast_by_Opp__c s"
I just get empty cells. In similar children of Opportunities with references back to their parents, there is some output with look up fields.
Is there somewhere in salesforce/sandbox where i can look at the documentation or code of custom objects? A previous dev created these things and it would be nice if i could follow his logic.
In this case - you're Opportunity lookup field is blank. The data isn't being provided.
If the field is not set with a value by the user - it will not be able to reference an opportunity owner... you could setup a validation rule to require it perhaps.
BAH,
I've been fooled. You're right the field wasn't getting populated properly.
Other than that the code seems to run just fine right now.
Thank you so much gtindu.
No problem - i'll expect you to return the favor on one of my questions in the future :smileyhappy:
Haha, i'll do my best to help you if that day comes.