function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vivek ravivivek ravi 

can we use soql query in set method

im just wan o control the dublicate in emails ids in the contacts in accoun so that i wan to use set method

can i write my query like this

set <contact> conids=[select id,email from contact where id=:accountid];
set <id> contactemail=new set<id>();
for(contact con:conids)
{
contactemail.add(con.id)

}
can i write like this
if any one knw help me in this
Ashish_SFDCAshish_SFDC
Hi Vivek, 


Yes that will work but you have to use a more selective query to avoid governor limits. 


Regards,
Ashish
Andries.NeyensAndries.Neyens
Even easier:

Map<Id, Contact> contacts = new Map<Id, Contact>([SELECT id, email FROM Contact where AccountId=:accountId]);

contacts.KeySet() will contain the id's
praveen murugesanpraveen murugesan
Hi Vivek,

set <contact> conids=[select id from contact where id!=:null]; // you can't use this. Becoz this ll return illegal assignment from list to set

you can you Andries way this is best way,

map<Id, Contact> contacts = new Map<Id, Contact>([SELECT id, email FROM Contact where AccountId=:accountId]);

contacts.KeySet() 
  //this will contain the unique contacts id's

else you can use this

list<Contact> contacts = new list<Contact>([SELECT id, email FROM Contact where AccountId=:accountId]);
set<id> idval;

for(Contact c: contacts)
{
idval.add(c.id)    //this will contain the unique contacts id's
}