Skip to main content

Script

info

To be able to use the script control, you must activate the corresponding feature in the client administration.

The script control executes JavaScript in a form. In this way, you can calculate values, load data dynamically, validate data when saving, etc.
The control element is not displayed to the user.

Configuration

The following options are available within the configuration:

  • Title:
    The title is not displayed and helps you to find the script control again.
  • Use external script:
    By default, you can specify the script directly in the configuration. If you activate this option, you can load a script from an external source instead.
  • Inline script:
    If you are not using an external script, you can define the script to be executed here. Details on the script API can be found below.
    You can also use placeholders to insert dynamic values.
  • External script url:
    If you are using an external script, you can enter the url of the script here.

Placeholder

This control does not provide placeholders.

Script API

The following entry points are available:

  • linqi.forms.addOnLoadEventHandler(callback: (formContext: FormContext) => void | Promise<void>)
    This function allows you to insert a callback function that is executed when the form is loaded.
    The callback function receives the FormContext (see below) as a parameter. You can use this to interact with the various control elements and areas.
  • linqi.forms.removeOnLoadEventHandler(callback: (formContext: FormContext) => void | Promise<void>)
    This function removes a callback function that is executed when loading.
  • linqi.forms.addOnSaveEventHandler(callback: (formContext: FormContext, event: FormSaveEvent) => void | Promise<void>)
    This function allows you to insert a callback function that is executed before the form is saved. The callback function receives the FormContext (see below) as a parameter. You can use this to interact with the various control elements and areas.
    A SaveEvent is also passed. If you set the cancel property to true, the save process is not carried out.
  • linqi.forms.removeOnSaveEventHandler(callback: (formContext: FormContext, event: FormSaveEvent) => void | Promise<void>)
    This function removes a callback function that is executed before saving.
  • linqi.forms.addOnPostSaveEventHandler(callback: (formContext: FormContext) => void | Promise<void>)
    This function allows you to insert a callback function that is executed after the form has been saved. The callback function receives the FormContext (see below) as a parameter. You can use this to interact with the various control elements and areas.
  • linqi.forms.removeOnPostSaveEventHandler(callback: (formContext: FormContext) => void | Promise<void>)
    This function removes a callback function that is executed after saving.
  • linqi.forms.addOnNextStepEventHandler(callback: (formContext: FormContext, event: FormNextStepEvent) => void | Promise<void>)
    This function allows you to insert a callback function that is executed before the process is transferred to the next step. The callback function receives the FormContext (see below) as a parameter. You can use this to interact with the various control elements and areas.
    A FormNextStepEvent is also passed. If you set the cancel property to true, the process is not transferred to the next step. The event also has a buttonId property that contains the ID of the button clicked as a value.
  • linqi.forms.removeOnNextStepEventHandler(callback: (formContext: FormContext, event: FormNextStepEvent) => void | Promise<void>)
    This function removes a callback function that is executed before the process switches to the next step.

FormContext

The FormContext class is the main object with which you interact with a form. A FormContext object has the following methods:

  • getSection(internalName: string): FormContextSection | null
    This function queries a section using the internal name. If the area is not found, zero is returned.
    Note: In order to keep scripts as stable as possible, it is only possible to query areas using an internal name. An area that does not have an internal name cannot be queried.
  • getSectionById(id: string): FormContextSection | null
    This function queries a section using the ID. If the area was not found, zero is returned.
  • getControl(internalName: string): FormContextControl | null
    This function queries a control element using its internal name. If the control is not found, null is returned.
    Note: In order to keep scripts as stable as possible, it is only possible to query controls using an internal name. A control that does not have an internal name cannot be queried.
  • getControlById(id: string): FormContextControl | null
    This function queries a control element using the Id. If the control was not found, null is returned.
  • showInformation(message: string): void
    This function displays an information banner at the top of the form.
  • showWarning(message: string): void
    This function displays a warning banner at the top of the form.
  • showError(message: string): void
    This function displays an error banner at the top of the form.
  • showSuccess(message: string): void
    This function displays a success banner at the top of the form.
  • save(): void
    This function saves the form.
  • moveToNextStep(buttonId: string, disableMandatoryCheck: boolean): void
    This function transfers the process to the next step. You must specify the ID of the "clicked" button as buttonId. You can determine this using the getControl function.
    If you set disableMandatoryCheck to true, mandatory fields are not checked.

FormContextSection

This class represents a section in the form. This class has the following methods:

  • isHidden(): boolean
    This function returns whether the section is hidden or not.
  • setHidden(hidden: boolean): void
    Determines whether the section is hidden or not.

FormContextControl

This class represents a control in the form. This class has the following methods:

  • getId(): string
    Returns the Id of the control.
  • getValue(): any
    Returns the current value of the control. The data type varies depending on the control.
  • setValue(value: any)
    Sets the new value of the control. If possible, the data type is converted to the data type of the control.
  • addOnChangeEventHandler(callback: (formContext: FormContext, value: any) => void)
    Inserts a callback function that is called when the value of the control changes.
    The FormContext and the new value are passed as parameters.
  • isHidden(): boolean
    Returns whether the control is hidden.
  • setHidden(isHidden: boolean)
    Sets whether the control is hidden.
  • isMandatory(): boolean
    Returns whether the control is mandatory.
  • setMandatory(mandatory: boolean)
    Sets whether the control is mandatory.
  • isReadonly(): boolean
    Returns whether the control is readonly.
  • setReadonly(readonly: boolean)
    Sets whether the control is readonly.
  • saveIfReadonly(): boolean | null
    Returns whether the control is also saved if it is readonly. If no value has been set, null is returned.
  • setSaveIfReadonly(readonly: boolean)
    By default, controls that are readonly are not saved. You can use this function to specify whether the control is also saved if it is readonly.

Sample script

linqi.forms.addOnLoadEventHandler(function(formContext) {
formContext.getControl("ScriptControl").addOnChangeEventHandler(function(fc, val) {
formContext.getControl("TargetUpdate").setValue("COPY " + val);
});
});

linqi.forms.addOnSaveEventHandler(function(formContext, saveEvent) {
var val = formContext.getControl("ScriptControl").getValue();
   if(val == "Invalid") {
formContext.showWarning("Dont enter Invalid");
saveEvent.cancel = true;
}
});