Tip #1198: Add email address to recipients in javascript

The advantage of having access to friendly brainpower is that the problems are getting resolved much quicker. The disadvantage is that it breeds inattentive coding. Daryl “Always Raising” LaBar almost threw in a towel but recovered and redeemed himself with the snippet how to add an arbitrary email address to the list of the email recipients on the email form.

function appendToEmail(executionContext)
{
var formContext = executionContext.getFormContext();
// prior to v9: Xrm.Page

var att = formContext.getAttribute('to'); // or 'cc'
var emails = att.getValue() || [];
emails.push({name: 'george@foobar.com', type: '9206'});
att.setValue(emails);
}

Potential gotchas:

  • the property is type not entityType as one would expect
  • sending emails to arbitrary recipients must be enabled in System settings

image

(Facebook and Twitter Cover photo by Mathyas Kurmann on Unsplash)

6 thoughts on “Tip #1198: Add email address to recipients in javascript

  1. Denis D. says:

    Each time I save the record with plain email address set like this, I get unsaved changes after. And there is no way to get rid of the “Unsaved changes” notification (“To” field is dirty, but it is set only once before the save).

  2. Rashi says:

    This code is not working on UCI. Any idea?

  3. chintan says:

    Any one figured out how this functionality works in UCI?

Leave a Reply to Rashi Cancel reply

Your email address will not be published. Required fields are marked *