that did not work. Still no value appears.
Could it be because the JavaScript runs on form open and the calculated value control hasn't had a chance to contain a value yet? Does JavaScript run BEFORE the form variables load?
Hi @dgraves
Is the "JulianToday" field filled with a value?
Here is my form
Control Name: JulianDate
Type: Single line of text (not connected)
javaID: jsJulianDate
Control Name: JulianToday
Type: Calculated Value
javaID: jsJulianToday
Formula: Named Control: Title
(I just use the value from Title)
Control Name: Title
Value: 12345678
Custom JavaScript in Form Settings
NWF.FormFiller.Events.RegisterAfterReady(function () {
{
//Get Value
var varJulianToday = NWF$("#" + jsJulianToday).val();
//Set Value
NWF$("#" + jsJulianDate).val(varJulianToday);
};
NWF$("#" + jsJulianToday).change(function () {
//Get Value
var varJulianToday = NWF$("#" + jsJulianToday).val();
//Set Value
NWF$("#" + jsJulianDate).val(varJulianToday);
});
});
Preview
Title has the default value "12345678".
JulianToday Formula is the value of Title
JulianDate is assign the value from JulianToday from JavaScript
Changing the value of JulianToday
When Title is changed, JulianToday value also changes as per Formula
JulianDate is assign the value from JulianToday from JavaScript event - NWF$("#" + jsJulianToday).change
Slight modification to the code to show difference between Initial value and changed value
Preview
Also, there is a Typo in your code.
Thanks for your great examples! They make sense.
Yes, JulianToday has a value on form load. It's a calculated value using a lookup function.
Is there a way to put a lookup function in the Form settings JavaScript? I read on another persons forum thread that Form settings JavaScript reads the value of calculated value control before it is really populated/calculated.
I really don't need the JulianToday control. I only added that because it's the only way I knew to populate the value with a lookup. The lookup formula that I use in the JulianToday control also uses a form variable.
- Form variable: vToday
- formula: formatDate(CurrentDate,"d")
- Calculated value control: JulianToday
- formula: lookup("Julian Dates","Date",vToday,"Title")
I tried building the lookup without using the vToday variable and just use the formatDate in its place but I couldn't get it to work. Do you know of a way to add these two things to the Form settings JS?
that did not work. I had tried that before and even tried adding parenthesis and/or quotes around the formatDate function and it still does not work. That's why I resorted to using the variable.
When it comes to auto-populating a value, or setting a value on some arbitrary condition (typically if the field is left blank), then I tend to take the easy way out and rely on the built in Nintex Rule System.
Why? Well it's because inside of a Rule, like a Calculated Control, you're given access to the Runtime Functions (ie: Lookup), AND they also will execute anytime your Control has been changed, automatically. This is both useful for inserting data when there isn't any initially, or when a Control has had its value removed.
There may be some extra overhead performance wise if you're doing this on tons of Form Controls in a single form, but here and there shouldn't hurt. Also, they implemented a "SetValue" option via a Rule in the On-Premise Responsive Forms, so I don't feel too terribly bad having made my own for the older Classic Forms.
Let's say that I have a List of Fruits and it happens to have 12 Fruits in it, making it 1 Fruit per Month:
And I got a form where when I load it up, I wanna have the JulianFruit control populate with a Fruit from that list, based on the current month, but that can also be changed by the user if they so choose to input something differently:
Let's make a new Rule on the JulianFruit Control:
Name the Rule, set the Type to Formatting (default), and make sure that neither of the bottom two checkboxes for Disable / Hide are checked. We don't actually want to use this rule to format the control in the any of the given ways because we're making our own rules now (please clap)!
To add code to the rule, I'm just going to click on the f(x) button next to the Condition textarea, where I enter the following:
(function(formControlCall, julianFruitLookup) {
var formControlID = formControlCall.split("'")[1] || "";
var targetControl = sourceContext.find("[formcontrolid='" + formControlID + "'].nf-filler-control").find("[formcontrolid][id]");
var currentValue = targetControl.val();
if (!currentValue) {
targetControl.val(julianFruitLookup).trigger("blur");
}
return false;
}("{Control:Self}", lookup("Fruits List", "ID", ({Common:CurrentDate}.getMonth() + 1), "Title")))
With the code inserted, I can click on the OK button to save and close the Formula Builder:
(note: this code is strictly an example of what I need to use for my form / list combo. I have different code for you to use on your form below!)
Let's Publish the Form, go back to the List, and create a New Item:
Because this is the 9th Month, and I'm getting the Title of the Fruit in my Fruits List by the ID value that matches the current month, we know that it's correctly being populated with a default value:
I'll now Save the Form and view it in View Mode:
Still the same.
Let's see what happens if I edit the form and change the value:
Viewing the form again, I can see that my custom Julian Fruit is saved:
But What about your specific Form / Lookup? Because you yourself are working with Dates, you should be aware that the Current Date reference actually returns a real javascript Date Object. So if you happen to be wanting to use the Current Date reference as a value to filter on in your lookup, then you might want to use the following:
(function(formControlCall, julianFruitLookup) {
var formControlID = formControlCall.split("'")[1] || "";
var targetControl = sourceContext.find("[formcontrolid='" + formControlID + "'].nf-filler-control").find("[formcontrolid][id]");
var currentValue = targetControl.val();
if (!currentValue) {
targetControl.val(julianFruitLookup).trigger("blur");
}
return false;
}("{Control:Self}", lookup("Fruits List", "ID", ({Common:CurrentDate}.toLocaleDateString()), "Title")))
Notice how I changed the code after the {Common:CurrentDate} reference to: ({Common:CurrentDate}.toLocalDateString())
What this will do is return a localized string value resembling the way you seem to have your Dates formatted in your Julian Dates list. So todays day (as of writing this) of "Mon Sep 19 2022 01:08:39 GMT-0400 (Eastern Daylight Time)" will get converted into the string '9/19/2022'.
Let me know if this works for your situation! We might have to play around with the Date formatting for the lookup a little bit, but I'm sure we can get it sorted.
Wow! Thanks for this! I ran out of time to try this today. Hopefully I can make time tomorrow. I'll let you know how it goes!