Auto populating 'Configuration item' field based on 'caller_id' on incident form.

Naga Ravindra R
Kilo Sage

Requirement: need to auto populate the 'configuration item' variable based on 'caller_id' variable.

What are the 'CI's' related to him and need to populate only if caller is VIP.

I know it can be done by writing a script include and onchnage client script. but I'm a admin need some assistance with script.

 

Thanks,

1 ACCEPTED SOLUTION

Suseela Peddise
Kilo Sage

Hi,

To get CIs owned by caller then use below scripts. If you want to get CIs assigned to caller then change the query.

Script Include :

var userOwnCI= Class.create();
userOwnCI.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getComputer: function(){
var caller= this.getParameter('sysparm_caller');
//query to computer table and get the list of computers owned by caller
var gr= new GlideRecord('cmdb_ci_computer'); //change table name as the requirements
gr.addQuery('owned_by',caller); //owned CI.

//For assigned assets change above line to gr.addQuery('assigned_to',caller);

gr.query();
if(gr.next())
{
return gr.getUniqueValue();
}
},

type: 'userOwnCI'
});

 

Client Script:


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

//check VIP user

var vipUser='false';
var caller = g_form.getReference('caller_id', doAlert); // doAlert is our callback function 
function doAlert(caller) { //reference is passed into callback as first arguments
if (caller.vip == 'true')
vipUser= 'true';

if (newValue && vipUser =='true') { //if VIP user
var ga= new GlideAjax('userOwnCI');
ga.addParam('sysparm_name','getComputer');
ga.addParam('sysparm_caller', newValue);
ga.getXML(getComputerDet);
}

}

}

function getComputerDet(response)
{

var answer = response.responseXML.documentElement.getAttribute('answer');

//Sets the field value
g_form.setValue('cmdb_ci',answer);
}

 

If I have answered your question, please mark my response as correct and/or helpful.

Thanks,

Suseela P.

 

View solution in original post

4 REPLIES 4

Suseela Peddise
Kilo Sage

Hi,

To get CIs owned by caller then use below scripts. If you want to get CIs assigned to caller then change the query.

Script Include :

var userOwnCI= Class.create();
userOwnCI.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getComputer: function(){
var caller= this.getParameter('sysparm_caller');
//query to computer table and get the list of computers owned by caller
var gr= new GlideRecord('cmdb_ci_computer'); //change table name as the requirements
gr.addQuery('owned_by',caller); //owned CI.

//For assigned assets change above line to gr.addQuery('assigned_to',caller);

gr.query();
if(gr.next())
{
return gr.getUniqueValue();
}
},

type: 'userOwnCI'
});

 

Client Script:


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

//check VIP user

var vipUser='false';
var caller = g_form.getReference('caller_id', doAlert); // doAlert is our callback function 
function doAlert(caller) { //reference is passed into callback as first arguments
if (caller.vip == 'true')
vipUser= 'true';

if (newValue && vipUser =='true') { //if VIP user
var ga= new GlideAjax('userOwnCI');
ga.addParam('sysparm_name','getComputer');
ga.addParam('sysparm_caller', newValue);
ga.getXML(getComputerDet);
}

}

}

function getComputerDet(response)
{

var answer = response.responseXML.documentElement.getAttribute('answer');

//Sets the field value
g_form.setValue('cmdb_ci',answer);
}

 

If I have answered your question, please mark my response as correct and/or helpful.

Thanks,

Suseela P.

 

Thank you so much @Suseela Peddisetty 

Hi!  I need to do this for every user, not VIPs, and based on Assigned to instead of owner. What needs to change in the Client Script version?

What would be the script for any user, not just VIP?  New to Servicenow and scripts. Thanks!