Expression tag examples

These code examples show you how you can customize email output.

This example shows how to add HTML code to change the color of the text, depending on the value of the priority field. If the priority has a value of 1, the text is displayed in red.

#@EXPRESSION::if ( #?Priority?# eq "1 - High" ) {"<FONT COLOR=\"red\">";} elsif ( #?Priority?# eq "2 - Medium" ) { "<FONT COLOR=\"orange\">";} elsif ( #?Priority?# eq "3 - Low" ) { "<FONT COLOR=\"yellow\">";}@#Priority: #?Priority?# </FONT>

This example code prints all record IDs for the related records on the same line, separated by spaces. If no related records are associated with the record, nothing is printed.

#@EXPRESSION::if ( scalar( @{#?RelatedRecords?#} ) ) { "Related Records: ".join(" ",@{#?RelatedRecords?#}); } else { ""; }@#

This example code prints the current value of the State field and if it has changed, also the previous value of the State field.

State: #?State?# #@EXPRESSION::if (#?State?# ne #%State%#) { "(Old Value: ". #%State%# .")"; }@#

Although field meta tags can be used in EmailPlus expressions, occasionally, expressions might not be evaluated correctly. Specifically, if a field value contains an odd number of double or single quotation marks, the expression is not evaluated correctly. In this case, you can use functions in the EmailPlus global scripts to resolve field values instead of using the field meta tags. In addition, use the quotemeta Perl function to escape any nonalphanumeric characters in a field value. With this function, you can effectively compare field values in EmailPlus expressions. This example compares the field value of the Description field with the original field value of the Description field. If they differ, then the EmailPlus notification includes a message that states that the Description field changed.

#@EXPRESSION::if ( quotemeta(Gfv("Description")) ne quotemeta(Gfov("Description") ) { "The Description field has changed: “.Gfv(“Description") ; }@#

The following example adds details about record attachments to the notification. See the comments in the code for explanations about the added details:

#@EXPRESSION::
  # Get a list of the attachment fields in this record type...
  my($AttachmentFields) = $entity->GetAttachmentFields();
  # Tell how many attachment fields there are and show their
  # names...
  $M = $M . "This entity contains " . $AttachmentFields->Count() .
      " attachment field(s)\n";
  for ($A = 0; $A < $AttachmentFields->Count(); $A++) 
  {
    $M = $M . "    " . ($AttachmentFields->Item($A) )->GetFieldName() . "\n";
  }
  $M .= "\n";

  # Iterate over the attachment fields; for each one, list the 
  # attachments it contains in the current record...
  for (my($AF) = 0; $AF <$AttachmentFields->Count(); $AF++) {
    my ($AttachmentField) = $AttachmentFields->Item($AF);

    $M = $M ."Attachment field '"
          . $AttachmentField->GetFieldName().
           "' contains:\n";
       # Iterate over the attachments in this field...
       my($Attachments) = $AttachmentField->GetAttachments();

    for (my($A) = 0; $A <$Attachments->Count(); $A++) {
      my($Attachment) = $Attachments->Item($A);

      # Report info about this attachment...
      $M = $M .
      "    Filename='" . $Attachment->GetFileName() . "'" .
      " FileSize=" . $Attachment->GetFileSize() . 
      " Description='" . $Attachment->GetDescription() . "'" .
      "\n";
    }
    $M = $M ."Total attachments: ". $Attachments->Count() ."\n\n";
  }
  # Display the results...
  $M;
@#