Sending an Email from a Table Level Script

Less than one minute read time.

Consider this scenario.

When an Opportunity is reassigned then an email needs to be sent to the newly assigned user.

The Table Level Script needs to fire correctly whether a single Opportunity is reassigned through the user interface or whether a batch of Opportunities are reassigned by another table level script. E.g. "A Table Level Script to update Opportunities when a Company is Reassigned".

The code below for an UpdateRecord event function will send an email that will aggregate the changes made and will work when either a single opportunity is changed or when a set of records have been updated.


function UpdateRecord()
{
var oppoRecord = CRM.FindRecord("opportunity",WhereClause);
var userRecord = CRM.FindRecord("users","user_userid ="+ Values("oppo_assigneduserid"));
var messagetext = "Please check the following opportunities\r\n";
while (!oppoRecord.eof)
{
  messagetext += oppoRecord.oppo_description +"\r\n";
  oppoRecord.NextRecord();
}
var myMailObject = CRM.GetBlock("messageblock");
with (myMailObject)
{
  DisplayForm = false;
  mSubject = "New Oppos";
  mBody = messagetext;
  mShowCC = true;
  mShowBCC = true;
  //TO|CC|BCC are valid recipients
  AddRecipient(userRecord.user_emailaddress,userRecord.user_firstname+ " "+userRecord.user_lastname,"TO");
  Mode=2;
  Execute();
  if (mSentOK)
  {
    //Valid = false;
    //ErrorStr = "Email Sent";
  }
  else
  {
    //Valid = false;
    //ErrorStr = "There has been a problem:  "+mErrorMessage;
  }
}
}

Note. This script does not create an communication record.