You need to sign in to do that
Don't have an account?

Null Reference Error on Upload Only but not every time
I'm receiving a null reference error only when uploading and strangely not always when uploading. The error is pointing to line 110 but I'm not sure that is the actual issue because records are fine individually and sometimes they don't cause problems at all. So maybe its getting confused when cross referencing the lists? I can't seem to find a reason for why in the upload they are causing this null reference error but not individually. At first I thought my qualification that it end if there were no eligible account team records was the issue but I've tried that and it worked on an individual one. Even a small group of 20 records sometimes gets stopped on upload. I'm concerned I may have bulked it improperly and that is what is causing the error. Any help would be greatly appreciated, even if it is to say that I did bulk it correctly and I need to look elsewhere at another code or something that could be causing the actual problem.
Thanks so much,
Amanda
trigger ProductCopyActTeamtoOpportunity on OpportunityLineItem (after insert) {
List<OpportunityTeamMember> oppTeamMembersList = new List<OpportunityTeamMember>();
//Get Account ID from Opportunity when products are added
List<OpportunityLineItem> OLIAccountIDs = new List<OpportunityLineItem>([SELECT Opportunity.AccountId, OpportunityID, Quantity, Product2.Family FROM OpportunityLineItem WHERE Id IN: Trigger.newMap.keySet()]);
Set<Id> accountIds = new Set<Id>();
for(OpportunityLineItem oli : OLIAccountIDs){
accountIds.add(oli.Opportunity.AccountId);
}
Map< Id , Id > OpportunityLineItemIdToAccountId = new Map< Id , Id >();
for(OpportunityLineItem oli :OLIAccountIDs) {
if(oli.Quantity > 0.0) {
OpportunityLineItemIdToAccountId.put(oli.opportunityid, oli.opportunity.accountid );
System.Debug(oli.quantity);
System.Debug(oli.id);
System.debug(oli.opportunityid);
system.debug(oli.opportunity.accountid);
}
}
//Map from Account ID to get Standard Account Team Members
Map<id, List<AccountTeamMember > > accountIdToAccountTeamMembers = new Map<id, List<AccountTeamMember > > ();
for(AccountTeamMember accountTeamMember : [SELECT a.UserId,a.User.Name,a.TeamMemberRole, a.Id, a.AccountId
FROM AccountTeamMember a
WHERE a.TeamMemberRole IN ('Partner', 'Partner Manager','QB', 'QB Manager') AND
a.AccountId in :OpportunityLineItemIdToAccountId.values() ]) {
List<AccountTeamMember > accountTeamMembers = ( accountIdToAccountTeamMembers.get(accountTeamMember.Accountid) == null) ?
new List<AccountTeamMember >() :
accountIdToAccountTeamMembers.get(accountTeamMember.Accountid);
accountTeamMembers.add(accountTeamMember);
accountIdToAccountTeamMembers.put(accountTeamMember.Accountid, accountTeamMembers);
system.debug(accountIdToAccountTeamMembers.size());
}
//Map Account ID for Hayden McNeil
Map< Id , Id > HMspecialistOpportunityIDtoAccountID = new Map< Id , Id >();
for(OpportunityLineItem oli :OLIAccountIDs) {
// check if any of the product families we care about are used
if(oli.Product2.Family == 'Hayden-McNeil') {
HMspecialistOpportunityIDtoAccountID.put(oli.opportunityid, oli.Opportunity.AccountId );
System.Debug(oli.quantity);
System.Debug(oli.Product2.Family);
System.Debug(oli.id);
System.debug(oli.opportunityid);
system.debug(oli.opportunity.accountid);
}
}
//Map from Account ID to get Specialists for HM
Map<id, List<AccountTeamMember > > HMspecialistaccountIdToAccountTeamMembers = new Map<id, List<AccountTeamMember > > ();
for(AccountTeamMember HMspecialistaccountTeamMember : [SELECT a.UserId,a.User.Name,a.TeamMemberRole, a.Id, a.AccountId
FROM AccountTeamMember a
WHERE a.TeamMemberRole IN ('HM Specialist','HM Specialist Manager') AND
a.AccountId in :HMspecialistOpportunityIDtoAccountID.values() ]) {
List<AccountTeamMember > HMspecialistaccountTeamMembers = ( HMspecialistaccountIdToAccountTeamMembers.get(HMspecialistaccountTeamMember.Accountid) == null) ?
new List<AccountTeamMember >() :
HMspecialistaccountIdToAccountTeamMembers.get(HMspecialistaccountTeamMember.Accountid);
HMspecialistaccountTeamMembers.add(HMspecialistaccountTeamMember);
HMspecialistaccountIdToAccountTeamMembers.put(HMspecialistaccountTeamMember.Accountid, HMspecialistaccountTeamMembers);
}
//Map Account ID for iClicker
Map< Id , Id > iClickerspecialistOpportunityIDtoAccountID = new Map< Id , Id >();
for(OpportunityLineItem oli :OLIAccountIDs) {
// check if any of the product families we care about are used
if(oli.Product2.Family == 'i>clicker') {
iClickerspecialistOpportunityIDtoAccountID.put(oli.opportunityid, oli.Opportunity.AccountId );
System.Debug(oli.quantity);
System.Debug(oli.Product2.Family);
System.Debug(oli.id);
System.debug(oli.opportunityid);
system.debug(oli.opportunity.accountid);
}
}
//Map from Account ID to get Specialists for iClicker
Map<id, List<AccountTeamMember > > iClickerspecialistaccountIdToAccountTeamMembers = new Map<id, List<AccountTeamMember > > ();
for(AccountTeamMember iClickerspecialistaccountTeamMember : [SELECT a.UserId,a.User.Name,a.TeamMemberRole, a.Id, a.AccountId
FROM AccountTeamMember a
WHERE a.TeamMemberRole IN ('Reef Specialist','Reef Specialist Manager') AND
a.AccountId in :iClickerspecialistOpportunityIDtoAccountID.values() ]) {
List<AccountTeamMember > iClickerspecialistaccountTeamMembers = ( iClickerspecialistaccountIdToAccountTeamMembers.get(iClickerspecialistaccountTeamMember.Accountid) == null) ?
new List<AccountTeamMember >() :
iClickerspecialistaccountIdToAccountTeamMembers.get(iClickerspecialistaccountTeamMember.Accountid);
iClickerspecialistaccountTeamMembers.add(iClickerspecialistaccountTeamMember);
iClickerspecialistaccountIdToAccountTeamMembers.put(iClickerspecialistaccountTeamMember.Accountid, iClickerspecialistaccountTeamMembers);
}
if( accountIdToAccountTeamMembers.keySet().size()> 0 ) // if there are no eligible Account Team records, end
{
for(OpportunityLineItem oli :OLIAccountIDs) {
if(oli.quantity > 0.0) {
Id accountId = OpportunityLineItemIdToAccountId.get(oli.opportunityid);
System.debug(accountid);
System.Debug(oli.product2.Family);
system.debug(accountidtoaccountteammembers.get(accountid));
for ( AccountTeamMember accountTeamMember : accountIdToAccountTeamMembers.get(accountId) ) {
OpportunityTeamMember opportunityTeamMember = new OpportunityTeamMember();
opportunityTeamMember.UserId = accountTeamMember.UserId;
opportunityTeamMember.TeamMemberRole = accountTeamMember.TeamMemberRole;
system.debug (accountTeamMember.TeamMemberRole);
opportunityTeamMember.OpportunityId = oli.opportunityID;
oppTeamMembersList.add(opportunityTeamMember);
}
}
if(oli.Product2.Family == 'Hayden-McNeil') {
System.Debug(oli.product2.Family);
Id HMaccountId = HMspecialistOpportunityIDtoAccountID.get(oli.opportunityId);
System.Debug(HMaccountId);
for ( AccountTeamMember accountTeamMember : HMspecialistaccountIdToAccountTeamMembers.get(HMaccountId) ) {
OpportunityTeamMember opportunityTeamMember = new OpportunityTeamMember();
opportunityTeamMember.UserId = accountTeamMember.UserId;
opportunityTeamMember.TeamMemberRole = accountTeamMember.TeamMemberRole;
system.debug (accountTeamMember.TeamMemberRole);
opportunityTeamMember.OpportunityId = oli.opportunityID;
oppTeamMembersList.add(opportunityTeamMember);
}
}
if(oli.Product2.Family == 'i>clicker') {
System.Debug(oli.product2.Family);
Id iClickeraccountId = iClickerspecialistOpportunityIDtoAccountID.get(oli.opportunityId);
System.Debug(iClickeraccountId);
for ( AccountTeamMember accountTeamMember : iClickerspecialistaccountIdToAccountTeamMembers.get(iClickeraccountId) ) {
OpportunityTeamMember opportunityTeamMember = new OpportunityTeamMember();
opportunityTeamMember.UserId = accountTeamMember.UserId;
opportunityTeamMember.TeamMemberRole = accountTeamMember.TeamMemberRole;
system.debug (accountTeamMember.TeamMemberRole);
opportunityTeamMember.OpportunityId = oli.opportunityID;
oppTeamMembersList.add(opportunityTeamMember);
}
}
}
insert oppTeamMembersList;
}
}
Thanks so much,
Amanda
trigger ProductCopyActTeamtoOpportunity on OpportunityLineItem (after insert) {
List<OpportunityTeamMember> oppTeamMembersList = new List<OpportunityTeamMember>();
//Get Account ID from Opportunity when products are added
List<OpportunityLineItem> OLIAccountIDs = new List<OpportunityLineItem>([SELECT Opportunity.AccountId, OpportunityID, Quantity, Product2.Family FROM OpportunityLineItem WHERE Id IN: Trigger.newMap.keySet()]);
Set<Id> accountIds = new Set<Id>();
for(OpportunityLineItem oli : OLIAccountIDs){
accountIds.add(oli.Opportunity.AccountId);
}
Map< Id , Id > OpportunityLineItemIdToAccountId = new Map< Id , Id >();
for(OpportunityLineItem oli :OLIAccountIDs) {
if(oli.Quantity > 0.0) {
OpportunityLineItemIdToAccountId.put(oli.opportunityid, oli.opportunity.accountid );
System.Debug(oli.quantity);
System.Debug(oli.id);
System.debug(oli.opportunityid);
system.debug(oli.opportunity.accountid);
}
}
//Map from Account ID to get Standard Account Team Members
Map<id, List<AccountTeamMember > > accountIdToAccountTeamMembers = new Map<id, List<AccountTeamMember > > ();
for(AccountTeamMember accountTeamMember : [SELECT a.UserId,a.User.Name,a.TeamMemberRole, a.Id, a.AccountId
FROM AccountTeamMember a
WHERE a.TeamMemberRole IN ('Partner', 'Partner Manager','QB', 'QB Manager') AND
a.AccountId in :OpportunityLineItemIdToAccountId.values() ]) {
List<AccountTeamMember > accountTeamMembers = ( accountIdToAccountTeamMembers.get(accountTeamMember.Accountid) == null) ?
new List<AccountTeamMember >() :
accountIdToAccountTeamMembers.get(accountTeamMember.Accountid);
accountTeamMembers.add(accountTeamMember);
accountIdToAccountTeamMembers.put(accountTeamMember.Accountid, accountTeamMembers);
system.debug(accountIdToAccountTeamMembers.size());
}
//Map Account ID for Hayden McNeil
Map< Id , Id > HMspecialistOpportunityIDtoAccountID = new Map< Id , Id >();
for(OpportunityLineItem oli :OLIAccountIDs) {
// check if any of the product families we care about are used
if(oli.Product2.Family == 'Hayden-McNeil') {
HMspecialistOpportunityIDtoAccountID.put(oli.opportunityid, oli.Opportunity.AccountId );
System.Debug(oli.quantity);
System.Debug(oli.Product2.Family);
System.Debug(oli.id);
System.debug(oli.opportunityid);
system.debug(oli.opportunity.accountid);
}
}
//Map from Account ID to get Specialists for HM
Map<id, List<AccountTeamMember > > HMspecialistaccountIdToAccountTeamMembers = new Map<id, List<AccountTeamMember > > ();
for(AccountTeamMember HMspecialistaccountTeamMember : [SELECT a.UserId,a.User.Name,a.TeamMemberRole, a.Id, a.AccountId
FROM AccountTeamMember a
WHERE a.TeamMemberRole IN ('HM Specialist','HM Specialist Manager') AND
a.AccountId in :HMspecialistOpportunityIDtoAccountID.values() ]) {
List<AccountTeamMember > HMspecialistaccountTeamMembers = ( HMspecialistaccountIdToAccountTeamMembers.get(HMspecialistaccountTeamMember.Accountid) == null) ?
new List<AccountTeamMember >() :
HMspecialistaccountIdToAccountTeamMembers.get(HMspecialistaccountTeamMember.Accountid);
HMspecialistaccountTeamMembers.add(HMspecialistaccountTeamMember);
HMspecialistaccountIdToAccountTeamMembers.put(HMspecialistaccountTeamMember.Accountid, HMspecialistaccountTeamMembers);
}
//Map Account ID for iClicker
Map< Id , Id > iClickerspecialistOpportunityIDtoAccountID = new Map< Id , Id >();
for(OpportunityLineItem oli :OLIAccountIDs) {
// check if any of the product families we care about are used
if(oli.Product2.Family == 'i>clicker') {
iClickerspecialistOpportunityIDtoAccountID.put(oli.opportunityid, oli.Opportunity.AccountId );
System.Debug(oli.quantity);
System.Debug(oli.Product2.Family);
System.Debug(oli.id);
System.debug(oli.opportunityid);
system.debug(oli.opportunity.accountid);
}
}
//Map from Account ID to get Specialists for iClicker
Map<id, List<AccountTeamMember > > iClickerspecialistaccountIdToAccountTeamMembers = new Map<id, List<AccountTeamMember > > ();
for(AccountTeamMember iClickerspecialistaccountTeamMember : [SELECT a.UserId,a.User.Name,a.TeamMemberRole, a.Id, a.AccountId
FROM AccountTeamMember a
WHERE a.TeamMemberRole IN ('Reef Specialist','Reef Specialist Manager') AND
a.AccountId in :iClickerspecialistOpportunityIDtoAccountID.values() ]) {
List<AccountTeamMember > iClickerspecialistaccountTeamMembers = ( iClickerspecialistaccountIdToAccountTeamMembers.get(iClickerspecialistaccountTeamMember.Accountid) == null) ?
new List<AccountTeamMember >() :
iClickerspecialistaccountIdToAccountTeamMembers.get(iClickerspecialistaccountTeamMember.Accountid);
iClickerspecialistaccountTeamMembers.add(iClickerspecialistaccountTeamMember);
iClickerspecialistaccountIdToAccountTeamMembers.put(iClickerspecialistaccountTeamMember.Accountid, iClickerspecialistaccountTeamMembers);
}
if( accountIdToAccountTeamMembers.keySet().size()> 0 ) // if there are no eligible Account Team records, end
{
for(OpportunityLineItem oli :OLIAccountIDs) {
if(oli.quantity > 0.0) {
Id accountId = OpportunityLineItemIdToAccountId.get(oli.opportunityid);
System.debug(accountid);
System.Debug(oli.product2.Family);
system.debug(accountidtoaccountteammembers.get(accountid));
for ( AccountTeamMember accountTeamMember : accountIdToAccountTeamMembers.get(accountId) ) {
OpportunityTeamMember opportunityTeamMember = new OpportunityTeamMember();
opportunityTeamMember.UserId = accountTeamMember.UserId;
opportunityTeamMember.TeamMemberRole = accountTeamMember.TeamMemberRole;
system.debug (accountTeamMember.TeamMemberRole);
opportunityTeamMember.OpportunityId = oli.opportunityID;
oppTeamMembersList.add(opportunityTeamMember);
}
}
if(oli.Product2.Family == 'Hayden-McNeil') {
System.Debug(oli.product2.Family);
Id HMaccountId = HMspecialistOpportunityIDtoAccountID.get(oli.opportunityId);
System.Debug(HMaccountId);
for ( AccountTeamMember accountTeamMember : HMspecialistaccountIdToAccountTeamMembers.get(HMaccountId) ) {
OpportunityTeamMember opportunityTeamMember = new OpportunityTeamMember();
opportunityTeamMember.UserId = accountTeamMember.UserId;
opportunityTeamMember.TeamMemberRole = accountTeamMember.TeamMemberRole;
system.debug (accountTeamMember.TeamMemberRole);
opportunityTeamMember.OpportunityId = oli.opportunityID;
oppTeamMembersList.add(opportunityTeamMember);
}
}
if(oli.Product2.Family == 'i>clicker') {
System.Debug(oli.product2.Family);
Id iClickeraccountId = iClickerspecialistOpportunityIDtoAccountID.get(oli.opportunityId);
System.Debug(iClickeraccountId);
for ( AccountTeamMember accountTeamMember : iClickerspecialistaccountIdToAccountTeamMembers.get(iClickeraccountId) ) {
OpportunityTeamMember opportunityTeamMember = new OpportunityTeamMember();
opportunityTeamMember.UserId = accountTeamMember.UserId;
opportunityTeamMember.TeamMemberRole = accountTeamMember.TeamMemberRole;
system.debug (accountTeamMember.TeamMemberRole);
opportunityTeamMember.OpportunityId = oli.opportunityID;
oppTeamMembersList.add(opportunityTeamMember);
}
}
}
insert oppTeamMembersList;
}
}
Add null and size check before using list in loop in line no 110.