› Forums › General discussion › Form Rules
- This topic has 14 replies, 2 voices, and was last updated 3 years, 10 months ago by
Alexander Bautz.
-
AuthorPosts
-
-
January 9, 2020 at 14:43 #28183
Silvestre Kassoka
ParticipantGood Alex,
I am working on a form which I need to do a couple of this but i do not know how,
a) I have a calculated field which give me number of days between two dates which i want displayed on the display form and also to be used to calculate a value based on the number of days. For example , if number of days is = 6 i want to have another field populated with 6 x a fixed value say (10) (annex Rules 1)
Basically i want to make a Rule based on two IF conditionsi) If value in a certain field is selected and Date Fields ( From and TO) are
selected ascertain the value of another field by making a calculation ( difference between (TO and From ) X 10.I have also attached Field Names for more clarity.
Thanks in advance
Attachments:
-
January 10, 2020 at 19:38 #28196
Alexander Bautz
KeymasterSorry for the late reply. I’ll look at this tomorrow morning and give you an answer.
Best regards,
Alexander -
January 11, 2020 at 09:39 #28198
Alexander Bautz
KeymasterTo do this you must use custom js. Add this to your Custom JS in the NewForm or EditForm where you want to do the calculation.
function dffs_PreSaveAction() { var d1 = spjs.utility.getDateFieldAsDateObject("DateColumn1"); var d2 = spjs.utility.getDateFieldAsDateObject("DateColumn2"); if (d1 > d2) { spjs.dffs.alert({ "title": "Error", "msg": "From date must be before to date.", "ok": function () { // Close dlg } }); return false; } var diff = Math.floor((d2 - d1) / 86400000); var x10 = diff * 10; setFieldValue("write_to_field", x10); return true; }
Replace DateColumn1, DateColumn2 and write_to_field with your field internal names.
This code will run on save and write the diff * 10 to the field write_to_field.
Let me know how this works out.
Alexander
-
January 11, 2020 at 16:02 #28199
Silvestre Kassoka
ParticipantHi Alex,
I tried it but i am getting an NaN of the set field.I do not know if i am doing something wrong.
Attachments:
-
January 11, 2020 at 20:21 #28202
Alexander Bautz
KeymasterYou must add some logging to the function to see what is going on – hit f12 to open the developer tools, select “Console” and paste this modified version of the script:
function dffs_PreSaveAction() { var d1 = spjs.utility.getDateFieldAsDateObject("DateColumn1"); console.log(d1); var d2 = spjs.utility.getDateFieldAsDateObject("DateColumn2"); console.log(d2); if (d1 > d2) { spjs.dffs.alert({ "title": "Error", "msg": "From date must be before to date.", "ok": function () { // Close dlg } }); return false; } var diff = Math.floor((d2 - d1) / 86400000); console.log(diff); var x10 = diff * 10; console.log(x10); setFieldValue("write_to_field", x10); return true; } // call the function directly dffs_PreSaveAction();
Remember to change the field internal names with your fields.
What does it output in the console?
Alexander
-
January 14, 2020 at 16:18 #28214
Silvestre Kassoka
ParticipantHi Alex,
From the Logging the date value seem to returning as 0 please see the print screen
attached.Attachments:
-
January 14, 2020 at 16:25 #28217
Alexander Bautz
KeymasterTry opening the console and type in only this line:
spjs.utility.getDateFieldAsDateObject("lydh");
Also, add this line of code and let me know what locale you are using:
_spPageContextInfo.currentLanguage
Alexander
-
January 15, 2020 at 10:25 #28222
Silvestre Kassoka
ParticipantHi Alex,
The date value is not being returned. ( invalid date).Please
see print screen.Regards
Attachments:
-
January 15, 2020 at 16:52 #28227
Alexander Bautz
KeymasterOK, just to ensure it is the correct field name – try this:
getFieldValue("lydh");
Alexander
-
January 15, 2020 at 17:02 #28229
Silvestre Kassoka
ParticipantThe getFieldValue returns the date.
RegardsAttachments:
-
January 15, 2020 at 17:19 #28236
Alexander Bautz
KeymasterI’m not sure why the function does not work, but it looks like your date format is not US English – maybe the function misses your date format. Can you try right clicking the calendar icon and selecting inspect and show me a snippet like the one attached.
To work around this you can modify the function replacing the two first variables with this:
var r1 = jQuery("#dffs_DateColumn1 input").val().split("/"); var d1 = new Date(r1[2],(r1[1]-1),r1[0]); var r2 = jQuery("#dffs_DateColumn1 input").val().split("/"); var d2 = new Date(r2[2],(r2[1]-1),r2[0]);
Alexander
Attachments:
-
January 15, 2020 at 17:35 #28239
Silvestre Kassoka
ParticipantHere goes,
Attachments:
-
January 15, 2020 at 18:31 #28246
Alexander Bautz
KeymasterThanks, it looks like I have the wrong date format for LCID = 2070 in my function. Try setting this ABOVE where you insert the function in Custom JS and see if my original function works now:
spjs.dffs.data.lcidToDateFormat["2070"] = "d/m/y";
Alexander
-
January 16, 2020 at 12:05 #28269
Silvestre Kassoka
ParticipantHi Alex,
After inserting the above code the intial code works like a charm.!!
Thank you so much!
-
January 16, 2020 at 16:38 #28271
Alexander Bautz
KeymasterThanks for the update – I’ll include this fix in the next version, but you can keep your code as is even if you update to the new version – the line used to override the 2070 LCID won’t interfere.
Alexander
-
-
AuthorPosts
- You must be logged in to reply to this topic.