Home › Forums › Classic DFFS › Rule validation along with javascript validaton
- This topic has 4 replies, 2 voices, and was last updated 4 years, 6 months ago by Amal Vellappillil.
-
AuthorPosts
-
-
April 24, 2020 at 17:25 #29632
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 thiswasFieldUncheckedBefore("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
-
April 26, 2020 at 07:57 #29643
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
-
April 28, 2020 at 00:29 #29655
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.
-
April 28, 2020 at 13:55 #29665
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
-
-
April 28, 2020 at 23:37 #29692
Awesome. Thank you so much for all the clarifications and directions.
Stay safe.
-
-
AuthorPosts
- You must be logged in to reply to this topic.