Prevent the user to fill serial number that is already present in pc_hardware table for another asset.

PriyaRaj
Tera Contributor

Hi All,

I am having a requirement on catalog task form. On the form there is two editable field hostname and serial number. Serial Number auto populates when hostname is filled but if the hostname is of a new asset which is not present in pc_hardware table then the user needs to manually enter the serial number.

In manual case, we need to validate if the serial number is already present in pc_hardware table or not. If yes, we need to show the error msg with the name of asset to which the serial number is assigned and prevent saving the form.

find_real_file.png

As I am new and learning, kindly help me with the script to validate serial number and display error message.

1 ACCEPTED SOLUTION

SumanthDosapati
Mega Sage
Mega Sage

Can you try below scripts :

 Yup! You can update scripts as below

Client script : 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

        return;

    }

    var ga = new GlideAjax('global.CMDBClientUtils'); //update script include name as needed

    ga.addParam('sysparm_name', 'checkForSerialNumber');

    ga.addParam('sysparm_serialnumber', g_form.getValue("serial_number"));

    ga.addParam('sysparm_hostname', g_form.getValue('host_name')); //check the backend value of hostname variable

    ga.getXML(checkSerialNumber);

}




function checkSerialNumber(response) {

    var answer = response.responseXML.documentElement.getAttribute("answer");

    if (answer != 'no') {

        g_form.clearValue('serial_number');

        g_form.addErrorMessage(answer);

    }

}

 

Script Include

var CMDBClientUtils = Class.create();

CMDBClientUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

       

        checkForSerialNumber: function () {

               try {

                var answer;

                       var serialNumber = this.getParameter('sysparm_serialnumber');

                       var hostName = this.getParameter('sysparm_hostname');

                       var grHardware = new GlideRecord('cmdb_ci_pc_hardware');

                       grHardware.addQuery('serial_number', serialNumber);

                grHardware.addQuery('name', '!=', hostName);

                       grHardware.query();

                       if (grHardware.next()) {

                                answer = 'Serial Number already exists, found in asset: ' + grHardware.getValue('name');

                       }

                else {

                       answer = 'no';

                }

                return answer;

               } catch (ex) {

                       gs.error(ex);

               }

        },




    type: 'CMDBClientUtils'

});

If the above is not working, just share the screenshot of your form which shows the hostname field and serial number field.

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

View solution in original post

18 REPLIES 18

SumanthDosapati
Mega Sage
Mega Sage

You already posted the same question and now this is a duplicate question.

https://community.servicenow.com/community?id=community_question&sys_id=dd99a2ef1b3c9910a59033f2cd4b...

 

You can check for answer already available there and reply back if you have any query.

Regards,
Sumanth

Hi @SUMANTH DOSAPATI , I created that but it is not working for me.

If its not working you might have asked there itself.

Okay. Can you share your scripts of what you tried.

 

Regards,

Sumanth

LinkedIn

SumanthDosapati
Mega Sage
Mega Sage

Can you try below scripts :

 Yup! You can update scripts as below

Client script : 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

        return;

    }

    var ga = new GlideAjax('global.CMDBClientUtils'); //update script include name as needed

    ga.addParam('sysparm_name', 'checkForSerialNumber');

    ga.addParam('sysparm_serialnumber', g_form.getValue("serial_number"));

    ga.addParam('sysparm_hostname', g_form.getValue('host_name')); //check the backend value of hostname variable

    ga.getXML(checkSerialNumber);

}




function checkSerialNumber(response) {

    var answer = response.responseXML.documentElement.getAttribute("answer");

    if (answer != 'no') {

        g_form.clearValue('serial_number');

        g_form.addErrorMessage(answer);

    }

}

 

Script Include

var CMDBClientUtils = Class.create();

CMDBClientUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

       

        checkForSerialNumber: function () {

               try {

                var answer;

                       var serialNumber = this.getParameter('sysparm_serialnumber');

                       var hostName = this.getParameter('sysparm_hostname');

                       var grHardware = new GlideRecord('cmdb_ci_pc_hardware');

                       grHardware.addQuery('serial_number', serialNumber);

                grHardware.addQuery('name', '!=', hostName);

                       grHardware.query();

                       if (grHardware.next()) {

                                answer = 'Serial Number already exists, found in asset: ' + grHardware.getValue('name');

                       }

                else {

                       answer = 'no';

                }

                return answer;

               } catch (ex) {

                       gs.error(ex);

               }

        },




    type: 'CMDBClientUtils'

});

If the above is not working, just share the screenshot of your form which shows the hostname field and serial number field.

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth