Form Rules

Home Forums General discussion Form Rules

Viewing 14 reply threads
  • Author
    Posts
    • #28183
      Silvestre Kassoka
      Participant

        Good 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 conditions

        i) 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

      • #28196
        Alexander Bautz
        Keymaster

          Sorry for the late reply. I’ll look at this tomorrow morning and give you an answer.

          Best regards,
          Alexander

        • #28198
          Alexander Bautz
          Keymaster

            To 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

          • #28199
            Silvestre Kassoka
            Participant

              Hi 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:
            • #28202
              Alexander Bautz
              Keymaster

                You 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

              • #28214
                Silvestre Kassoka
                Participant

                  Hi Alex,

                  From the Logging the date value seem to returning as 0 please see the print screen
                  attached.

                  Attachments:
                • #28217
                  Alexander Bautz
                  Keymaster

                    Try 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

                  • #28222
                    Silvestre Kassoka
                    Participant

                      Hi Alex,

                      The date value is not being returned. ( invalid date).Please
                      see print screen.

                      Regards

                      Attachments:
                    • #28227
                      Alexander Bautz
                      Keymaster

                        OK, just to ensure it is the correct field name – try this:

                        getFieldValue("lydh");

                        Alexander

                      • #28229
                        Silvestre Kassoka
                        Participant

                          The getFieldValue returns the date.
                          Regards

                          Attachments:
                        • #28236
                          Alexander Bautz
                          Keymaster

                            I’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

                          • #28239
                            Silvestre Kassoka
                            Participant

                              Here goes,

                              Attachments:
                            • #28246
                              Alexander Bautz
                              Keymaster

                                Thanks, 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

                              • #28269
                                Silvestre Kassoka
                                Participant

                                  Hi Alex,

                                  After inserting the above code the intial code works like a charm.!!

                                  Thank you so much!

                                • #28271
                                  Alexander Bautz
                                  Keymaster

                                    Thanks 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

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