< All Topics

Form Script

A form script can be used to perform dynamic operations/manipulations in the form, while filling the form from the mobile device.

A form script can have one or more simple javascript functions written inside of it.

The functions need to be registered with the value change event of the fields in the form while designing the form from the form builder.

Various actions, such as automatically changing field labels/values, showing error/warning alerts, based on the values entered in the form, can be performed by writing in the form script functions, which are registered with the callback.

 

 

Helper Functions list:

  • //Function to show error message
    function show_error(ws, title, message, type) {
    ws.showAlert(JSON.stringify({
    “t”: title,
    “m”: message,
    “tp”: type
    }));
    }
  • //Function to set today’s date and time
    function setTodaysDateTime(ws) {
    let date = new Date();
    datestring = date.toISOString().slice(0, 16).replace(‘T’, ‘ ‘);
    ws.setWidgetValue(‘Today_Date_Time’, datestring);
    }
  • //Function to get subtitle information attached to a widget and set it to another widget (in the same form)
    function updateWidgetSubInfo(ws) {
    let variable1 = ws.getWidgetValue(“Widget_Unique_ID”);
    let variable1_fost = JSON.parse(variable1)[0][‘fost’];
    variable1_foid = JSON.parse(variable1)[0][‘foid’]; //optional – only add if further information needs to be extracted
    ws.setWidgetValue(‘Other_Widget_UniqueID’, variable1_fost);
    ws.reloadUICell(‘Other_Widget_UniqueID’);
    }
  • //Function to get information from widget in another form and set in current form
    function updateFormObjectIDWidgetInfo(ws) {
    let variable2 = ws.updateFormObjectWidgetValue(variable1_foid, “Widget_UniqueID_OtherForm”);
    ws.setWidgetValue(‘Unique_ID’, variable2);
    ws.reloadUICell(‘Unique_ID’);
    }
  • // Function to compare Entered Date and Today’s date:
    function compareDateWithToday(ws) {
    if (entered_date_str > datestring) {
    show_error(ws, “TRUE”, “Entered Date is MORE than Today’s Date”,“Error”);
    //any other action to perform
    }
    else {
    show_error(ws, “FALSE”, “Entered Date is LESS than or equal to Today’s Date”,“Error”);
    }
    }
  • //Function to compare two date fields
    function compareDateFields(ws) {
    if (new_date_str > entered_date_str) {
    show_error(ws, “YES”, “New Date is MORE than Entered.”,“Error”);
    }
    else {
    show_error(ws, “NO”, “New Date is LESS than Entered.”,“Error”);
    }
    }
  • //Function to compare one date field with one hard-coded date
    function compareDateWithField(ws) {
    if (hardcoded_date > entered_date_str) {
    show_error(ws, “YES”, “Hardcoded Date is MORE than Entered.”,“Error”);
    }
    else {
    show_error(ws, “NO”, “Hardcoded Date is LESS than Entered.”,“Error”);
    }
    }
  • //Example usage of these functions:
    var user_foid = “”;
    var datestring = “”;
    var entered_date_str = “”;
    var new_date_str = “”;
    var hardcoded_date = “”;

 

 

  • function on_load(ws) {
    show_error(ws, “ERROR!!!”, “Test Error”,“Error”);
    setTodaysDateTime(ws);
    const value = “2024-02-29 10:10”;
    ws.setWidgetValue(“Hardcoded_Date_Time”, value);
    }

 

  • function on_entered_time(ws) { //add on_entered_time to the On Value Change field of the widget
    entered_date_str = ws.getWidgetValue(‘Unique_Identifier’);
    compareWithToday(ws);
    hardcoded_date = ws.getWidgetValue(‘Hardcoded_Date_Time_Unique_ID’);
    compareDateWithField(ws);
    }

 

  • function on_newdate(ws) { //add on_newdate to the On Value Change field of the widget of new date
    new_date_str = ws.getWidgetValue(‘New_Date_WidgetID’);
    compareDateFields(ws);
    }

 

  • function on_populate(ws) { //add on_populate to the On Value Change field of the widget
    updateWidgetSubInfo(ws);
    ws.downloadAndCacheAssetFormObject(variable1_foid, “widget_info”, false);
    }

 

  • function widget_info(ws) {
    updateFormObjectIDWidgetInfo(ws);
    }
Table of Contents