Qntrl | Online Help | Business case 4: Set conditions for users to delete cards using Server Scripts (Before Delete)

Business case 4: Set conditions for users to delete cards using Server Scripts (Before Delete)

Organizations provide permissions and privileges to various sections of staff according to their roles and profiles. In order to make sure that the data within Qntrl can only be deleted by the right personnel, organizations can implement complex criteria and conditions using server scripts.

Business Scenario

To delete cards of the Appraisal orchestration of Zylker organization, the user in Qntrl must have a Product Admin role. Employees with the User profile must not be allowed to delete a card, and cards with due dates in the future must not be deleted.
Note : Even if a user's profile or role does not have permissions in Access Control to delete a card, this server script overwrites the existing permissions and allows the user to delete the card.


When a user tries to delete a card in Appraisal orchestration, you can trigger server scripts to check user permissions and restrict the deletion operation.
  1. Create a server script and code the logic to validate the users who perform delete operation.
  2. Delete a card in Appraisal orchestration and test the server script.

Sample Configuration

Step 1: Create an orchestration

Create a new orchestration titled 'Appraisal' and add relevant custom fields to it.

Once the form is saved, proceed to design the blueprint, set permissions, and publish the orchestration.

Step 2: Code server scripts

Create a new server script for the Appraisal  orchestration to validate users.
  1. Click New Script and enter the name as 'Validate card deletion'.
  2. Choose Appraisal in the Form dropdown.
  3. Choose Job as the Execution Location to execute the script.
  4. Choose Before Delete  as the Execution Trigger to execute this script whenever the card page loads.
  5. Copy and paste the below script into your script editor and replace the parameter names.
    1. You can use the ? icon at the top-right corner of the script editor to refer to parameter names.
  6. Once the script is ready, Publish it. 

  1. function beforeDelete(){
  2.     var userProfileId = "<enter profile ID here>";
  3. //Get profile ID from our API doc: Get profile details
  4.     var productAdminRoleId = "<enter role ID here>";
  5. //Get role ID from our API doc: Get role details
  7.     // This script allows users with Product Admin role alone to delete the cards. Employees with  User profiles cannot delete the card. 
  8.     // Also due date must exceed the current date. Otherwise reject the card deletion.
  10.     var user = current.User;
  11.     var profile = user.getProfile();
  12.     var roles = user.getRoles();
  13.     if (profile.getId() == userProfileId) {
  14.         throwError("You do not have permission to delete the card");
  15.     }
  17.     var hasRole = false;
  18.     for (var i = 0; i < roles.length; i++) {
  19.         var role = roles[i];
  20.         if (role.getId() == productAdminRoleId) {
  21.             hasRole = true;
  22.             break;
  23.         }
  24.     }
  26.     if (!hasRole) {
  27.         throwError("You don't have Product Admin role to delete this card");
  28.     }
  30.     var dueDate = current.Job.getValue(current.Layout.Fields.duedate.id);
  31.     if (dueDate) {
  32.         var currentMillis = Date.now();
  33.         var dueDateMillis = dateStringToTimestamp(dueDate);
  34.         console.log("currentMillis < dueDateMillis = " + currentMillis < dueDateMillis);
  35.         if (currentMillis < dueDateMillis) {
  36.             throwError("Card's due date is in the future. Cannot delete it.");
  37.         }
  38.     }
  40.     return true;
  42. }
  45. function dateStringToTimestamp(dateString) {
  46.     // logic based on exact dateformat: mm-dd-yyyy h:mm a
  47.     if (!dateString || !(typeof dateString === "string")) {
  48.         return null;
  49.     }
  51.     var target = new Date();
  53.     dateString = dateString.trim();
  54.     var spaceSplitted = dateString.split(" ");
  56.     var datePart = spaceSplitted[0];
  57.     var dateSplitted = datePart.split("-");
  58.     target.setMonth(+dateSplitted[0] - 1);
  59.     target.setDate(dateSplitted[1]);
  60.     target.setFullYear(dateSplitted[2]);
  62.     if (!spaceSplitted[1]) {
  63.         // there is no time detail in given dateString
  64.         target.setHours(0, 0, 0, 0);
  65.     } else {
  66.         var timeSplitted = spaceSplitted[1].split(":");
  67.         var isAmPm = spaceSplitted[2];
  68.         if (!isAmPm) {
  69.             target.setHours(timeSplitted[0], timeSplitted[1], 0, 0);
  70.         } else {
  71.             var ampm = isAmPm.toLowerCase();
  72.             var hour = +timeSplitted[0];
  73.             var mins = +timeSplitted[1];
  74.             if (hour == 12 && ampm == "am") {
  75.                 hour = 0;
  76.             } else if (hour == 12 && ampm == "pm") {
  77.                 hour = 12;
  78.             } else if (ampm == "pm") {
  79.                 hour += 12;
  80.             }
  81.             target.setHours(hour, mins, 0, 0);
  82.         }
  83.     }
  84.     return target.getTime();
  85. }

Step 2: Delete card

To test the script, delete a card from the  Appraisal orchestration using different user profiles and roles.
  1. When someone other than users in Product Admin role deletes the card, an error message is displayed as follows: ‘You don't have Product Admin role to delete this card'.
  2. When someone with User profile deletes the card, an error message is displayed as follows: ‘You do not have permission to delete the card’.
  3. When a card with due date in the future is deleted, an error message is thrown as follows: ‘Card's due date is in the future. Cannot delete it.’


    Zoho DataPrep Personalized Demo

    If you'd like a personalized walk-through of our data preparation tool, please request a demo and we'll be happy to show you how to get the best out of Zoho DataPrep.

    Zoho CRM Training

      Create, share, and deliver

      beautiful slides from anywhere.

      Get Started Now

              Zoho CRM Training Programs

              Learn how to use the best tools for sales force automation and better customer engagement from Zoho's implementation specialists.

              Zoho CRM Training

                  Zoho SalesIQ Resources

                      Zoho TeamInbox Resources

                                Zoho DataPrep Resources

                                  Zoho DataPrep Demo

                                  Get a personalized demo or POC

                                  REGISTER NOW

                                    Design. Discuss. Deliver.

                                    Create visually engaging stories with Zoho Show.

                                    Get Started Now



                                                          Watch comprehensive videos on features and other important topics that will help you master Zoho CRM.


                                                          Download free eBooks and access a range of topics to get deeper insight on successfully using Zoho CRM.


                                                          Sign up for our webinars and learn the Zoho CRM basics, from customization to sales force automation and more.

                                                          CRM Tips

                                                          Make the most of Zoho CRM with these useful tips.

                                                            Zoho Show Resources