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)

10 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).

    • Leigh Hogan says:

      I managed to get rid of the “Unsaved Changes” by adding an ID to the array of just all 0’s.

      emails.push({id: “00000000-0000-0000-0000-000000000000”, name: ‘george@foobar.com’, type: ‘9206’});

  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?

  4. MolyOm says:

    It works for UCI:
    var UserId = formContext.context.getUserId();
    var UserName = formContext.context.getUserName();

    // bcc has a simple text email I I add user to partylist
    var bccField = formContext.getAttribute(“bcc”).getValue();

    var partlistData = new Array();
    //first Party
    partlistData[0] = new Object();
    partlistData[0].id = UserId;
    partlistData[0].name = UserName;
    partlistData[0].entityType = “systemuser”;
    //second Party
    partlistData[1] = new Object();
    partlistData[1].name = bccField[0].name; // “antraconflictreciever@alantra.com”;
    partlistData[1].entityType = bccField[0].entityType;
    partlistData[1].id = bccField[0].id;

    formContext.getAttribute(“bcc”).setValue(partlistData);

  5. Michael Großhauser says:

    For UCI

    I think I got it with this:

    formContext.getAttribute(‘to’).setValue(
    [
    {
    name: ‘abc@123.de’,
    entityType: ‘unresolvedaddress’,
    id: ‘{00000000-0000-0000-0000-000000000000}’
    }
    ]);

    I just put the email address in the form and used a debugger to see what the standard does with it. This seems to be the solution to this problem.

  6. That is awesome, Michael! I would have never guessed unresolvedaddress as an entity.

Leave a Reply

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