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

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

 

 

@SUMANTH DOSAPATI , It is still creating the same issue. In the autopopulate case, it is showing error and clearing out the value.

In the case of manually filling serial number and hostname does not match, it is not clearing out the value and saving the form after showing error msg.

can you share screenshot of the form where those two fields are present.

Or else drop me a mail at sumanthdosapati3@gmail.com so that i can connect and see.

 

Regards,
Sumanth

@SUMANTH DOSAPATI  please check your email.

Hi @Ethan Davies , It is also showing the error msg when the serial number is autopopulated by the hostname that is present in table. For new hostname only, I wanted to show that error msg. But it showing in the other case too and clearing out the value.