I just want to ask whether we can trigger the button click event programmatically? Thank you.

  1. Jye Cusch
    Hi Lan,
    It is possible, but could tell us what you're trying to do, because there is likely a better way, such as firing custom events.
  2. Unknown User (support)
    Hi Jye,
    Actually I was asked that question. :-) The situation is like the button click calls web services and this click event could also be triggered by some other function in the form.
    It would be great if you can show me both ways, I would like to know too.
  3. Unknown User (support)
    Very detailed explanation. Thank you Jye. :-)
CommentAdd your comment...

1 answer

    1. Jye Cusch
      Hi Lan,
      Happy to explain both options, although I believe custom events are the right solution for the requirement you described.

      Option 1 - Trigger Click Event on Button (HTML Only).

      You can trigger the click event of a button fairly easily with a line of JavaScript that uses JQuery. The line takes the node of the button field (just double click it in the script editor), finds the HTML button element inside it and triggers the click event.
      The code is:

      The script editor would look something like this when setup correctly:
      If you are triggering this from a business/validation/calculation rule or some other script that runs in the PDF receipt make sure you wrap it in something like:
      // code here

      If the code gets run in the PDF you will get an error, since JQuery is not available in the PDF version of a form.

      Option 2 - Fire Custom Events

      I believe this option will work out better, 1. because it works in both the HTML and PDF versions of the form, and 2. because it's easier to maintain/modify, for example if you decide you no longer want the button on the form, but you still want the code that 'clicks' the button to run. Option 1 would break when the button was removed, but this option separates the button from the action it performs, meaning the button can be removed without any issues.
      The way this option works is by moving the code that the runs when the button gets clicked to a business rule instead. Then you can trigger that business rule with an event, the button can trigger that event, but so can any other code in your form.
      Here are the steps to implement this option:
      1. Add a newBusiness Rule [General Purpose] to your form
      2. Give the business rule a name to indicate what it does, e.g. 'TaxCalcEventHandler'
      3. Edit the script of the business rule and switch to theTriggering Conditions tab
      4. Uncheck Trigger rule at initialization
      5. Click the + (plus)button next toAdditional Events

      6. Add the name of your event (you need to remember this for later), e.g. 'TaxCalcEvent'
      7. Save the custom event
      8. Switch back to theScript tab, put the code that you originally had on the button, in the script for the business rule
      9. Save the business rule
      10. Update the button so that instead of running the code itself, it instead fires your new event, the code for that is:
      sfc.fireEvent('YourCustomEvent', me, null);

      for more info on this method, look into the Composer JavaScript API documentation
      11. Add that exact same code anywhere else in your form where you want the same action to be performed.

      Hope this helps,
    CommentAdd your comment...