+ Start a Discussion
ckellieckellie 

Error: List Index Out of Bounds

I am writting a trigger that will take the name of the Product Specialist on the Opportunity Sales Team and placing the name on the custom object that is related to the opportunity. I am recieving the following error:

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ProductSpecialists: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0 Trigger.ProductSpecialists: line 17, column 37: []

 

 

Here is the trigger:

 

trigger ProductSpecialists on Custom_Layout_Object__c(before insert, before update) {

    Set<Id> layoutid = new Set<Id>();
     for(Custom_Layout_Object__c o : trigger.new){ 
        System.debug('**** 0 opp id : '+ o.id); 
        LayoutId.add(o.id); 

    List<Custom_Layout_Object__c> clo = [Select id, Opportunity__c, Product_Specialist__c from
        Custom_Layout_Object__c WHERE id in:LayoutID limit 1]; 

    List<Opportunity> opp = [Select id from Opportunity limit 1];
    
    List<OpportunityTeamMember> Otms = [Select id, opportunityid, 
    TeamMemberRole, UserId, user.name from OpportunityTeamMember where
    TeamMemberRole = 'Product Specialist' and opportunityid in:opp limit 1];  

     o.Product_Specialist__c = otms[0].User.name;
     
     }
}

 

 

The error is on the last line.

 

o.Product_Specialist__c = otms[0].User.name;

 

I believe that I need to put this inside a for statement but am not sure. Any Suggestions of what I can do?

 

Thank you

Manu ErwinManu Erwin

hi ckellie,

this should help + bulkify your code (i.e., let it handle large numbers of records in your trigger context.

 

 

trigger ProductSpecialists on Custom_Layout_Object__c(before insert, before update) {

  // Obtain Opportunity Ids for ALL records in the trigger
  Set<Id> setOpptyIds = new Set<Id>();
  for (Custom_Layout_Object__c custLayoutObj : trigger.new) {
    System.debug('**** 0 opp id : '+ custLayoutObj.OpportunityId); 
    setOpptyIds.add(custLayoutObj.Opportunity__c);
  }

  // Now use the parent Opportunity Ids to query
  // for ALL Opportunity Team Member records
  // for ALL the Custom Layout Object records in the trigger
  Map<Id, String> mapOpptyIdsToOpptyTeamMemberNames = Map<Id, String>();
  for (OpportunityTeamMember otm : [SELECT Id,
      OpportunityId, TeamMemberRole,
      UserId, User.Name
      FROM OpportunityTeamMember
      WHERE TeamMemberRole = 'Product Specialist'
      AND OpportunityId IN :setOpptyIds]) {
    mapOpptyIdsToOpptyTeamMemberNames.put(otm.OpportunityId, User.Name);
  }

  // Now loop through the records in the trigger again,
  // populating the Product_Specialist__c field
  // with the Name from the map collection
  for (Custom_Layout_Object__c custLayoutObj2 : trigger.new) {
    custLayoutObj2.Product_Specialist__c = mapOpptyIdsToOpptyTeamMemberNames.get(custLayoutObj2.Opportunity__c);
  }
}

 

 

 

 

Hope this helps.

 

regards,

Manu

ckellieckellie

Manu,

 

This is great, thank you so much. I have copied the code into my trigger and am coming up with an error though.

 

 

Error: Compile Error: unexpected token: 'Map' at line 13 column 54

 

 

I originally thought this would be a simple punctuation error, but that is not what is happening. Why else would I be getting this error? How can it be fixed.

 

Thank you very much

ckellie

Manu ErwinManu Erwin

Opps forgot to instantiate the new Map collection.

 

 

Map<Id, String> mapOpptyIdsToOpptyTeamMemberNames = new Map<Id, String>();

 

 

Pradeep_NavatarPradeep_Navatar

Before using the data of the “Otms” List first you need to check whether it contains any record or not If at run time the size of the “Otms” List will be zero then it will give error so better to handle it in code. Below is the correct code-

 

                if(Otms.size() > 0)

                {

                                o.Product_Specialist__c = otms[0].User.name;

                }

                else

                {

                                // give some default value in o.Product_Specialist__c, if it is a required field.

                }