Conditionally send e-mail on entity creation

1 minute read time.

It is common to have CRM send out an e-mail on the creation of a new entity. For example you may have an e-mail sent to the assigned user on a new Opportunity notifying them that a the Opportunity has been created and that it is assigned to them. This is achieved by putting a Send E-mail action on the Primary Rule.

But what if you want to conditionally send that e-mail? For example say you don't want to send the e-mail to the assigned user if it was them that created the opportunity in the first place. This would be a nice feature as everyone gets too many e-mails as it is!

This article shows you a technique for doing this.

You can actually use the messageblock object in the COM API within a table level script. Jeff has previously blogged about using this object here.

function InsertRecord()
{
// Check is the current user not the assigned user?
if (CurrentUser.user_userid!=Values('oppo_assigneduserid'))
{
var myMailObject = eWare.GetBlock('messageblock');
with (myMailObject)
{
// Build the message subject and body
mSubject = 'A new Opportunity has been assigned to you!';
mBody = 'Description: ' + Values('oppo_description') + '
';
mBody += 'Details:
' + Values('oppo_note');

// Add the assigned user as a recipient
var assignedUser = eWare.FindRecord('user', 'user_userid=' + Values('oppo_assigneduserid'));
AddRecipient(assignedUser.user_emailaddress,assignedUser.user_firstname + ' ' + assignedUser.user_lastname, 'TO');

// Send the mail
DisplayForm = false;
Mode = 2;
Execute();

// Did it work?
if (mSentOK)
{
// Email Sent
}
else
{
// There has been a problem
Valid = false;
ErrorStr += mErrorMessage;
}
}
}
}