UI Script (UI type: all) calling issue

Mainak1
Tera Contributor

I have a UI Script of type all as following:

(function() {
	if (typeof g_ui_scripts !== 'undefined') return CincValidatorUtils;
})();

function CincValidatorUtils() {
    return {
        fieldNAValidator: function(g_form, field, fieldValue) {
            // some logic using g_form
        }
    };
}

I am able to call it with g_ui_scripts as follows:

function onLoad() {
    var fieldsToWatch = [
        'u_comments',
        'u_issue_summary'
    ];
    var validator;
    if (typeof g_ui_scripts !== 'undefined') {
        g_ui_scripts.getUIScript('CincValidatorUtils').then(script => {
            validator = script();
        });
    }

    var handler = function(fieldName, originalValue, newValue) {
        if (typeof g_ui_scripts !== 'undefined') {
            if (fieldsToWatch.indexOf(fieldName) > -1) {
                validator.fieldNAValidator(g_form, fieldName, newValue);
            }
        }
    }
    g_form.onUserChangeValue(handler);
}

But unable to call it with ScriptLoader:

function onSubmit() {
    var fieldsToWatch = [
        'u_comments',
        'u_issue_summary'
    ];
    var isValid = false;
    if (g_scratchpad.isFormValid) {
        return true;
    }

	ScriptLoader.getScripts(['CincValidatorUtils.jsdbx'], function() {
		g_form.addInfoMessage("script loaded");
		var validator = CincValidatorUtils();
		g_form.addInfoMessage("validator loaded");
		for (var elem of fieldsToWatch) {
            if (validator.fieldNAValidator(g_form, elem, g_form.getValue(elem).toString())) {
                isValid = false;
            }
        }
	})
    if (isValid) {
        g_scratchpad.isFormValid = true;
        g_form.submit(g_form.getActionName());
    }
    return false;
}

I want to call it from all the UIs like classic and ServicePortal without any globalThis or window object. Is it possible to do?

0 REPLIES 0