+ Start a Discussion
Matt MetrosMatt Metros 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTrigger: execution of AfterInsert caused by: System.ListException: List index out of bounds: 1

public with sharing class NumberOfAccountsRelatedToUser {

	public static void triggerHandler( System.TriggerOperation triggerEvent , list<Account> newAccounts, List<Account> oldAccounts) {

		Set<ID> sdr_owner_IDs = new Set<ID>();
		Set<ID> account_owner_IDs = new set<ID>();


		if(triggerEvent == TriggerOperation.AFTER_INSERT){
			for(Account a: newAccounts){
				sdr_owner_IDs.add(a.SDR_Owner__c);
				account_owner_IDs.add(a.OwnerID);
			}
		}
		if(triggerEvent == TriggerOperation.AFTER_UPDATE){
			for(Account a: newAccounts){
				sdr_owner_IDs.add(a.SDR_Owner__c);
				account_owner_IDs.add(a.OwnerID);
			}
			for(Account a: oldAccounts){
				sdr_owner_IDs.add(a.SDR_Owner__c);
				account_owner_IDs.add(a.OwnerID);
			}
		}

		if(triggerEvent == TriggerOperation.AFTER_DELETE){
			for(Account a: oldAccounts){
				sdr_owner_IDs.add(a.SDR_Owner__c);
				account_owner_IDs.add(a.OwnerID);
			}
		}

		System.debug(sdr_owner_IDs);
		System.debug(account_owner_IDs);


		List<AggregateResult> sdr_prospect_accounts = [SELECT COUNT(ID) sdr_accounts, SDR_Owner__c FROM Account WHERE SDR_Owner__c in :sdr_owner_IDs and (type ='Prospect - Being Worked' OR type='Prospect - Open' ) GROUP BY SDR_Owner__c];
		List<AggregateResult> owner_prospect_accounts = [SELECT COUNT(ID) owner_accounts, OwnerID FROM Account WHERE OwnerID in :account_owner_IDs and (type ='Prospect - Being Worked' OR type='Prospect - Open' ) GROUP BY OwnerID];

		Map<Id, List<Integer>> userID_And_num_of_accounts = new Map<Id, List<Integer>>();

		for(AggregateResult ar: sdr_prospect_accounts){
			// put the id in the list
			userID_And_num_of_accounts.put((ID)ar.get('SDR_Owner__c'), new List<Integer>());

			// add the number of sdr accounts to that IDs map
			userID_And_num_of_accounts.get((ID)ar.get('SDR_Owner__c')).add((Integer)ar.get('sdr_accounts'));
			userID_And_num_of_accounts.get((ID)ar.get('SDR_Owner__c')).add(0);
		}

		for(AggregateResult ar: owner_prospect_accounts){

			// if the id already exists
			if(userID_And_num_of_accounts.containsKey((ID)ar.get('OwnerID'))){

				userID_And_num_of_accounts.get((ID)ar.get('OwnerID')).add(1, (Integer)ar.get('owner_accounts'));
			} else {
				userID_And_num_of_accounts.put((ID)ar.get('SDR_Owner__c'), new List<Integer>());

				userID_And_num_of_accounts.get((ID)ar.get('SDR_Owner__c')).add(0);
				userID_And_num_of_accounts.get((ID)ar.get('SDR_Owner__c')).add((Integer)ar.get('owner_accounts'));
			}
		}

		// now we want to update the users field
		List<ID> user_IDs = new List<ID>();

		for(ID user_ID: userID_And_num_of_accounts.keySet()){
			user_IDs.add(user_ID);
		}

		// now we want to get the actual user object
		List<User> users = [SELECT ID, SDR_Owner_Prospect__c , Account_Owner_Prospect__c FROM User WHERE ID in :user_IDs];


		List<User> users_to_update = new List<User>();
		for(User u: users){
			List<Integer> sdr_and_owner_accounts = userID_And_num_of_accounts.get(u.ID);
			u.SDR_Owner_Prospect__c = sdr_and_owner_accounts[0];
			u.Account_Owner_Prospect__c = sdr_and_owner_accounts[1];
			users_to_update.add(u);
		}

		// update users
		update users_to_update;

	}


}

Line 45 is saying I am out of bounds. Does not make sense.
Best Answer chosen by Matt Metros
PrasathPrasath
Hi Matt Metros,

If you then attempt to access an element at row 0, it'll throw an error as that row doesn't exist. It's good practice to check there are records first in order to make sure that the list is not empty before accessing it.

You are getting error because of the below line,
u.SDR_Owner_Prospect__c = sdr_and_owner_accounts[0];

u.Account_Owner_Prospect__c = sdr_and_owner_accounts[1];

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Prasath