0

I'm trying to loop over CPQupdate but I keep getting an error and im not sure why.

Error: Loop must iterate over collection: CPQ_Notes_Attachments__c

Cpq_Notes_Attachments__c CPQupdate = [Select id,name,Leadform__c,Leadform_Type__c,Sales_Campaign__r.name,createdDate, Sales_Campaign__c,Snecma_Lead_name__c,Snecma_Regional_Lead_Name__c,GE_SSD_Name__c,GE_SSD_Email__c,Snecma_Lead_Emails__c,Snecma_Regional_Lead_Email__c,Trigger_Email__c,Sales_Campaign__r.GE_RGM__r.Email from cpq_notes_attachments__c where id =: accid];

for(Cpq_Notes_Attachments__c r: CPQupdate)
{
  //display message
}

4 Answers 4

7

Your soql returns List<CPQ_Notes_Attachments__c>, but you assign it to single CPQ_Notes_Attachments__c variable. Assign queried records to List<CPQ_Notes_Attachments__c> and after that you can foreach it

List<CPQ_Notes_Attachments__c> cpqUpdate = [
    select Id, Name, Leadform__c, Leadform_Type__c, CreatedDate, Snecma_Lead_name__c, Snecma_Regional_Lead_Name__c, GE_SSD_Name__c, GE_SSD_Email__c, Snecma_Lead_Emails__c, Snecma_Regional_Lead_Email__c, Trigger_Email__c,
        Sales_Campaign__c, Sales_Campaign__r.Name, Sales_Campaign__r.GE_RGM__r.Email
    from CPQ_Notes_Attachments__c
    where Id =: accid
    ];

for(CPQ_Notes_Attachments__c cpqNoteAttachment: cpqUpdate){
    //display message
}

if you are expecting to work with big number of records, you can use SOQL For Loops, to avoid the limit on heap size.

for(Cpq_Notes_Attachments__c cprNoteAttachment: [
    select Id, Name, Leadform__c, Leadform_Type__c, CreatedDate, Snecma_Lead_name__c, Snecma_Regional_Lead_Name__c, GE_SSD_Name__c, GE_SSD_Email__c, Snecma_Lead_Emails__c, Snecma_Regional_Lead_Email__c, Trigger_Email__c,
        Sales_Campaign__c, Sales_Campaign__r.Name, Sales_Campaign__r.GE_RGM__r.Email
    from Cpq_notes_attachments__c
    where Id =: accid
    ]){
    //display message
}
1

A SOQL query always returns a list of sObjects. And, since you are assigning the result of query to a Cpq_Notes_Attachments__c reference, Apex does the conversion automatically and assigns the only record in the list to CPQupdate.

Now, let's consider a scenario in which no records are returned by your query, in that case, it will result in Exception

System.QueryException: List has no rows for assignment to SObject

To avoid such issues, its recommended to assign the query result to an variable of type List

  1. Either iterate over the list of objects, if you want to manipulate all the objects.
  2. Or add a check to make sure the list is not empty and retrieve the only object from list and use that object.

So, in your case, you should modify your code to be like

List<Cpq_Notes_Attachments__c> CPQupdate = [Select id,name,Leadform__c,Leadform_Type__c,Sales_Campaign__r.name,createdDate, Sales_Campaign__c,Snecma_Lead_name__c,Snecma_Regional_Lead_Name__c,GE_SSD_Name__c,GE_SSD_Email__c,Snecma_Lead_Emails__c,Snecma_Regional_Lead_Email__c,Trigger_Email__c,Sales_Campaign__r.GE_RGM__r.Email from cpq_notes_attachments__c where id =: accid];

and then iterate over the list of records in a for loop.

Hope this helps.

3
  • As far as I know, I think a SOQL query returns a list of sObjects and its Apex which does the conversion to single object at runtime. And, that's the reason why we get that System.QueryException exception when the list is empty. Isn't it so?
    – jagmohan
    Aug 16, 2017 at 11:04
  • I've just tested this point one more time, and you're right. My apologizes. Aug 16, 2017 at 11:09
  • This answer doesn't quite address the error mentioned in the question (i.e. why trying to loop over a single instance, rather than a collection, is an issue), but it does go over some other very important points. +1 from me.
    – Derek F
    Aug 16, 2017 at 14:44
0

Yes! Make it a List of Cpq_Notes_Attachments__c

change it to

List<Cpq_Notes_Attachments__c >CPQupdate = [Select 
id,name,Leadform__c,Leadform_Type__c,Sales_Campaign__r.name,createdDate,  
Sales_Campaign__c,Snecma_Lead_name__c,Snecma_Regional_Lead_Name__c,
GE_SSD_Name__c,GE_SSD_Email__c,Snecma_Lead_Emails__c, 

Snecma_Regional_Lead_Email__c,Trigger_Email__c
,Sales_Campaign__r.GE_RGM__r.Email from cpq_notes_attachments__c where id =: 
accid];
1
  • 2
    While this would fix the issue that OP (Original Poster of the question) has, you haven't explained why this would work or why the original code was causing issues. Answers become a lot more helpful for OP (and other people, with similar problems, viewing this question in the future) when you take the time to explain the "why" in addition to the how (by how, I mean how to fix the problem).
    – Derek F
    Aug 16, 2017 at 14:39
0

In below your code, Your try to assign the list object to an single object. The SOQL your try to get is return the list object until you don't mention the SOQL limit to 1.

Cpq_Notes_Attachments__c CPQupdate = [Select id,name,Leadform__c,Leadform_Type__c,Sales_Campaign__r.name,createdDate, Sales_Campaign__c,Snecma_Lead_name__c,Snecma_Regional_Lead_Name__c,GE_SSD_Name__c,GE_SSD_Email__c,Snecma_Lead_Emails__c,Snecma_Regional_Lead_Email__c,Trigger_Email__c,Sales_Campaign__r.GE_RGM__r.Email from cpq_notes_attachments__c where id =: accid];

Change to Cpq_Notes_Attachments__c CPQupdate single Object instance to list of object. As below mentioned

   List<Cpq_Notes_Attachments__c> CPQupdate = [Select id,name,Leadform__c,Leadform_Type__c,Sales_Campaign__r.name,createdDate, Sales_Campaign__c,Snecma_Lead_name__c,Snecma_Regional_Lead_Name__c,GE_SSD_Name__c,GE_SSD_Email__c,Snecma_Lead_Emails__c,Snecma_Regional_Lead_Email__c,Trigger_Email__c,Sales_Campaign__r.GE_RGM__r.Email from cpq_notes_attachments__c where id =: accid];
2
  • 2
    While this would fix the issue that OP (Original Poster of the question) has, you haven't explained why this would work or why the original code was causing issues. Answers become a lot more helpful for OP (and other people, with similar problems, viewing this question in the future) when you take the time to explain the "why" in addition to the how (by how, I mean how to fix the problem).
    – Derek F
    Aug 16, 2017 at 14:37
  • Yup Sure........
    – NITHESH K
    Aug 17, 2017 at 5:44

You must log in to answer this question.