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
SashibansalSashibansal 

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

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9
Remove this line String toaddress = new String[] {List1.Email};
and
Change this
MessageEmail.setToAddresses (toaddress);
To
MessageEmail.setToAddresses (List1);

All Answers

Avidev9Avidev9

Error is in this line

 

  List<String> List1 = new List<String> ([Select Email from Contact where id in :Poc1IdSet]);

  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 

List<String> List1 = new List<String>();
For(Contact con : [Select Email from Contact where id in :Poc1IdSet]){
    List1.add(con.Email);
}

 Replace the query at line number 6 with the above code and it should work

SashibansalSashibansal

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

sushant sussushant sus
chznge it
List<String> List1 = new List<String> ([Select Email from Contact where id in :Poc1IdSet]);

list<contact>
Avidev9Avidev9
Remove this line String toaddress = new String[] {List1.Email};
and
Change this
MessageEmail.setToAddresses (toaddress);
To
MessageEmail.setToAddresses (List1);
This was selected as the best answer
ronycronyc

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

ronycronyc

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.