I am working on a validation requirement for the Change Request table and need help with a character

PratikshaAbhang
Tera Contributor

Requirement:

  1. Please Note: This validation is not applicable for Data Patch tickets where the change type is emergency  & justifications is eligiblefordataPatchCR

  2. Character Limits

    • Minimum of 100 characters for the following sections:

      • Description
      • Justification
      • Risk and Impact Analysis
    • Minimum of 15 characters for the following sections:

      • Implementation Plan

Backout Plan

i write below script but its not woring

function onSubmit  () {
// Initialize GlideAjax for server-side validation
var ga = new GlideAjax('ValidateChangeRequest1');
ga.addParam('sysparm_name', 'validateDataPatchEligibility');
ga.addParam('service_name', g_form.getValue('business_service'));

// Perform the synchronous GlideAjax call to get the response immediately
//var answer = ga.getXMLAnswer();
ga.getXMLAnswer(function(answer) {
var changeType = g_form.getValue('type');
var isDataPatchEligible = (answer == 'true');
var description = g_form.getValue("description");
// Check if it's a Data Patch ticket with an emergency type
if (changeType == 'emergency' && isDataPatchEligible) {
alert("IDFC11");
// No validation needed for Data Patch tickets with emergency type
alert("No validation because of data patch ticket");
return true; // Allow form submission
} else if (description.length < 100) {
// Check if the description length is valid

// If description is too short, show an error message and prevent form submission
g_form.addErrorMessage("Description must be at least 100 characters.");
return false; // Prevent form submission
}
return false;

});
}

var ValidateChangeRequest1 = Class.create();
ValidateChangeRequest1.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateDataPatchEligibility: function() {

gs.warn("Running data patch eligibility validation");

var businessServiceId = this.getParameter("service_name");
var serviceGr = new GlideRecord("cmdb_ci_service");

// Check if the service record exists and is eligible
if (serviceGr.get(businessServiceId)) {
gs.warn("Service ID: " + businessServiceId);
gs.warn("Service justification: " + serviceGr.justification);

if (serviceGr.justification == "**EligibleForDataPatchCR**") {
gs.warn("Service is eligible for Data Patch CR.");
return "true";
}
}

return "false";
},
type: 'ValidateChangeRequest1'
});

2 REPLIES 2

Runjay Patel
Giga Sage

Hi @PratikshaAbhang ,

 

You script looks okay. The way you are check description same way just add else if condition.

else if (description.length < 100) {
            g_form.addErrorMessage("Description must be at least 100 characters.");
            return false; // Prevent form submission
        }else if (Justification.length < 100) {
            g_form.addErrorMessage("Justification must be at least 100 characters.");
            return false; // Prevent form submission
        }
        return false;

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

boteeuwen
Tera Expert

This might help you:

function onSubmit() {
    // Get all required field values
    var changeType = g_form.getValue('type');
    var description = g_form.getValue('description');
    var justification = g_form.getValue('justification');
    var riskImpactAnalysis = g_form.getValue('risk_impact_analysis');
    var implementationPlan = g_form.getValue('implementation_plan');
    var backoutPlan = g_form.getValue('backout_plan');
    
    // Initialize GlideAjax
    var ga = new GlideAjax('ValidateChangeRequest');
    ga.addParam('sysparm_name', 'validateDataPatchEligibility');
    ga.addParam('sysparm_business_service', g_form.getValue('business_service'));
    
    ga.getXMLAnswer(function(answer) {
        var isDataPatchEligible = (answer === 'true');
        
        // If it's a data patch eligible emergency change, skip validation
        if (changeType === 'emergency' && isDataPatchEligible) {
            return true;
        }
        
        // Validation messages array
        var errorMessages = [];
        
        // Validate 100 character minimum fields
        if (description.trim().length < 100) {
            errorMessages.push('Description must be at least 100 characters');
        }
        
        if (justification.trim().length < 100) {
            errorMessages.push('Justification must be at least 100 characters');
        }
        
        if (riskImpactAnalysis.trim().length < 100) {
            errorMessages.push('Risk and Impact Analysis must be at least 100 characters');
        }
        
        // Validate 15 character minimum fields
        if (implementationPlan.trim().length < 15) {
            errorMessages.push('Implementation Plan must be at least 15 characters');
        }
        
        if (backoutPlan.trim().length < 15) {
            errorMessages.push('Backout Plan must be at least 15 characters');
        }
        
        // Display all error messages if any
        if (errorMessages.length > 0) {
            g_form.addErrorMessage(errorMessages.join('\n'));
            return false;
        }
        
        return true;
    });
    
    // Always return false initially since we're using async validation
    return false;
}

var ValidateChangeRequest = Class.create();
ValidateChangeRequest.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    validateDataPatchEligibility: function() {
        try {
            var businessServiceId = this.getParameter('sysparm_business_service');
            
            if (!businessServiceId) {
                gs.debug('Business Service ID is empty');
                return 'false';
            }
            
            var serviceGR = new GlideRecord('cmdb_ci_service');
            if (!serviceGR.get(businessServiceId)) {
                gs.debug('Business Service not found: ' + businessServiceId);
                return 'false';
            }
            
            // Check if service is eligible for data patch
            if (serviceGR.justification == 'eligiblefordataPatchCR') {
                gs.debug('Service is eligible for Data Patch CR: ' + businessServiceId);
                return 'true';
            }
            
            gs.debug('Service is not eligible for Data Patch CR: ' + businessServiceId);
            return 'false';
            
        } catch (ex) {
            gs.error('Error in validateDataPatchEligibility: ' + ex);
            return 'false';
        }
    },
    
    type: 'ValidateChangeRequest'
});