Using Glide Ajax on Multi Row Variable Set onSubmit Client Script for Validation

palvinder99
Tera Contributor

Hello I am trying to implement validation on multi row variable set (MVRS) in a scope application which calls a script include using glide Ajax. the logic is preventing adding row to the MVRS when validation fails, which it working as expected. but the issue i am having is that even when validation succeeds it still doesn't add row to the MVRS, any suggestion on how to handle adding row  when validation passes to the MVRS.

function onSubmit() {
    // Get field values from the form
    var sourceField = g_form.getValue('source_ip_ipv4');
    var targetField = g_form.getValue('destination_ip_ipv4');
    var portProtocolField = g_form.getValue('protocol_ports_ip_ipv4');
    var hasError = false;

    // Create GlideAjax object for source IP validation
    var gaSource = new GlideAjax('FirewallUIValidationUtil');
    gaSource.addParam('sysparm_name', 'validateIPV4s');
    gaSource.addParam('sysparm_ip_list', sourceField);

    // Create GlideAjax object for destination IP validation
    var gaDest = new GlideAjax('FirewallUIValidationUtil');
    gaDest.addParam('sysparm_name', 'validateIPV4s');
    gaDest.addParam('sysparm_ip_list', targetField);

	// GlideAjax object for source-destination combination validation
    var gaCombination = new GlideAjax('FirewallUIValidationUtil');
    gaCombination.addParam('sysparm_name', 'validateSourceAndDestCombination');
    gaCombination.addParam('sysparm_source_ip_list', sourceField);
    gaCombination.addParam('sysparm_destination_ip_list', targetField);


    // Track completion of both validations
    var sourceValidated = false;
    var destValidated = false;
    var combinationValidated = false;


    // Track completed validations
    var validationsCompleted = 0;
    var totalValidations = 3; // We have three async validations

    function checkValidationCompletion() {
        //validationsCompleted++;

        if (validationsCompleted === totalValidations) {
            if (!hasError) {
				//g_form.addInfoMessage('Rule Valited!');
				
				
				var rowAdd = {
					source_ip_ipv4: sourceField,
					destination_ip_ipv4: targetField,
					protocol_ports_ip_ipv4: portProtocolField
				};

				//g_form.addInfoMessage(JSON.stringify(rowAdd));

				var mvrsData = g_form.getValue('firewall_rules_ip_ipv4');

				var currentRows = mvrsData ? JSON.parse(mvrsData) : [];

				currentRows.push(rowAdd);

			
				var updatedRows = JSON.stringify(currentRows);

				g_form.addInfoMessage(updatedRows);
				g_form.setValue('firewall_rules_ip_ipv4',updatedRows);
				//alert(g_form.getValue('firewall_rules_ip_ipv4'));
                //g_form.submit(); // Only submit if there are no errors
				//return true;
            }
        }
    }

    //Validate Source IPs
    gaSource.getXMLAnswer(function(answer) {
        var res = JSON.parse(answer);
        if (!res.isValid) {
            g_form.showFieldMsg('source_ip_ipv4', 'Invalid IP(s): ' + res.invalidIPs.join(', '), 'error');
            hasError = true;
        }
        if (res.duplicateIPs.length > 0) {
            g_form.showFieldMsg('source_ip_ipv4', 'Duplicate IP(s): ' + res.duplicateIPs.join(', '), 'error');
            hasError = true;
        }
        sourceValidated = true;
        checkValidationCompletion();
    });


    // Validate Destination IPs
    gaDest.getXMLAnswer(function(answer) {
        var res = JSON.parse(answer);
        if (!res.isValid) {
            g_form.showFieldMsg('destination_ip_ipv4', 'Invalid IP(s): ' + res.invalidIPs.join(', '), 'error');
            hasError = true;
        }
        if (res.duplicateIPs.length > 0) {
            g_form.showFieldMsg('destination_ip_ipv4', 'Duplicate IP(s): ' + res.duplicateIPs.join(', '), 'error');
            hasError = true;
        }
        destValidated = true;
        checkValidationCompletion();
    });

	// Validate Source & Destination Combination
    gaCombination.getXMLAnswer(function(answer) {
        var res = JSON.parse(answer);
        if (!res.isValid) {
            g_form.showFieldMsg('source_ip_ipv4', res.errorMsg, 'error');
            hasError = true;
        }
        combinationValidated = true;
        checkValidationCompletion();
    });
    return false; // Prevent default form submission while waiting for async response
}
0 REPLIES 0