​​​How to notify users by email?

Last Update: January 08, 2018

In this use case, you will learn how to send an email notification to a particular user(s) using Email Templates feature and sendMail() function. In this case, we will design a form to send two email notifications: the first one will be sent when you add a new item to the list, the second notification will be sent when you edit an item in a list. In the first notification, we will use a function named saveAndCallFunction() to make you able to use the Item ID of the added item to generate the Form URL.

Here are the steps on how to do that.

  1. Open the form in design mode.
  2. Add the following to the form design workspace:
    How to notify users by email1.jpg
  3. Add new form variable and name it "varFormURL". We will use this variable to store the Form URL in and use it as a hyperlink in the email template. For more information on how to add form variable, please refer to use case Using Form Variables.
  4. Add a new email template as follows:
    • Click on Email Template in the Settings group ribbon.
      How to notify users by email2.jpg
    • In the Email Templates Ribbon, Click on New.
      How to notify users by email3.jpg
    • Name the email template as "NewTaskNotification"
    • In the Body section, type the following text:
      Hi,
      A new task named #Control(txtTaskName) has been created.
      You may view and respond to the task by clicking on this link Form URL.

      Regards,
      System Administrator

      Note: you can add form controls and form variables by clicking on fx button.

    • To create a link of the form url, follow these steps:
      • In the email body section, point to the position that you want to add the hyperlink in.
      • Select the Insert Ribbon.
        How to notify users by email4.jpg
      • Click on the link.
      • In the Text to display, type the name of the URL (i.e. Form URL).
      • In the Address, type the following: #FormVariable(varFormURL)
    • Under Email Templates Group, click on the Save button to save the email template that has been created.
    For more details about Email Templates, please refer to the SAPARK Forms User Guide.
     
  5. Select btnSave
  6. In the Rules pane, click on the Add icon to add a new rule, the Rule Manager dialog will open.
    How to auto-shifting Panel control in a form2.jpg
  7. Add a new rule as follows:
    1. Change the Rule Name as desired.
    2. Change the Rule Type to Action.
    3. Change the Event Type to onClick.
    4. In the Condition section, type the following script:
      getFormQueryString('type') == 'New'
    5. In the Action section, type the following script:
      function NotifyAssignees_NewItem(){
      varFormURL = '[Form URL Portion1]' + getFormID() + '[Form URL Portion2]';
      /*
      Open the form "item" in Edit mode, copy the URL of the form, [Form URL Portion1] starts from the beginning of URL until you reach "ID=", [Form URL Portion2] starts from "&Source" until the end of URL.

      Example: if the Form URL is '/_layouts/15/SPARK/SparkForm.aspx?type=Edit&List=57a6e9a0-f0a0-4a50-9221-fd8e26e931db&ID= 10 &Source= Lists%2FItems_Test%2FAllItems%2Easpx&Web=713c2cc4-ebee-4b19-b6be-83bc59ebe860', then the [Form URL Portion1] = '/_layouts/15/SPARK/SparkForm.aspx?type=Edit&List=57a6e9a0-f0a0-4a50-9221-fd8e26e931db&ID=', and the [Form URL Portion2] = '&Source= Lists%2FItems_Test%2FAllItems%2Easpx&Web=713c2cc4-ebee-4b19-b6be-83bc59ebe860'
      */
      var Assignees = replace(getPeoplePickerValue(ppAssignee,3).toString(),',',';'); //convert array to string and replace , with ;
      sendMail(Assignees,'','','Assignee to take action','NewTaskNotification');
      }
      saveAndCallFunction(NotifyAssignees_NewItem);

    6. Click on the Save button to save the rule.
      Note: You can use Assistance Panel to help you adding functions and related parameters, form variables and form controls to Conditions and/or Actions sections.
  8. Add the second rule as follows:
    1. Change the Rule Name if desired.
    2. Change the Rule Type to Action.
    3. Change the Event Type to onClick.
    4. In the Condition section, type the following script:
      getFormQueryString('type') != 'New'
    5. In the Action section, type the following script:
      varFormURL = '[Form URL Portion1]' + getFormID() + '[Form URL Portion2]';
      /*
      Open a new form "item", copy the URL of the form, [Form URL Portion1] starts from the beginning of URL until you reach "ID=", [Form URL Portion2] starts from "&Source" until the end of URL.

      Example: if the Form URL is '/_layouts/15/SPARK/SparkForm.aspx?type=New&List=57a6e9a0-f0a0-4a50-9221-fd8e26e931db&ID= 10 &Source= Lists%2FItems_Test%2FAllItems%2Easpx&Web=713c2cc4-ebee-4b19-b6be-83bc59ebe860', then the [Form URL Portion1] = '/_layouts/15/SPARK/SparkForm.aspx?type=New&List=57a6e9a0-f0a0-4a50-9221-fd8e26e931db&ID=', and the [Form URL Portion2] = '&Source= Lists%2FItems_Test%2FAllItems%2Easpx&Web=713c2cc4-ebee-4b19-b6be-83bc59ebe860'
      */
      var Assignees = replace(getPeoplePickerValue(ppAssignee,3).toString(),',',';'); //convert array to string and replace , with ;
      saveAndDoNothing();
      sendMail(Assignees,'','','Assignee to take action','NewTaskNotification');
      }
    6. Click on the Save button to save the rule.
  9. Publish the form by clicking on the Publish button in Publishing Group in the Form Ribbon.
  10. Test the form by clicking on an existing or new item on the correspondences list.

Related Resources:
These resources may not reflect the same exact case steps.
https://www.youtube.com/embed/91gvVO4JYPE