Opt in/out of email subscription | Zoho Creator Academy

Opt in/out of email subscription

Requirement

Users should be able to choose if they want to subscribe to or unsubscribe from a periodic newsletter email.

Use Case

A fintech company sends monthly newsletters to its customers. Customers should have the option to subscribe or unsubscribe to the newsletter at any point of time.

Steps to follow

1. Create two forms with the following details.

Form

Form Link Name

Field Type

Field Name

Field Link Name

Opt In

Opt_In

Add Notes

I want to receive updates

plain

Name

Name

Name

Email

Email

Email

Opt Out

Opt_Out

Add Notes

I do not want any further updates

plain

Email

Email

Email

Name

Name

Name


The Opt In form is used by customers to sign up for the monthly newsletter. The Opt Out form is used by customers to unsubscribe from the monthly newsletter.
2. Create workflow with the following details to restrict duplicate email IDs.



We are using the Form Event as Validations on form submission so that the entered email address can be checked before the data is added to the database.

3. Click Add New Action and add the following snippet in the Deluge editor.
  1. // check if any duplicate records exist and alert the user if yes
  2. duplicate_records_count = Opt_In[Email == input.Email].count();
  3. if(duplicate_records_count != 0)
  4. {
  5. alert "This email is already registered for updates";
  6. cancel submit;
  7. }
Note: The No duplicate values field property can also be used to prevent duplicate entries. However, we have used the script to display a customized alert message.

Now let's add workflows to the Opt Out form to achieve the following:
  1. Disable Name field 
  2. Fetch data and populate Name field based on the entered email address
  3. Display an alert when then entered email address does not exist in database
  4. Remove the email address from Opt In form after form submission
4. Create workflow with the following details to disable the name field.



5. Click Add New Action and save the following snippet in the Deluge editor.
  1. disable Name;
6. Create a new workflow with the following details.




The Form Event is selected as User input of a field so that the corresponding Name value can be fetched when an email address is entered.

7. Click Add New Action and save the following snippet in the Deluge editor.
  1. // fetch the record containing the email address
  2. fet = Opt_In[Email == input.Email];
  3. // if the record exists, assign the corresponding name to the Name field, and display an alert to the user that the email will be removed from the list
  4. if (fet.Email != null)
  5. {
  6. input.Name = fet.Name;
  7. alert ("This email is registered with the name " + input.Name) + " and will be removed from the updates list";
  8. }
8. Create another workflow as below to display an error if the email is not found/subscribed



We are using the Form Event as Validations on form submission so that the entered email address can be checked before the data is added to the database.

9. Click Add New Action and save the following snippet in the Deluge editor.
  1. // fetch the record containing the email address
  2. fet = Opt_In[Email == input.Email];
  3. // if the email address is not found, display an alert and restrict the form submission
  4. if(fet.Email == null)
  5. {
  6. alert "This email is not registered for updates";
  7. cancel submit;
  8. }
10. Lastly, create a workflow as displayed, to remove the email address from the subscription list on successful form submission.


11. Click Add New Action > Deluge Script, and save the following script in the Deluge editor.
  1. // delete the record in Opt In form which contains the specfied email address
  2. delete from Opt_In[Email == input.Email];

See how it works


Points to note

  1. Permissions can be used to hide the reports from users. 
  2. The email list in the Opt In form can be used to send periodic emails or newsletters using schedules.
  1. Workflows
  2. Form events in workflows