You need to sign in to do that
Don't have an account?
![Patrick G. Brown Patrick G. Brown](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Null Pointer Exception - Only on Bulk Insert
I have a trigger that looks to see if the lookup field AVSFQB__Primary_Contact__c (looks up to Contact) has changed on an Opportunity and if so, deletes all Contact Roles and adds the value from the AVSFQB__Primary_Contact__c field.
Everything works perfectly except when I try to fire the trigger via DataLoader. I've handled null pointer issues before, but in this example, I'm checking for null on both the list of Opportunities as well as the field AVSFQB__Primary_Contact__c just to be sure. When executing via the ui, it works great. When trying to upload a set of records it fails.
Can someone point me in the right direction?
Everything works perfectly except when I try to fire the trigger via DataLoader. I've handled null pointer issues before, but in this example, I'm checking for null on both the list of Opportunities as well as the field AVSFQB__Primary_Contact__c just to be sure. When executing via the ui, it works great. When trying to upload a set of records it fails.
Can someone point me in the right direction?
trigger opportunityMaintenance on Opportunity (after insert, after update) { if(Trigger.isAfter){ if(Trigger.isUpdate || Trigger.isInsert){ set<Id> setOpportunityIds = new set<Id>() ; for(Opportunity opp : Trigger.new){ if(opp != null){ //Error on the next line: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object if((opp.AVSFQB__Primary_Contact__c != null) && (Trigger.oldMap.get(opp.Id).AVSFQB__Primary_Contact__c != Trigger.newMap.get(opp.Id).AVSFQB__Primary_Contact__c)) { setOpportunityIds.add(opp.Id) ; } } } if(!setOpportunityIds.isEmpty()) delete([SELECT Id from opportunityContactRole WHERE OpportunityID IN: setOpportunityIds]) ; List<Opportunity> oppList = new List<Opportunity>(); oppList = [select Id, Name, AVSFQB__Primary_Contact__c from Opportunity where Id in :Trigger.New]; for(Opportunity opp : oppList) { if (oppList.size()>0){ if(opp.AVSFQB__Primary_Contact__c != NULL){ opportunityContactRole ocr = new opportunityContactRole( ContactId = opp.AVSFQB__Primary_Contact__c, OpportunityId = opp.Id, Role = 'Decision Maker', IsPrimary = TRUE); insert ocr; } } } } } }
Please write if condition before below code:
the condition should to be like :
Choose it best answer if it helps you.
Thanks,
Satish kumar Prajapat.
Please share the exception line which is showng null.
try this -
trigger opportunityMaintenance on Opportunity (after insert, after update) {
if(Trigger.isAfter){
if(Trigger.isUpdate){
set<Id> setOpportunityIds = new set<Id>() ;
for(Opportunity opp : Trigger.new){
if(opp != null){
//Error on the next line: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object
if(opp.AVSFQB__Primary_Contact__c != null && Trigger.oldMap.get(opp.Id).AVSFQB__Primary_Contact__c != opp.AVSFQB__Primary_Contact__c)
{
setOpportunityIds.add(opp.Id) ;
}
}
}
if(setOpportunityIds != null && !setOpportunityIds.isEmpty())
delete([SELECT Id from opportunityContactRole WHERE OpportunityID IN: setOpportunityIds]) ;
}
if(Trigger.isinsert){
List<Opportunity> oppList = new List<Opportunity>();
opportunityContactRole ListOCR= new opportunityContactRole();
oppList = [select Id, Name, AVSFQB__Primary_Contact__c from Opportunity where Id in :Trigger.New];
if (oppList.size()>0){
for(Opportunity opp : oppList) {
if(opp.AVSFQB__Primary_Contact__c != NULL){
opportunityContactRole ocr = new opportunityContactRole(
ContactId = opp.AVSFQB__Primary_Contact__c,
OpportunityId = opp.Id,
Role = 'Decision Maker',
IsPrimary = TRUE);
ListOCR.add(ocr);
}
}
}
if(ListOCR.size()>0)
insert ListOCR;
}
}
}