I have below code which throws List index out of bound error.

I am not getting where i do wrong here.


String Territory= '\''+ (String) p.get('Primary_Territory_vod__c')+'\'';

List<Territory> terr =[select Id, name , ParentTerritoryId  from territory where name =:Territory]; 
  • 1
    check again. this code looks fine to me. error comes from other pieces of code... somewhere you must be using index of list
    – Ratan Paul
    Jun 30, 2017 at 10:25

1 Answer 1


I do not see a LIMIT statement in the SOQL query, which is usually not a smart thing. And do you iter over the "terr" results? When "terr.size()" would return 0, and you try to get the first record "terr[0]", then you could expect this error.

See also: https://help.salesforce.com/articleView?id=000181121&type=1

  • SOQL Limit not required here as OP is fetching a specific Territory by name
    – cropredy
    Jun 30, 2017 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.