Form Scripts in servis.ai empower administrators to manage and customize Form behaviors within any App using JavaScript.
This low-code feature allows for the consolidation and control of all Form Rules and actions from a single location, enhancing efficiency and scalability.
Key Features
- Comprehensive Control: Form Scripts provide full control over Form Fields, enabling actions such as setting fields as read-only, mandatory, or adjusting their visibility based on specific conditions.
- Low-Code Environment: While Form Scripts require some coding, the amount is minimal, making it accessible for administrators to set up and manage.
- Parent-Child Functionality: Form Scripts operate at both the App level and within Lines (sub-records) of the App, functioning in a Parent-Child relationship to ensure cohesive behavior across the application.
Enabling Form Scripts
1.Navigate to App Configuration:
- Go to Settings and select Apps.
- Select the desired App.
2.Activate Form Scripts:
- On the ‘General’ tab scroll down to the ‘Form Script’ section, click the ‘Use Form Script’ checkbox.
- This action will enable the custom code script field.
3.Enter Custom Code:
- Input your JavaScript code into the script field to define the desired form behaviors.
- Utilize the ‘CTRL + space’ command to view available fields by their Field Name; upon selection, they will auto-convert to their System Name to ensure consistency.
4.Save and Modify:
- Click Save to apply the Form Script.
- You can return to App’s Settings to modify the Form Script as needed.
Form Actions in Form Scripts
Form Scripts support various actions that were previously managed through Form Rules, now enhanced and simplified within the scripting environment. These actions include:
| FORM ACTION | CODE |
| Add Validation to a Field | addFieldValidation: (fieldName, validationCallback) |
| Change a Field | fieldChanged: (fieldName) |
| Make Fields Read Only | makeFieldsReadOnly: (fieldNames, value) |
| Make Fields Required | makeFieldsRequired: (fieldNames, value) |
| Make Fields Visible | makeFieldsVisible: (fieldNames, value) |
| Make Line Fields Read Only | makeLineFieldsReadOnly: (lineApp, fieldNames, value) |
|
Make Line Fields Required |
makeLineFieldsRequired: (lineApp, fieldNames, value) |
|
Make Line Fields Visible |
makeLineFieldsVisible: (lineApp,fieldNames, value) |
| Reset Fields | resetFields: (changedField, fieldNames) |
| Set a Field Value | setFieldValue: (changedField, fieldName, value) |
| Hide or Show Columns in Lines | makeLineFieldsVisible: (lineApp, fieldNames, value) |
| Make Columns Read Only in Lines | makeLineFieldsReadOnly: (lineApp, fieldNames, value) |
| Make Columns Required in Lines | makeLineFieldsRequired: (lineApp, fieldNames, value) |
These commands allow for dynamic and responsive form behaviors tailored to specific requirements.
Example 1
Consider a scenario where, if a deal’s sales stage is ‘Closed Won’, certain fields become read-only, and the status is set to closed.
(function(form, context){
// If deal sales stage is 'Closed Won', make amount and closeDate read-only, set status to closed
if (form.deal_fa_field16 === '69dcb80b-ebb8-43cb-ab61-b23596ac800f') {
context.formActions.makeFieldsReadOnly(['deal_fa_field0', 'deal_fa_field8'], true);
context.formActions.setFieldValue('deal_fa_field13', false);
}
}(form, context));
Explanation:
- form.deal_fa_field16 represents the sales stage field.
- The condition checks if the sales stage equals the specific ID for ‘Closed Won’.
- If true, it sets the ‘amount’ and ‘closeDate’ fields to read-only and updates the status field accordingly.
Example 2
Consider a scenario where the Discount % field must only accept numbers from 0 to 100 with up to 2 decimals. We add a validation to block any other input.
(function(form, context) { // Validate "Discount %" (deal_fa_field21): must be 0–100 with up to 2 decimals context.formActions.addFieldValidation('deal_fa_field21', function(value, formValues) { // Allow empty if the field isn't required; return 'Required' instead if you want to force a value. if (value === null || value === undefined || String(value).trim() === '') { return ''; } const str = String(value).trim(); // Must be a number format with up to 2 decimals if (!/^d{1,3}(.d{1,2})?$/.test(str)) { return 'Enter a number with up to 2 decimals (e.g., 12 or 12.34).'; } const num = Number(str); if (!Number.isFinite(num)) { return 'Enter a valid number.'; } if (num < 0 || num > 100) { return 'Value must be between 0 and 100.'; } return ''; }); }(form, context));
Explanation:
deal_fa_field21 represents the Discount % field.context.formActions.addFieldValidation attaches a validator that runs on change.
The callback rejects non-numeric formats, more than two decimals, and values outside 0–100 by returning a message. Returning an empty string ('') means the value is valid and the form can be saved.
Relation to Form Rules
While Form Rules continue to function and provide a no-code solution for managing form behaviors, Form Scripts offer a more streamlined and scalable approach, especially beneficial for apps with complex dependencies. Administrators can choose to transition to Form Scripts to consolidate and enhance their form management processes.
Note: Only administrators have the permissions to customize Form Scripts.
By leveraging Form Scripts, you can achieve a higher degree of customization and control over your app’s forms, enhancing both functionality and user experience within servis.ai.