Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

client script

AK001025799
Tera Expert

We need to list all the mandatory fields in the record using a client script (code)

1 ACCEPTED SOLUTION

Hi @AK001025799 ,
1.Create a onLoad client script.

Table - Incident

UI type - All

Type - onLoad

Active - checked

Global - Checked else specify viewName

Rakesh_M_0-1784038333715.png

2.Add the following script with a delay

function onLoad() {
    // Delay execution by 500ms to allow Dictionary Overrides to fully load in the browser
    setTimeout(function() {
        
        var fields = g_form.getEditableFields();
        var mandatoryFieldsList = [];

        for (var i = 0; i < fields.length; i++) {
            var fieldName = fields[i];
            
           
            if (g_form.isMandatory(fieldName)) {
               
                var fieldLabel = g_form.getLabelOf(fieldName);
                mandatoryFieldsList.push(fieldLabel);
            }
        }

        // If mandatory fields are found, display them 
        if (mandatoryFieldsList.length > 0) {
            g_form.addInfoMessage("The following fields are mandatory:\n\n" + mandatoryFieldsList.join(","));
        }
    }, 500); // 500 milliseconds delay
}

 

3.Save the record.

4.you will get a Info message on form load.

Rakesh_M_1-1784038557426.png

 

View solution in original post

15 REPLIES 15

Nishant8
Tera Sage

Hello @AK001025799 , If you just want to print the mandatory fields on the form load, then you can create a UI policy with following details:

- Order 201 (It can be configured with a lower order as well, but a round of testing to be done for this. safer side 201)

- Keep the condition blank something like below and select On Load:

Nishant8_0-1784042230167.png

- Select the Advanced View and paste below script in the 'Execute if true':

function onCondition() {
    var fields = g_form.getEditableFields();
    var mandatoryFields = [];

    for (var i = 0; i < fields.length; i++) {
        if (g_form.isMandatory(fields[i])) {
            mandatoryFields.push(g_form.getLabelOf(fields[i]));
        }
    }

    if (mandatoryFields.length > 0) {
        g_form.addInfoMessage('Mandatory fields on this form: ' + mandatoryFields.join(', '));
    } else {
        g_form.addInfoMessage('No mandatory fields found on this form.');
    }

}

 

P.S: Ideally, Client Script should be used for the scripting.

 

Regards,

Nishant