Custom JS examples

Forums Modern DFFS Custom JS examples

Viewing 4 reply threads
  • Author
    Posts
    • #36150
      Alexander Bautz
      Keymaster

      To use all these examples you must have v1.0.12.0 or later


      Updated February 20, 2023: Added more code examples


      Please note that you can do most of this with rules. Using Custom JS is an option for advanced users.

      Do something before the item is saved

      You can use a function like this to run your own custom code before save. The function must return true to allow save, or false to stop the save. If you use this function it will run before the rules triggering on “Before save of the form” and “After save of the form”. If your function returns false the rules described will not run.

      function dffs_PreSaveAction() {
        // This example will prevent save if the title field value is not "test"
        if (getFieldValue("Title") !== "test") {
          alert("Nope, you cannot save!");
          return false;
        }
        // Allow save
        return true;
      }
      

      Do something after the item is saved

      You can use a function like this to run your own custom code after the item is saved

      function dffs_PostSaveAction() {
        // Do something after the item is saved - for example redirect to another page
        // You can read values from the current item using getFieldValue("Name_of_field")
        location.href = "https://contoso.com/SavedMsg";
      }
      

      Do something if the item is cancelled / closed

      You can use a function like this to run your own custom code if a form is opened and then cancelled out of without saving.

      function dffs_PostCancelAction() {
        // Do something if the item is cancelled - for example redirect to another page
        location.href = "https://contoso.com/CancelMsg";
      }
      

      Do something if the item is deleted

      You can use a function like this to run your own custom code after an item is deleted (from within a form, not from the list view menu).

      function dffs_PostDeleteAction() {
        // Do something if the item is deleted - for example redirect to another page
        location.href = "https://contoso.com/DeletedMsg";
      }
      

      Show a modal dialog

      To show a modal dialog with a custom message you can use this code. Please note that this will be a non-blocking dialog so it will not prevent other code from running when the dialog is shown (like the default browser “alert” function does).

      dffs_showModal({
        "title": "A message to you",
        "body": "Add your message body here. You can use plain text or HTML",
        "isBlocking": true,
        "showClose": true,
        "closeCallbackFn": () => {
          // Add your custom code that runs when the dialog has been dismissed
        },
        "cancelBtnLabel": "Cancel button",
        "cancelBtnFn": () => {
          // Add your custom code to run when clicking the cancel button
        },
        "okBtnLabel": "OK Button",
        "okBtnFn": () => {
          // Add your custom code to run when clicking the OK button
        },
      });
      

      If you don’t include the “cancelBtnFn”, the Cancel button is not shown. The OK button will always show and will by default close the dialog.

      Attach a custom onchange to a text field

      To attach a custom onchange you must have the ID of the field placeholder – you find this by openinghte field property panel for the field in edit mode. Plase note that if you use the same field in your NewForm and your EditForm (or you use the same field in multiple tabs in the same form, this ID will be different. In that case you can use the other selector show below.

      // This will format an US 10 digit phone number in a field with internal name PhoneNumber
      // This example requires that you toggle the Load jQuery option above the Custom JS textarea to ON
      jQuery("#7663586659705095_PhoneNumber input").on("change", function () {
        var s = getFieldValue("Title");
        s = s.replace(/[^\d]/g, '');
        if (s.length !== 10) { alert("Phone Number must be 10 digits") }
        else s = s.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
        setFieldValue("Title", s);
      });
      

      Alternatively you can use this selector in the first line

      jQuery("div[data-fin='Title'] input").on(...
      

      Toggle readonly on a field

      Toggling readonly can be easily done in rules, but if you want to do it using custom js you can do it like this:

      // Set as readonly
      dffs_doReadonly(["Status", "Title", "AssignedTo"]);
      // Undo readonly
      dffs_undoReadonly(["Status", "Title", "AssignedTo"]);
      

      Toggle the visiblity on individual options in a choice field

      dffs_choice_option_hidden["31838417512562245_DropdownChoice"] = { "Enter Choice #1": true, "Enter Choice #3": true };
      

      The id (31838417512562245_DropdownChoice) for your field can be found in the field property panel. Add the label of the options you want to hide and set the value to true. Reversing the action and make them visible again by setting the value as false. You only have to address the options you want to hide or show.

      Detect change in a field

      Use this in for example dffs_PreSaveAction to detect change in the title field:

      if(dffs_beforeProperties["Title"] !== getFieldValue("Title")){
          alert("Title has changed!");
        }
      

      Post any question below.

      Best regards,
      Alexander

    • #36169
      SteveE
      Participant

      Hi Alexander,

      I’m just getting to play around with this and I think you might have missed some of your instructions. You mention setting something as read only but then your code is for hiding a choice.

      Anyway, I’m trying the hide option and I can’t get it to work. I wrapped it in a ready function that I know works. I tried adding # to the beginning of the field reference also.

      Here’s what I’m using:
      jQuery(document).ready(function($) {
      dffs_choice_option_hidden[“8506407795472748_DropdownField”] = {“Enter Choice #3”: true };
      });

      I’m on version 0.9.0.14. Thanks.

      • #36173
        Alexander Bautz
        Keymaster

        Sorry about that – the example has been updated to add the readonly example.

        Not sure why your options are not hiding – can you add some screenshots (or email it to me)?

        Alexander

    • #36587
      Joe Penland
      Participant

      Hi Alexander,

      Can you provide an example of triggering a rule in Custom JS? I am struggling to figure out how to do that with Modern DFFS, and I may be missing it, but can’t seem to find an example (for Modern DFFS) by searching the forum.

      Thank you for your help.

    • #36588
      Alexander Bautz
      Keymaster

      This is currently not possible. What are you trying to accomplish?

      Alexander

      • #36589
        Joe Penland
        Participant

        Oh, I assumed it was possible because in the rule it shows:

        RuleID (for use in Custom JS)

        I am trying to show or hide an element based on the value in a field. For some reason the rule does not apply when the form loads–just when the field is manually updated afterwards. We do have it set to trigger on “Form load and change of conditions for triggering this rule,” but it seems to only trigger on the change of conditions part. Our condition is just if a particular field is “Yes” and the “If yes” part uses the “Show elements” action while the “If no” part uses the “Hide elements” action. It works if the field is manually updated, but it doesn’t seem to run the rule when the form opens.

        My thought was to run the rule on a button click that is part of the form navigation anyway. I will see if I can come up with another way to make sure the rule applies like I want it to.

        If I do resort to an action in the button click, is there a built-in action to hide an element from Custom JS?

      • #36590
        Alexander Bautz
        Keymaster

        I see, the id can only be used to check the status of a rule in your custom js – like this:

        if(dffs_ruleState['030030010565992526']){
            // The rule is true - do something
        }
        

        Are you sure you have checked correct option under “Trigger this rule on”?

        If you have, please give me some details on the field type and any other things I need to replicate the issue.

        Alexander

    • #36591
      Joe Penland
      Participant

      It is just a normal choice field with “No” and “Yes” options. There may be more to this on our side. I am picking up where another user created the beginning of the form. I will do some more research into this and report back whether or not there is something that requires your assistance. Thanks for your willingness to help.

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