+ Start a Discussion

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){
  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.setToAddresses (toaddress);
        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


Hello Sashibansal,


Replace the following line


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




String List1 = [Select Email from Contact where id in :Poc1IdSet].Email;


and replace this one

String toaddress = new String[] {List1.Email};


String[] toaddress = new String[] {List1l};



I would not go for a Set<Id> variable the way you are using it because if you have multiple Visit__c that are updated/ created, you will have no mean of knowing which Contacts is retrieved by your query as your Set is never empty.


I suggest that you either do not use it at all and simply receplace your query with  " where id =: Visit.POC_1__c"

or use a for loop to populate your Set<Id> variable with all the POC values then query all the email in one soql. 


Hope it helps,