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

Invalid Initial value type
Hi team,
I am wrting a Trigger to send an email whenever a Visit__c (custom object) record is created. Visit__c has a custom field named Poc_1_c which is nothing but a look up to contact.
So now when I create a Visit record the email should go to the POC.
Below is the Trigger I wrote and the error I am getting
trigger Send_Email_Visit on Visit__c (after insert,after update) {
Set<Id> Poc1IdSet = new Set<Id>();
for(Visit__c Visit :Trigger.new){
Poc1IdSet.add(Visit.POC_1__c);
List<String> List1 = new List<String> ([Select Email from Contact where id in :Poc1IdSet]);
Messaging.SingleEmailMessage MessageEmail = new Messaging.SingleEmailMessage();
String toaddress = new String[] {List1.Email};
if(Visit.Send_Email__c ==True && Visit.Portfolio__c =='Walmart')
{
MessageEmail.setTemplateId('00XK0000000QN37');
MessageEmail.setTargetObjectId(Visit.CreatedByID);
MessageEmail.setToAddresses (toaddress);
MessageEmail.setSaveAsActivity(False);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {MessageEmail});
}
}
}
Error: Compile Error: Invalid initial value type LIST<Contact> for LIST<String> at line 6 column 24
Can ANyone please help me on this?
Thanks in advance
and
Change this
MessageEmail.setToAddresses (toaddress);
To
MessageEmail.setToAddresses (List1);
All Answers
Error is in this line
This line will return you List<Contact> not List<String>.
To get a list of email Id you have to iterate through the result set to add them to a List. Something like
Replace the query at line number 6 with the above code and it should work
Thanks for replying Avidev But i am getting another error here its
Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<String> at line 13 column 37
trigger Send_Email_Visit on Visit__c (after insert,after update) {
Set<Id> Poc1IdSet = new Set<Id>();
for(Visit__c Visit :Trigger.new){
Poc1IdSet.add(Visit.POC_1__c);
List<String> List1 = new List<String>();
for(Contact con :[Select Email from Contact where id in :Poc1IdSet]){
List1.add(con.Email);
}
Messaging.SingleEmailMessage MessageEmail = new Messaging.SingleEmailMessage();
String toaddress = new String[] {List1.Email};
if(Visit.Send_Email__c ==True && Visit.Portfolio__c =='Walmart')
{
MessageEmail.setTemplateId('00XK0000000QN37');
MessageEmail.setTargetObjectId(Visit.CreatedByID);
MessageEmail.setToAddresses (toaddress);
MessageEmail.setSaveAsActivity(False);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {MessageEmail});
}
}
}
thanks for your reply again
List<String> List1 = new List<String> ([Select Email from Contact where id in :Poc1IdSet]);
list<contact>
and
Change this
MessageEmail.setToAddresses (toaddress);
To
MessageEmail.setToAddresses (List1);
Team,
Do you think if the code below will hit the governor limit ?
here is the final code ?
trigger Send_Email_Visit on Visit__c (after insert,after update) {
Set<Id> Poc1IdSet = new Set<Id>();
for(Visit__c Visit :Trigger.new){
Poc1IdSet.add(Visit.POC_1__c);
List<String> List1 = new List<String>();
for(Contact con :[Select Email from Contact where id in :Poc1IdSet]){
List1.add(con.Email);
}
Messaging.SingleEmailMessage MessageEmail = new Messaging.SingleEmailMessage();
if(Visit.Send_Email__c ==True && Visit.Portfolio__c =='Walmart')
{
MessageEmail.setTemplateId('00XK0000000QN37');
MessageEmail.setTargetObjectId(Visit.CreatedByID);
MessageEmail.setToAddresses (List1);
MessageEmail.setSaveAsActivity(False);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {MessageEmail});
}
}
}
please reply
Hi Team,
The above code is working fine and the email is sent when the trigger is fired but the problem is that
the email template doesnt show up any value in the merge fields when the values actually exists in record.
any help on this issue is highly appreciated.