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

Murthy Ch
Giga Sage

Hi @sailor_moon 

Which value you need to populate in the computer name field?

If it is some specific value to be auto populated you can set the value in  default value field:

MurthyCh_0-1668483627835.png

 

 

Thanks,
Murthy

I need to auto populate this field based on the requested_for user that is selected, and the value that needs to come out is the asset tagsailor_moon_1-1668483851999.png

sailor_moon_0-1668483761432.png

 

 

Hi @sailor_moon 

Then you can try like below there is no client script required.

I hope you have configured variable something like below:

MurthyCh_0-1668484349931.png

If yes you can use below logic in default value field:

MurthyCh_1-1668484380519.png

 

javascript: var dt; var gr=new GlideRecord("cmdb_ci_computer"); gr.addQuery("assigned_to",gs.getUserID()); gr.query(); if(gr.next()) { dt=gr.asset_tag} dt;

//in place of gs.getUserID() just replace with current.variables.requestedFor_VariableName;

 

Hope it helps:)

 

 

 

Thanks,
Murthy

Hi @sailor_moon 

If you want to happen this during onload of the form you can use above suggestion but if we want to on change of requested for then you have write client script and script include as well.

 

Thanks,
Murthy

Yup I need it onchange