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
sylar20sylar20 

register trigger email as an activity

I could crack logging a trigger email as an activty but it sends the email twice and it also sends the email to contact email id too??

I just wanna send an email to CLient_Email_Address__c

 

Any idea?

 

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    String[] toAddresses = new String[] {c.CLient_Email_Address__c};
    mail.setToAddresses(toAddresses);
    mail.setTargetObjectId(c.ContactId);
    mail.setwhatId(c.Id);
    EmailTemplate et = [SELECT id FROM EmailTemplate WHERE developerName = 'Completion_Notification'];
    mail.setTemplateId(et.id);
    mail.saveAsActivity = true;
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

Best Answer chosen by Admin (Salesforce Developers) 
gm_sfdc_powerdegm_sfdc_powerde

If you set the what id, the default behavior is to send an email to the standard email associated with what id (lead, contact or user) and unfortunately there is no easy way to override that.  If you are particular about sending email to an email address from a custom field and still log it as an activity, you could leave saveAsActivity as false and then then log the activity yourself (An insert of task record with who id, what id, subject, description and status set).  This would require some heavy lifting on your part. But I am afraid this is the best way to address both requirements.

All Answers

gm_sfdc_powerdegm_sfdc_powerde

If you set the what id, the default behavior is to send an email to the standard email associated with what id (lead, contact or user) and unfortunately there is no easy way to override that.  If you are particular about sending email to an email address from a custom field and still log it as an activity, you could leave saveAsActivity as false and then then log the activity yourself (An insert of task record with who id, what id, subject, description and status set).  This would require some heavy lifting on your part. But I am afraid this is the best way to address both requirements.

This was selected as the best answer
sylar20sylar20

The challenge now is that the same email is going twice.

 

following code for sending email (part of class)

 

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    String[] toAddresses = new String[] {c.Client_Email_Address__c};
    if(c.Client_Email_Address_Optional__c != null){
    String[] toAddresses2 = new String[] {c.Client_Email_Address_Optional__c};
    mail.setCcAddresses(toAddresses2);}
    mail.setToAddresses(toAddresses);
     EmailTemplate et = [SELECT id FROM EmailTemplate WHERE developerName = 'Dispute_Notification'];
    mail.setTemplateId(et.id);
    mail.setTargetObjectId(c.ContactId);
    mail.setwhatId(c.Id);
    mail.saveAsActivity = true;
    mail.setOrgWideEmailAddressId('0D2S0056674CiI');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

hgarghgarg

Chek you email settings:

Follow the left setup menu :- Personal setup -->Email --> My Email Settings --> Uncheck Automatic BCC option

Hope it works for you.