+ Start a Discussion

Illegal assignment from Id to LIST<Id>

I have a custom object linking Accounts and Opportunities. I am trying to get all the opportunity ids related to an account, using following query.



public Id[] Opps = [select Opportunity__c from  RA_Opp_Link__c where RingLead_Account__c = 'a0AT0000001nBrS'].Opportunity__c;


public Id Opps = [select Opportunity__c from  Custom_object__c where Account__c = 'some account id'].Opportunity__c;


The query works fine for one record but fails for multiple records with the following error:

System.QueryException: List has more than 1 row for assignment to SObject

To resolve the issue I changed public Id Opps to public Id[] Opps. but I started getting "Illegal assignment from Id to LIST<Id>" error. How can I change the query to accept multiple IDs? Thanks!





The cause is ].Opportunity__c;


I can suggest you change to for loop:

List<String> opps = new List<String>();

for(Object o = [Select ......]){







why not:


list<Custom_object__c> Opps = [select Opportunity__c from  Custom_object__c where Account__c = 'some account id'];


The problem is that when you do the select it can have one record or more than one. If there is more than one then you are matching an Id type variable with a List of Id's.

That why you need to do as i recommend the following


List <Id> Opss =  [select Opportunity__c from  Custom_object__c where Account__c = 'some account id'];


Then ask if there is any row (allways do this when you ask for a soql query)



   //Do what you want with the id in this part of the code.

  //To iterate on the list do the following

for (Id oppId : Opps){

     //Do anything you want with the id's