Fetching serial number from hostname of asset on catalog tasks.

Anshika2
Kilo Expert

Hi I am having a requirement. I am having one catalog item to order virtual machines. It contains two variables : 'hostname' and 'serial number'. These two variables are not visible in form. They are visible on RITM and sc_task.

When the task is created for SD team then they fill this hostname variable. When they are filling the variable then the serial number should get auto-populated for that hostname.

Table from which virtual machines are :

cmdb_ci_pc_hardware

Fields on table : name > contains hostname of asset

                             serial_number > contains serial number of asset.

 

Can you help me achieving this. Thank you in advance!!!

1 ACCEPTED SOLUTION

Anshika2
Kilo Expert

Hi, Thank you for the responses.

My issue is resolved. It is working fine for me now. Posting the script that I have written.

Script Include :

var Populate_serial_number = Class.create();
Populate_serial_number.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getID: function() {
        
        var asset = this.getParameter('sysparm_host');
        if(asset != ''){
            var gr = new GlideRecord('cmdb_ci_pc_hardware');
            gr.addQuery('name',asset);
            gr.query();
            if(gr.next()){
                return gr.serial_number;
            }
        }
    
    type: 'Populate_serial_number'
    }
});

 

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //    var host = g_form.getValue('hostname');
    
    var number = g_form.getValue('serial_number');

    var ga = new GlideAjax('Populate_serial_number');
    ga.addParam('sysparm_name', 'getID');
    ga.addParam('sysparm_host', newValue);
    ga.getXML(getResponse);
    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
    
        g_form.setValue('serial_number', answer);
    
    }

View solution in original post

4 REPLIES 4

Brian Lancaster
Tera Sage

You can use an onChange Client script that only runs on the request item and catalog task. My variables may not match yours put this code worked in my PDI.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
	g_form.getReference('host_name', callback); //host_name is my variable and is a reference field.
}
function callback(host){
	g_form.setValue('serial_number', host.serial_number); //first serial_numer is my variable and it is of type single line text
}

Hi @Brian Lancaster ,

 

My hostname field is single line text and the table from which we are getting value is different. This is not working for me. 

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

share the script you started with and where are you stuck?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Anshika2
Kilo Expert

Hi, Thank you for the responses.

My issue is resolved. It is working fine for me now. Posting the script that I have written.

Script Include :

var Populate_serial_number = Class.create();
Populate_serial_number.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getID: function() {
        
        var asset = this.getParameter('sysparm_host');
        if(asset != ''){
            var gr = new GlideRecord('cmdb_ci_pc_hardware');
            gr.addQuery('name',asset);
            gr.query();
            if(gr.next()){
                return gr.serial_number;
            }
        }
    
    type: 'Populate_serial_number'
    }
});

 

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //    var host = g_form.getValue('hostname');
    
    var number = g_form.getValue('serial_number');

    var ga = new GlideAjax('Populate_serial_number');
    ga.addParam('sysparm_name', 'getID');
    ga.addParam('sysparm_host', newValue);
    ga.getXML(getResponse);
    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
    
        g_form.setValue('serial_number', answer);
    
    }