You need to sign in to do that
Don't have an account?

i jus need help in for loop
List<Contact> accounts=[SELECT Email FROM Contact WHERE AccountId ='001i000000g7erl' ORDER BY Email ASC NULLS FIRST LIMIT 3];
list<Id> emailid=new list<id>();
for(id acc: Email)
{
acc.emailid[Email];
}
wat is worng in this can u explain me im new to apex code
list<Id> emailid=new list<id>();
for(id acc: Email)
{
acc.emailid[Email];
}
wat is worng in this can u explain me im new to apex code
If what you would like is to setTargetObjectId with your contacts, you will need to specify the contact Id instead the eamil, as SetTargetObjectId needs an Id as parameter ( contact Id, user Id... :) )
Then in your case :
public static List<Contact> getContactsForAccount(Id accountId)
{
//here you could also check if you have access to the object before doing query :-)
return [Select Email, AccountId from Contact where AccountId=: accountId];
}
public void sendingEmails(Id accountId) // might be you would like to return something... as you need
{
//your code
List<Messaging.SingleEmailMessage> sendEmails = new List<Messaging.SingleEmailMessage>();
for(Contact contact: getContactsForAccount(accountId))
{
/// check if the email has been processed alreday using a Se<String> processedEmails;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(contact.Id);
//your code
}
//your code
}
Notice that I changed the method and not it returns a List. If the list is null , the for loop wont be executed that is fine but if there is any contacts that have same email it will be repeated , then might be you would like to check before create mail :-)
Hope it helps.
Kind Regards,
Carolina.
All Answers
I'll try to help :)
The code that you are using could be failing for several reasons, please have a look to the code below that is working and I added comments in yours to describe what I mean:
public Set<String> getContactEmailsForAccount(Id accountId)
{
// List<Contact> accounts=[SELECT Email FROM Contact WHERE AccountId ='001b000000NktwJ' ORDER BY Email ASC NULLS FIRST LIMIT 3]; --> the query is fine , however I like to call the list with using the object name of records that I get ;-)
List<Contact> contacts=[SELECT Email FROM Contact WHERE AccountId = :accountId ORDER BY Email ASC NULLS FIRST LIMIT 3];
// instead to hard code the Account Id I passed it through, only a better practice but it was working writing it directly too :)
//list<Id> emailid=new list<id>(); //emailId.add(contact.Email); --> you can't add email to this list because Email is not an Id , it will fails
//List<Email> myEmails ;
Set<String> contactEmails = new Set<String> (); // --> I used a Set this way you wont have repeated emails on it, the lists allows you to have repeated elements. Also I need to use a String type instead Id.
/* for(id acc: Email)
{ ---> the loop needs to be in the contact list that you just queried before*/
for(Contact contact: contacts)
{
//once inside we check if the contact has email and if it is not null then I added to the Set
if(contact.email!=null ){
contactEmails.add(contact.Email);
//acc.emailid[Email]; -- > I left this line here if you want to un-commented to see the error
}
}
return contactEmails; // at the end I return back the set with the contact Emails :-)
}
Hope the info helps.
Have a nice Monday,
Carolina.
im just using this code for mass email messaging can i use like his for
setTarget Objectid(contactEmails);
public Set<String> setEmails=new Set<String>();
for(Contact con: [Select Email, AccountId from Contact where Id=: accountId]){
if(con.Email !=null){
setEmails.add(con.Email);
}
}
if(!setEmails.isEmpty()){
return setEmails;
}
}
thanks for your tip , I left the query outside because the limit was 3 :-) But good one.
However if you leave this way you would have 3 issues :
public Set<String> setEmails=new Set<String>(); -- > Illegal modifier on local variable
... and when you remove public from it the next issue will be :
Non-void method might not return a value or might have statement after a return statement.
.. the last one will be the query won't return anything because of the filtering AccountId = :accountId ( i guess it is a typo :) )
It happens as you know because our method is not void and needs to return something,
I would personally check if the Set is empty on the call of the method when it is going to be use, but if you preffer to do it inside you could return null .
Thanks for the contributions Shailforce ,good tip that I forgot to comment :)
Kind Regards,
Carolina.
If what you would like is to setTargetObjectId with your contacts, you will need to specify the contact Id instead the eamil, as SetTargetObjectId needs an Id as parameter ( contact Id, user Id... :) )
Then in your case :
public static List<Contact> getContactsForAccount(Id accountId)
{
//here you could also check if you have access to the object before doing query :-)
return [Select Email, AccountId from Contact where AccountId=: accountId];
}
public void sendingEmails(Id accountId) // might be you would like to return something... as you need
{
//your code
List<Messaging.SingleEmailMessage> sendEmails = new List<Messaging.SingleEmailMessage>();
for(Contact contact: getContactsForAccount(accountId))
{
/// check if the email has been processed alreday using a Se<String> processedEmails;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(contact.Id);
//your code
}
//your code
}
Notice that I changed the method and not it returns a List. If the list is null , the for loop wont be executed that is fine but if there is any contacts that have same email it will be repeated , then might be you would like to check before create mail :-)
Hope it helps.
Kind Regards,
Carolina.
public static List<Contact> getContactsForAccount(Id accountId)
{
//here you could also check if you have access to the object before doing query :-)
if(accountId!=null)
return [Select Email, AccountId from Contact where AccountId=: accountId];
else
return new List<Contact> () ; // or return null
}
;-)