Sending an Email from a Table Level Script

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

Sending an Email from a Table Level Script

  • Comments 5
  • Likes

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.

 

Comments
  • Hi Jeff,

    have you also an example with using email template?

    regards

    Christian

  • Hi Jeff

    What is the method used to add an attachment?  I have tried AddAttachment and AddFile, with no luck

    Thanks

    Brett

  • The in built message block can't use attachments.  Please see the artilce:  community.sagecrm.com/.../sending-emails-in-asp-com-api.aspx

    If you need to send attachments then you can use the cdosys mail object.

  • Great thanks Jeff, I got it right with cdosys

  • For anyone looking to include Attachments, and using SQL Server, I suggest looking into using DBMail which allows attachments, and is a standard built in feature for SQL Server.