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
maddy27maddy27 

Messaging.SingleEmailMessage - settargetObjectId

Do we need to use only Standard object in setTargetObjectId ? or we can use both custom/Standard ?
Raj VakatiRaj Vakati
Its could be The ID of the contact, lead, or user to which the email will be sent. Refer the below code

 
EmailTemplate emailTemplate = [select Id, Subject, HtmlValue, Body from EmailTemplate where DeveloperName ='Test'];


Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();

	email.setReplyTo(fromaddress);
	email.setSenderDisplayName(fromaddress);
	String[] toAddresses = new String[] {ufEmail};
	email.setToAddresses(toAddresses);
	email.setTargetObjectId([Select Id from Contact Limit 1].id);
	email.setSaveAsActivity(true);
email.setTemplate(emailTemplate.Id);
	email.setSubject(subject);
	email.setHtmlBody(htmlBody);
	email.setPlainTextBody(plainBody);

	Messaging.sendEmail(new Messaging.SingleEmailmessage[] {email});

 
maddy27maddy27
Thanks for quick response.
So we can set CustomObject.Contact__c in setTargetObjectID ? 
Cheers!
Raj VakatiRaj Vakati
Yes by assuimg that  CustomObject.Contact__c is the lookup to the contacts .. (Contact__c))
maddy27maddy27
thanks again Raj