+ Start a Discussion
Venkata Sravan Kumar BandariVenkata Sravan Kumar Bandari 

How to get ids from SOQL

Here is soql i want get ids of attachments as follows and how can i retrieve ids from the list
 
list<Account> accs = [select (select id from attachments where createddate < last_N_days:10) from account where is_read__c = true]

Now i want ids of attachments into a seperate list how it is possible..

Thanks in advance
Best Answer chosen by Venkata Sravan Kumar Bandari
Jigar.LakhaniJigar.Lakhani

Hello,

You can use for loop iteration over accounts and its attachemnts to get ids of attachments.
In below code "setAttachmetIds" is set of your all attachments ids.

List<Account> accs = [select Id,(select id from attachments where createddate < last_N_days:10) from account where is_read__c = true]
Set<Id> setAttachmetIds = new Set<Id>();
for (Account objAccount:accs) {
	if (objAccount.attachments != null && objAccount.attachments.size() > 0) {
		for (Attachment objAttachment:objAccount.attachments) {
			setAttachmetIds.Add(objAttachment.Id);
		}
	}
}

Thanks & Cheers,
Jigar

All Answers

Jigar.LakhaniJigar.Lakhani

Hello,

You can use for loop iteration over accounts and its attachemnts to get ids of attachments.
In below code "setAttachmetIds" is set of your all attachments ids.

List<Account> accs = [select Id,(select id from attachments where createddate < last_N_days:10) from account where is_read__c = true]
Set<Id> setAttachmetIds = new Set<Id>();
for (Account objAccount:accs) {
	if (objAccount.attachments != null && objAccount.attachments.size() > 0) {
		for (Attachment objAttachment:objAccount.attachments) {
			setAttachmetIds.Add(objAttachment.Id);
		}
	}
}

Thanks & Cheers,
Jigar
This was selected as the best answer
Mahmood ButtMahmood Butt
Why are you using Account if you are not getting any field from it? If you do want to have attachments belonging to a particular account then something like this would be feasible.
List<Attachment> accountAttachments = [SELECT id, name FROM Attachments WHERE Parent=: reference_account_id];

 
Venkata Sravan Kumar BandariVenkata Sravan Kumar Bandari
Thank you Mr. Salesforce