You need to sign in to do that
Don't have an account?
ckellie
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
hi ckellie,
this should help + bulkify your code (i.e., let it handle large numbers of records in your trigger context.
Hope this helps.
regards,
Manu
Manu,
This is great, thank you so much. I have copied the code into my trigger and am coming up with an error though.
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
Opps forgot to instantiate the new Map collection.
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.
}