Salesforce System.ListException: Row with null Id at index: 0

Salesforce System.ListException: Row with null Id at index: 0

Exception:

System.ListException: Row with null Id at index: 0

When we initialize a Map data type with Id as key and the record doesn’t have Id in it, then Salesforce Apex will throw System.ListException: Row with null Id at index: 0 exception.

Sample Code:

Account objAccount = new Account(
    Name = 'Test',
    Description = 'Test',
    Phone = '9999999999'
);
Map < Id, Account > mapAccount = new Map < Id, Account > (
    new List < Account > { objAccount }
);

Another Example:

Account objAccount = new Account(
    Name = 'Test',
    Description = 'Test',
    Phone = '9999999999'
);
insert objAccount;
Map < Id, AggregateResult > mapParentAccountAggregateResult = new Map < Id, AggregateResult >(
    [
        SELECT ParentId, COUNT( Id ) 
        FROM Account 
        GROUP BY ParentId 
    ]
);

In the above example, Account record is created with Parent Id as null. We are trying to initialize a Map data with Account Parent Id from the Aggregate SOQL since it is grouped by ParentId. Since the ParentId is null, it will throw System.ListException: Row with null Id at index: 0 exception

Leave a Reply