Zoomed Image

How to Add a Button with a Single-Use Link to an Email

Configuration Guide

How To

How to Add a Button with a Single-Use Link to an Email

This page shows how to send an email with a button that opens one xAssets form for one record. The person who clicks it can open the form without an xAssets login. The asset user confirmation emails work this way: the custodian clicks Acknowledge Receipt and the assets on the task are confirmed.

The link carries an API token. The token names the form, the record and an expiry date. See API Tokens for how tokens work.

Prerequisites

  • Configuration access to edit transformations and email templates.
  • SMTP settings that send email. See How to Configure Email Notifications.
  • A form for the link to open, with a script that reads the record from the link. See How to Create a Custom Form.
  • An email address on the Custodian record of each person who will receive the email.

Step 1: Add the Button to the Email Template

Email templates are kept in the database. Open the Email Templates editor from the gear icon > Email Notifications > Settings > Email Templates. Copy an existing template, such as Asset User Confirmation Email Template, and give it a new name. Add the button below to its HTML.

Put the button in the template twice. Outlook draws the first part. Other mail clients draw the second part.

<!--[if mso]>
<v:roundrect xmlns:v="urn:schemas-microsoft-com:vml"
  xmlns:w="urn:schemas-microsoft-com:office:word"
  href="%URL%" style="height:40px;v-text-anchor:middle;width:220px;"
  arcsize="10%" stroke="f" fillcolor="#2167c0">
  <w:anchorlock/>
  <center style="color:#ffffff;font-family:Arial,sans-serif;font-size:10pt;font-weight:bold;">
    Approve
  </center>
</v:roundrect>
<![endif]-->
<!--[if !mso]><!-- -->
<table border="0" cellspacing="0" cellpadding="0" role="presentation">
  <tr>
    <td align="center" bgcolor="#2167c0" style="border-radius:5px;">
      <a href="%URL%" target="_blank"
         style="display:inline-block;padding:10px 20px;font-family:Arial,sans-serif;
                font-size:10pt;font-weight:bold;color:#ffffff;text-decoration:none;">
        Approve
      </a>
    </td>
  </tr>
</table>
<!--<![endif]-->

Each %name% in the template is replaced when the email is sent. %URL% becomes the link. Add others for the text of the email. %Name% is the recipient's name in the examples below.

In the transformation that sends the email, create a token with GetAPITokenForRole:

Set APIToken = CommandProcessor "GetAPITokenForRole", _
    "Purchase Approval", _
    "PurchaseApprovalAccountWrite", _
    "10000", "7", _
    "currentkey", "%RequestID%"

Set DBName = Sql select db_name()
Set URL = "%weburl%/xpanel.aspx?xtoken=%APIToken%&database=%DBName%"

The arguments are:

Argument Example Meaning
Form Purchase Approval The form the link opens
Role PurchaseApprovalAccountWrite The account the visitor works as
Key expiry days 10000 Used once, when the role's account is first created
Token expiry days 7 The link stops working after this many days
Name and value pairs currentkey, %RequestID% Values passed to the form. currentkey is the record to open

A role name has 12 to 40 letters and digits and ends in Read or Write. The first time a role is used, xAssets creates a user with that name. The user joins the APIKEYWRITE user group for a Write role, or APIKEYREAD for a Read role. The key is kept in a setting with the same name as the role.

The values in the token cannot be changed by the person who clicks the link. Only the token's identifier is in the link.

Step 3: Merge the Template and Send the Email

Set Email = SQL "select Email from Custodian where CustodianID = %CustodianID%"
Set Name = SQL "select CustodianName from Custodian where CustodianID = %CustodianID%"

MergeTemplateFile "PurchaseApprovalTemplate.htm", _
    "Name", "%Name%", _
    "URL", "%URL%"

SendEmail "%Email%", "", "", "Purchase request for approval", "", "", "%spo:FromMailAddress%"

MergeTemplateFile reads the template of that name from the database, or else from the templates folder of the web server. It replaces each name with its value. SendEmail sends the merged text when its body argument is blank.

A token works until it expires. Each click logs on again. To make a link work once:

  1. Check the record before the action. The form's script reads the record with QueryString "currentkey". If the record is already approved, show a message and stop.
  2. Delete the token when the action is done. Keep the token with the record when you create it. Then call DeleteAPIToken with that value at the end of the action:
DeleteAPIToken "%StoredToken%"

Pass the token's value. With a blank value, DeleteAPIToken deletes the token of the logon request only, and the form's script runs after that request. How to Create Single-Use and Multiple-Use Links has the full steps.

Put the action on a button in the form. Do not run it when the form opens. Some mail systems open every link in an email to check it for threats, and an action that runs on opening would run then.

Step 5: Set the Rights of the Role's User Group

The visitor works as the role's user, with the rights of its user group. A person who has the link can use any part of xAssets those rights allow. Give the group only what the form and its transformations need:

  • Use a Read role when the form only displays data.
  • A form that runs a transformation needs the Import right on Asset. A transformation with its own Permissions needs those permissions.
  • Use a different role for each kind of email. Each role's user group then has the rights of one workflow.

Each click is an API logon from the visitor's own network. The AllowedAPIIPs setting lists the addresses allowed to make API logons. When the list has entries, a click from an address outside it fails. The visitor sees "Your IP address is not allowed to log on to this system."

Step 6: Test the Email

  1. Run the transformation for a test record with your own email address on the Custodian record.
  2. Open the email and click the button. The form opens for the test record.
  3. Complete the action. Click the button again. The second click shows a logon error once the token is deleted.

Security Considerations

Item Detail
Forwarded emails Anyone who has the link can use it until it expires or is deleted. Set the expiry to the days the workflow needs.
Multiple emails A reminder that creates a new token leaves the earlier links working. Delete the earlier token before you send a new one.
Multi-factor authentication Token logons do not ask for a second factor.
Expired links An expired link shows a logon error. The batch service deletes expired tokens.