The code below causes the error mentioned in the title. Not sure what this means or how to fix it.

        Map <ID,ID> mapID = new Map<ID,ID>([SELECT ContactId, OpportunityId FROM OpportunityContactRole]);

1 Answer 1


You can't initialize a Map<Id, Id> from a SOQL query result. Passing a SOQL query (or list of sObjects) to the Map constructor is valid for producing a Map<Id, sObject>.

It's not clear from your question which Id you want to map to which other Id, but in any case, you'll need to iterate over the query results to build your Map.

  • He could group the query by needed Id field and rename it like this [SELECT ContactId Id, OpportunityId FROM OpportunityContactRole GROUP BY ContactId] this then can be used in the Map<Id, AggregatedResult> constructor. But this approach has its own limitation.
    – Damecek
    Aug 7, 2021 at 9:24
  • Thx @DavidReed.
    – Ibrahim
    Aug 7, 2021 at 20:19

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.