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

@PriyaRaj 

To do that, we need to shake things up a little. Instead of returning a True / False statement from our Script Include function, we should return the error or info message we wish to show instead.

Script Include

var CMDBClientUtils = Class.create();
CMDBClientUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	checkForSerialNumber: function () {
		try {
			var serialNumber = this.getParameter('sysparm_serialnumber');
			var grHardware = new GlideRecord('cmdb_ci_pc_hardware');
			grHardware.addQuery('serial_number', serialNumber);
			grHardware.setLimit(1);
			grHardware.query();
			// We are just going to return a simple error message if we do / dont find the 
			if (grHardware.next()) {
				return 'Serial Number already exists, found in asset: ' + grHardware.getValue('name');
			}
		} catch (ex) {
			gs.error(ex);
		}
	},

    type: 'CMDBClientUtils'
});

Client Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	// GlideAjax, if you're not familiar with the concepts I suggest spending some time looking it up, its super useful.
    var ga = new GlideAjax('global.CMDBClientUtils');
    ga.addParam('sysparm_name', 'checkForSerialNumber');
    ga.addParam('sysparm_serialnumber', g_form.getValue("serial_number"));
    ga.getXML(checkSerialNumber);
}

function checkSerialNumber(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer) {
        g_form.addErrorMessage(answer);
        g_form.clearValue('serial_number');
    } 
}

The modifications to the code mean that when we DO find Serial Number, we return the Error message we want to the Client Script, but the Error message is formulated in the Script Include (Server Side) rather than the Client Script (Client Side). There is a reason we do this, but I won't cover it here.

The error message I have put in the script include is just an example, you will need to modify it to show and say exactly what you want it to.

Please mark the answer as correct if this has solved your issue, or upvote the answer if it was helpful.

@Ethan Davies 

Doing so will clear the serial number value even if it is auto populated from the hostname.

In her question, she mentioned that serial number will be auto populated after hostname is filled.

So with the above script, the auto populated value gets cleared because it is already present in table.

🙂

Hi @SUMANTH DOSAPATI , you are right. It is clearing out the value that is autopopulated and it is saving the manually filled value after the error when saved.

@PriyaRaj  Yes.

So before I give you new script, answer this:

Is the hostname field a reference field or string field?

If reference field then it is referring to which table?

 

Regards,
Sumanth

@SUMANTH DOSAPATI , Hostname is a string field in catalog task variables. We are checking the value from 'cmdb_ci_pc_hardware'.

There could be 3 scenerios:

1. Hostname is in table and serial number autopopulates, which is existing functionality.

> It should save the form without error.

2. Hostname is not in table and serial number match with table.

> It should display error msg with hostname of that asset and not allow user to save the form.

3. Hostname is not in table and serial number is not in table.

> It should save form without error.