function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDCDevQASFDCDevQA 

Null Reference Error only on upload

I'm receiving a null reference error only when uploading.  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 gets stopped on upload.  It will only insert them individually. I guess I must have bulked this improperly but I'm not sure where the hangup is steming from.  The error is pointing to line 110 but I'm not sure that is the actual issue because like I said the records are fine individually.  So maybe its getting confused when cross referencing the lists?  Any help would be greatly appreciated.

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;
}
}
 
Hargobind_SinghHargobind_Singh
Does your debug log also tell you the line number ? Would save some time for some to read through the whole trigger :) 
SFDCDevQASFDCDevQA
I don't think the email reply works correctly because I emailed a reply comment Friday and it obviously never showed up.  I'm so sorry, the line number is 110.  It is the line that says " for ( AccountTeamMember accountTeamMember : accountIdToAccountTeamMembers.get(accountId) )  {"
Thanks so much,
Amanda