• Tushar khorgade 3
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi all, 
Greetings, 

I am facing a error while sending a email notification to the property (custom object Property__C) general manager. 
Error:
execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_TYPE_FOR_OPERATION, Only User, Contact, Lead, or Person objects are allowed for targetObjectId:


Apex Code: 
=========
for (case cass : casss){

if(cass.Notify_Property__C == True ){
Csid=cass.Property_Name__c;
}
Property__c pr = [select id, General_Manager_Email__c from Property__C where id =: csid]; 

propertyId.add(pr.General_Manager_Email__c); 
string Emailaddress = pr.General_Manager_Email__c; 
if (propertyId.size()> 0){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] address= new String[] {Emailaddress};
System.debug('Query email address ]]'+ address  );
mail.setToAddresses(address);
mail.setTargetObjectId(pr.id);

mail.setSenderDisplayName('Delivery notice');
EmailTemplate et=[Select id from EmailTemplate where Name=:'Product Delivery Notice'];
mail.setTemplateId(et.id);
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

can you help on the issue. if any code changes need  you can twist and place it in the post. 

Thanks