Auto populate the computer name field

sailor_moon
Tera Contributor

I'm creating a catalog item wherein I need to auto populate the computer name field and the type of my computer name field is lookup field. I'm having a hard time with the catalog client script and script include. Can someone help me with this since I tried to research but it's not working in my side.

1 ACCEPTED SOLUTION

SP22
Mega Sage
Mega Sage

Hi,
you can modify the code as per your requirement and please let me know whether it is useful or not.

 

script include

var PopulateWorkstationName = Class.create();
PopulateWorkstationName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getComputer: function() {
        var computer = '';
        var uid = this.getParameter('sysparm_usid');
        var com = new GlideRecord('cmdb_ci_computer');
        com.addQuery('assigned_to', uid);
        com.query();
        if (com.next()) {
            computer = com.sys_id;
        }
        return computer;
    },
    type: 'PopulateWorkstationName'
});

On-load Client script

function onLoad() {
    //Type appropriate comment here, and begin script below
    var user = g_form.getValue('caller_id');
    var ga = new GlideAjax('PopulateWorkstationName');
    ga.addParam("sysparm_name", "getComputer");
    ga.addParam("sysparm_usid", user);
    ga.getXML(getResponse);
}

function getResponse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.setValue('u_user_computer', answer); //you can mention your field name.
}

 

Thanks
SP.

View solution in original post

11 REPLIES 11

SP22
Mega Sage
Mega Sage

Hi,
you can modify the code as per your requirement and please let me know whether it is useful or not.

 

script include

var PopulateWorkstationName = Class.create();
PopulateWorkstationName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getComputer: function() {
        var computer = '';
        var uid = this.getParameter('sysparm_usid');
        var com = new GlideRecord('cmdb_ci_computer');
        com.addQuery('assigned_to', uid);
        com.query();
        if (com.next()) {
            computer = com.sys_id;
        }
        return computer;
    },
    type: 'PopulateWorkstationName'
});

On-load Client script

function onLoad() {
    //Type appropriate comment here, and begin script below
    var user = g_form.getValue('caller_id');
    var ga = new GlideAjax('PopulateWorkstationName');
    ga.addParam("sysparm_name", "getComputer");
    ga.addParam("sysparm_usid", user);
    ga.getXML(getResponse);
}

function getResponse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.setValue('u_user_computer', answer); //you can mention your field name.
}

 

Thanks
SP.

I have provided above code. please verify it and let me know @sailor_moon 

sailor_moon
Tera Contributor

Hi @SP22 I tried to modify your code but when I change my g_form.setValue(computer_name, answer)

This is what I got 

sailor_moon_0-1668485695022.png

 

Hi @sailor_moon ,
can you please send me the full screenshot.
you need to go to type specification add reference table as cmdb_ci_computer (refer screenshot of @Murthy Ch )