Send E-Mail from custom entities in CRM 4

A load of customers are still running CRM 4 and still manage to come up with problems I can’t just find solutions to with Google!

This time it was automatically populating the To: field of a new email when initiated from a custom entity. In this case, the custom entity has a related Contact but would work just as well with an associated Account or User. Unfortunately you can’t just have it send directly to a email address without some fancy plugin footwork – it has to be a CRM record.

I have a plugin which attaches any files from notes to the email when it gets sent so I suppose this could be extended to support plain-text email addresses. Maybe one day I’ll get around to documenting it here – trying to keep the blog focussed on CRM 2011 as much as possible.

Yet again, this requires Daniel Cai’s excellent CRM Web Service Toolkit.

var regarding = crmForm.all.regardingobjectid.DataValue;
if (regarding) {
   if (regarding[0].type == 10038) {

    var venueId = regarding[0].id;

    var fetchXml = [
    "<fetch mapping='logical' count='1'>",
      "<entity name='msa_eventvenue'>",
        "<attribute name='new_contactid' />",
        "<filter>",
          "<condition attribute='msa_eventvenueid' operator='eq' value='" + venueId + "' />",
        "</filter>",
      "</entity>",
    "</fetch>"
    ].join("");

    var fetchData = CrmServiceToolkit.Fetch(fetchXml);
    if (fetchData.length > 0) {

      var lookupObject = new Object;
      lookupObject.id = fetchData[0].getValue("new_contactid");
      lookupObject.name = fetchData[0].getValue("new_contactid","name");
      lookupObject.typename = "contact";

      var lookupObjectArray = new Array();
      lookupObjectArray[0] = lookupObject;

      crmForm.all.to.DataValue = lookupObjectArray;

    }

  }
}
Advertisements
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: