You need to sign in to do that
Don't have an account?
sending SingleEmailMessage with setSaveAsActivity(true)
Describing the goal:
We have a need to send an email to an external email address (which we did) but we also wants salesforce to add a task for this action.
We managed to do this from a send an email button in the following way (you can see in the image)

The problem is that we can't reproduce this behavior with apex code.
we tried this with the code below : The Email sent but Task not created
[ Please be advised - we do not want to use message.setTargetObjectId() becuase we don't want to send an email to the contact ]
List<string> toList = new List<string>();
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
toList.add('any@external.email');
message.setUseSignature(false);
message.setSaveAsActivity(true);
message.setSubject('Any subject);
message.setHtmlBody('any Body');
message.setWhatId('00111000007dqTk'); // THIS IS AN ID OF THE SIMPSON'S FAMILY
message.setToAddresses(toList);
Messaging.sendEmail(message);
We have a need to send an email to an external email address (which we did) but we also wants salesforce to add a task for this action.
We managed to do this from a send an email button in the following way (you can see in the image)
The problem is that we can't reproduce this behavior with apex code.
we tried this with the code below : The Email sent but Task not created
[ Please be advised - we do not want to use message.setTargetObjectId() becuase we don't want to send an email to the contact ]
List<string> toList = new List<string>();
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
toList.add('any@external.email');
message.setUseSignature(false);
message.setSaveAsActivity(true);
message.setSubject('Any subject);
message.setHtmlBody('any Body');
message.setWhatId('00111000007dqTk'); // THIS IS AN ID OF THE SIMPSON'S FAMILY
message.setToAddresses(toList);
Messaging.sendEmail(message);
You would do the insert/delete on the fly yes - so it would be saved in Salesforce only for the duration of your APEX execution.
All Answers
1. Insert a "dummy" contact with a fake email address
2. Set that contact's ID as your setTargetObjectId
3. Send the email
4. Delete the "dummy" contact
It's kind of a hack, but should work for you.
TIA
You would do the insert/delete on the fly yes - so it would be saved in Salesforce only for the duration of your APEX execution.
will notify when test the code
again thank you very much