How to validate a variable in a form on the employee screen?

LinhN
Tera Contributor

Hi Servicenow community. I am a newbie to Servicenow. Now I am having a task with this requirement: A User registers a consumable asset.

- If they register for the first time, system allows them to submit the request. 

- If they register for the second time and more times, system will require user to fill in the reason field ( variable) why they want to register for more. 

I want to create a client script with Glide Ajax to validate the reason field ( variable) and Script include to query if this user had registered this asset. How can you help me query to the table Consumable with the user who had registered asset ? Thank you

 

1 ACCEPTED SOLUTION

Satishkumar B
Giga Sage
Giga Sage

Hi @LinhN 

 

Script Include: checks if the user has previously registered the asset.

 

 

var ConsumableUtils = Class.create();
ConsumableUtils.prototype = {
    initialize: function() {},
    
    // Method to check if the user has already registered the asset
    hasRegistered: function(userId, assetId) {
        var gr = new GlideRecord('consumable'); // Replace 'consumable' with your actual table name
        gr.addQuery('user', userId);
        gr.addQuery('asset', assetId); // Replace with actual field names
        gr.query();
        return gr.getRowCount() > 0;
    },

    type: 'ConsumableUtils'
};

 

 

GlideAjax Script: ConsumableAjax calls ConsumableUtils to get the registration status.

 

 

var ConsumableAjax = Class.create();
ConsumableAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    // Method to be called from Client Script
    checkRegistration: function() {
        var userId = this.getParameter('sys_id'); // Parameter for User ID
        var assetId = this.getParameter('asset_id'); // Parameter for Asset ID

        var utils = new ConsumableUtils();
        var registered = utils.hasRegistered(userId, assetId);
        
        return registered ? 'true' : 'false';
    }
});

 

 

Client Script: Validates if a reason is provided for multiple asset registrations and prevents submission if not

 

 

function onSubmit() {
    var assetId = g_form.getValue('asset_id'); // Replace 'asset_id' with your actual variable name
    var userId = g_user.userID; // Get the current user's ID

    var ga = new GlideAjax('ConsumableAjax');
    ga.addParam('sys_id', userId);
    ga.addParam('asset_id', assetId);
    ga.getXMLAnswer(function(response) {
        var registered = response.responseXML.documentElement.getAttribute('answer') === 'true';
        if (registered) {
            var reason = g_form.getValue('reason'); // Replace 'reason' with your actual variable name
            if (!reason) {
                // Show an error message and prevent submission
                g_form.addErrorMessage('Please provide a reason for registering the asset again.');
                g_form.setValid(false);
                return false; // Prevent form submission
            }
        }
        // Allow submission if no issues
        return true;
    });
}

 

 

Ensure field names and table names match your actual configuration, and test thoroughly to verify functionality.

……………………………………………………………………………………………………

Please Mark it helpful 👍and Accept Solution✔️!! If this helps you to understand. 



View solution in original post

4 REPLIES 4

Satishkumar B
Giga Sage
Giga Sage

Hi @LinhN 

 

Script Include: checks if the user has previously registered the asset.

 

 

var ConsumableUtils = Class.create();
ConsumableUtils.prototype = {
    initialize: function() {},
    
    // Method to check if the user has already registered the asset
    hasRegistered: function(userId, assetId) {
        var gr = new GlideRecord('consumable'); // Replace 'consumable' with your actual table name
        gr.addQuery('user', userId);
        gr.addQuery('asset', assetId); // Replace with actual field names
        gr.query();
        return gr.getRowCount() > 0;
    },

    type: 'ConsumableUtils'
};

 

 

GlideAjax Script: ConsumableAjax calls ConsumableUtils to get the registration status.

 

 

var ConsumableAjax = Class.create();
ConsumableAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    // Method to be called from Client Script
    checkRegistration: function() {
        var userId = this.getParameter('sys_id'); // Parameter for User ID
        var assetId = this.getParameter('asset_id'); // Parameter for Asset ID

        var utils = new ConsumableUtils();
        var registered = utils.hasRegistered(userId, assetId);
        
        return registered ? 'true' : 'false';
    }
});

 

 

Client Script: Validates if a reason is provided for multiple asset registrations and prevents submission if not

 

 

function onSubmit() {
    var assetId = g_form.getValue('asset_id'); // Replace 'asset_id' with your actual variable name
    var userId = g_user.userID; // Get the current user's ID

    var ga = new GlideAjax('ConsumableAjax');
    ga.addParam('sys_id', userId);
    ga.addParam('asset_id', assetId);
    ga.getXMLAnswer(function(response) {
        var registered = response.responseXML.documentElement.getAttribute('answer') === 'true';
        if (registered) {
            var reason = g_form.getValue('reason'); // Replace 'reason' with your actual variable name
            if (!reason) {
                // Show an error message and prevent submission
                g_form.addErrorMessage('Please provide a reason for registering the asset again.');
                g_form.setValid(false);
                return false; // Prevent form submission
            }
        }
        // Allow submission if no issues
        return true;
    });
}

 

 

Ensure field names and table names match your actual configuration, and test thoroughly to verify functionality.

……………………………………………………………………………………………………

Please Mark it helpful 👍and Accept Solution✔️!! If this helps you to understand. 



Hello @Satishkumar B ,

 

Your script is awesome. But why don't your create Client Callable Script Include (it eliminates to write another script include (in your case ConsumableAjax). We can call that client Callable Script Include from Client script.

 

Or else is there any concept behind it, if it is can you please explain me for better understanding.

 

Thank you

 

Please mark my answer helpful 👍 & correct if it helps you

Thank you

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

Thank you, but why dont we create the GlideAjax Script in the Script Include together? Meaning that I have to create 2 scripts include, right?

Hi, I don't understand about asset_id. You mean asset_id is the ID of the asset record when creating right? Is it the sys_id of record in the consumable table?