2

I'm trying to find out how can I pass the Attachment ParentId value that is part of a SOQL to a Set of Ids. Just testing things around.

Lets say I have the Set ready:

Set<Id> eveIds = new Set<Id>(); 

The newMap.keySet() is just a list of attachments coming from a SOQL (more playing around).

List<Attachment> listAtt = [SELECT Id, ParentId FROM Attachment];
Map<Id, Attachment> newMap = new Map<Id, Attachment>(listAtt);  

Now, here is the SOQL:

Map<Id, Attachment> attList = new Map<Id, Attachment>([SELECT id, ParentId, Parent.Type FROM Attachment WHERE Parent.Type = 'Event' AND Id IN : newMap.keySet()]);

All this is fine, I tested this query just with a few system.debug and is fine. What I want to know is how I can pass the "ParentId" to the eveIds set that is above.

I tried something like:

for(Attachment a : attList.values())
{
 eveIds.add(a.ParentId); 

 System.debug('This is the id of the attachemnts ' + a.Id); 
 System.debug('This is the parent id for the attachment ' + a.ParentId); 

 }

And is not passing the "ParentId" ids to the eveIds set. The System.debug are all fine, they are showing the correct ids.

This shouldn't be that hard, right? I missing something here... could anyone bring some light to this?. I will really appreciated it.
Thanks.

1 Answer 1

3

I don't see what is stopping your code working: are you sure it isn't? Unless it is too complicated, you would be better off posting the complete section of code rather than snippets in case something you have left out is causing the problem.

A simpler version of what you have listed would be:

Set<Id> eveIds = new Set<Id>();
for (Attachment a : [
        SELECT ParentId
        FROM Attachment
        WHERE Parent.Type = 'Event'
        ]) {
    eveIds.add(a.ParentId);
}
System.debug('eveIds=' + eveIds);

But without a more selective "where" clause you will hit a governor limit when the number of attachments gets large in your org.

2
  • Actually you are some how right. Is is not part of any particular code. I'm just using the developer console to run some codes for a trigger I will be working on. It may be that I'm missing some rules here. On the "Set<Id> eveIds = new Set<Id>();" I was running a System.debug right under: "System.debug(eveIds.size()); " . This system.debug was giving me "0". If I iterate through the Set eveIds and run a system.debug within that for loop, then I get the ids. This a be strange because as far as I know size() method can be use on Set collections, so ... can't get it. But thanks. May 28, 2015 at 13:04
  • @Viasur Sounds like you were outputting the size before anything had been added to the set.
    – Keith C
    May 28, 2015 at 13:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .