Rule validation along with javascript validaton

Forums Classic DFFS Rule validation along with javascript validaton

Viewing 3 reply threads
  • Author
    Posts
    • #29632
      Amal Vellappillil
      Participant

      Hi,

      Here is a scenario I am working with. I need to send an email when saving a record if a check box is checked. So I made an email template and then associated it with a rule which checks if a field (yes/no field) is ‘Yes’. The validation is on ‘Fiend Change’. With this setup, the email will get sent every time a user just unchecks and checks that field. So I was thinking to have some JS code to check if the beforeproperty for this field is no. For that I tried the following.

      1. inline js
      ————-
      I tried entering the following in the ‘Run these functions’ textbox.

      
      
      {inline}
      spjs.dffs.beforeProperties["Received"] === false;

      When i debugged this rule, I see that the rule is matched. For testing purposes, I change the code to this

      
      
      {inline}
      spjs.dffs.beforeProperties["Received"] === true;

      To my surprise, the rule is matched.

      2. Call a customjs function (hard coded it to return false)
      —————————–
      I tried all of these but all either error or Match
      – Created a function called wasFieldUncheckedBefore(fin). Tried calling this from the rule like this

      wasFieldUncheckedBefore("Received")

      This gave me an error “DFFS: Configuration error in Run these functions/evaluate these rules”

      – Tried this

      
      
      {inline}
      wasFieldUncheckedBefore("Received");

      I didnt get any errors but the rule matched.

      – Removed parameter from function and called the function without parameter like this

      wasFieldUncheckedBefore

      I didnt get any errors but the rule matched.

      So I have 3 questions
      1. What is the best way to achieve my goal?
      2. Is it possible to use inline JS in JS textbox to evaluate some value and validate/invalidate the rule? (like I did in #1 method)
      3. Is it possible to use JS functions in customJS section to return true/false and use that to validate/invalidate the rule?
      4. Is it possible to pass parameter from rule to customJS function? (like I did in #2 method)

      I know this has been a long post for possibly a simple answer. Thank you for all your continued help.

      – Amal Vellappillil

    • #29643
      Alexander Bautz
      Keymaster

      Hi,
      The {inline} functionality in the Run these functions / evaluate these rules textarea cannot be used to control how the rule is evaluated – it will run the snippet you have provided when the rule is evaluated to true.

      You could do this by using two rules and some Custom JS, but it’s easier to do it with only Custom JS. Add this snippet to your Custom JSs textarea:

      jQuery("#dffs_Received input").on("click", function () {
          // Run this only if the Received checkbox was NOT already checked when the form loaded
          if (!spjs.dffs.beforeProperties.Received) {
              if (getFieldValue("Received")) {
                  // If the checkbox is checked > prepare email for sending when the form is saved
                  spjs.dffs.handleEmailTemplate("TestEmailTemplate", true);
              } else {
                  // If the checkbox is unchecked > ensure the email is not send when the form is saved
                  spjs.dffs.handleEmailTemplate("TestEmailTemplate", false);
              }
          }
      });

      Change Received to match your Boolean (Yes/No) field internal name and TestEmailTemplate to match your email template name.

      Let me know how this works out.

      Answer to Q4: When not using {inline} in Run these functions / evaluate these rules you cannot pass any arguments – but the function you add to Custom JS will take one argument automatically and this is the name of the rule triggering it.

      If you use {inline} you can pass arguments, but as mentioned above this cannot be used to control how the rule is evaluate – it is always run when the rule is evaluated to true.

      Alexander

    • #29655
      Amal Vellappillil
      Participant

      Hi Alex,

      Thank you very much for clarifying the fact about JS in “Run these functions/evaluate these rules” does not have an impact on rule validation.

      I have been using a generic version of what you had mentioned in your post with another list. I just thought about asking this question and see if there was a better way of getting this done. Here is the sample code. Just putting it out there just in case if it is helpful for others.

      
      
      function queueEmailToBeSent(ruleID) {
          var initval, val, rule;
          
          jQspjs.each(spjs.dffs.ruleObj.ruleArr, function (k, v) {
              if (v.friendlyName === ruleID) {
                  rule = v;
              }
          });
      
          if (!jQspjs.isEmptyObject(rule)) {
              var field = rule.triggerField;
              var type = spjs.dffs.fieldtype[field];
      
              initVal = spjs.dffs.beforeProperties[field];
              val = getFieldValue(field);
      
              if (type === "SPFieldBoolean") {
                  if (val !== initVal && val) {
                      spjs.dffs.handleEmailTemplate(ruleID, true);
                  } else {
                      spjs.dffs.handleEmailTemplate(ruleID, false);
                  }
              }
              else
              {
                  if (val !== initVal && !jQspjs.isEmptyObject(val)) {
                      spjs.dffs.handleEmailTemplate(ruleID, true);
                  } else {
                      spjs.dffs.handleEmailTemplate(ruleID, false);
                  }
              }
          }
      }

      As a follow up can you answer these as well?
      1. Is it possible to find out if a rule is validated or not using JS?
      2. Is it possible to manually evaluate a rule from within JS?

      Thank you for all your help.

      • #29665
        Alexander Bautz
        Keymaster

        I see, her is the answers to your questions.

        #1: Use this in Custom JS:

        var ruleStatus = spjs.dffs.data.ruleIndexObj.NAME_OF_RULE

        #2: Yes, from v4.4.4.0 you can do it like this:

        var ruleStatus = spjs.dffs.evaluateRule("NAME_OF_RULE")

        Alexander

    • #29692
      Amal Vellappillil
      Participant

      Awesome. Thank you so much for all the clarifications and directions.

      Stay safe.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.