How to Auto populate multiple values on Multi line text variable based on selection on other field

JRY
Mega Guru

Hello Team,

I'm creating new catalog item where i'm creating a variable's 

  1. Application
  2. configuration item
  3. used for environment

find_real_file.png

Here I'm trying to populate values 'used for environment' variable, based on selection on configuration item, I have selected multiple values on configuration item list collector field which is reference to table cmdb_ci_server her I'm trying to auto populate 'used for environment'  values from server table  on  'used for environment' variable, but it's not populating multiple values it's showing only one value which i is first one other two are not populating. how to populate multiple values.

 

Thanks

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

First create an onChange Catalog Client Script on the Configuration item variable.  The script will look something like this, depending on your Script Include and variable names.

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

    var ga = new GlideAjax('CILookup');//name of the Script Include
    ga.addParam('sysparm_name', 'getCIDetails');//name of the function in the Script Include
    ga.addParam('sysparm_cis', newValue);
    ga.getXML(getCI);
    
    function getCI(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('variableName', answer);//name of your text variable
    }
}

Next create a Script Include with the same name used in the GlideAjax line above, ensuring the Client callable box is checked.  Your script include will look like this

var CILookup = Class.create();
CILookup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    getCIDetails: function() {
        var ciArr =[];
        var cis = this.getParameter('sysparm_cis');
        var ci = new GlideRecord('cmdb_ci_server');
        ci.addQuery('sys_id', 'IN', cis);
        ci.query();
        while(ci.next()){
            ciArr.push(ci.used_for.toString());
//if you want the used_for values comma-separated use the line above
//or if you want the used_for values each on one line in the text field
//then use the line below
ciArr.push(ci.used_for.toString() + '\n');
        }
        
        return ciArr.join(',');
 },

type: 'CILookup'
});

 

View solution in original post

4 REPLIES 4

Bhagyashri Sort
Kilo Guru

Hi,

 Refer below link.

https://community.servicenow.com/community?id=community_question&sys_id=c6bd476ddb9cdbc01dcaf3231f96...

 

Mark it correct and helpful.

Thanks

Bhagyashri Sorte.

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi,

here the solution to use, please try to create a new onchange client script:

https://community.servicenow.com/community?id=community_question&sys_id=60c2dba1db101fc01dcaf3231f96...

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

Thank you very much

Cheers
Alberto

Yash Agrawal1
Tera Guru

Hello JRY,

Write an on change client script on Configuration item variable,

var gajax=new GlideAjax('abc')//name of script include

gajax.addParam('sysparm','xyz');//function name

gajax.addParam('sysparm_condig',newValue);//newValue contain sys_id of all selected ci;

gajax.getXML(pqr);

 

function pqr(response)

{

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

answer.setValue('field name',answer);//field name of  used for field.

}

----------------------------------------------------------------

Script include

function:xyz()

{

   var arr=[];

   var config_values=this.getParameter('sysparm_condig');

  arr=config_values.split(',').toString();

  var i;

  var gr= new GlideRecord('server table name');

  for(i=0;i<arr.length;i++)

{

    gr.addQuery('ci',arr[i]);//put correct value name instead of CI. here for every ci we are geeting used for

    gr.addQuery();

    if(gr.next())

{

  gr.getValue(used_for+'\n');

}

}

return arr.toString();

}

 

Please Mark it helpful/correct if my answer helps in any way to resolve your query.
Reach out to me if any more help required.

Regards

Yash.K.Agrawal

 

Brad Bowman
Kilo Patron
Kilo Patron

First create an onChange Catalog Client Script on the Configuration item variable.  The script will look something like this, depending on your Script Include and variable names.

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

    var ga = new GlideAjax('CILookup');//name of the Script Include
    ga.addParam('sysparm_name', 'getCIDetails');//name of the function in the Script Include
    ga.addParam('sysparm_cis', newValue);
    ga.getXML(getCI);
    
    function getCI(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('variableName', answer);//name of your text variable
    }
}

Next create a Script Include with the same name used in the GlideAjax line above, ensuring the Client callable box is checked.  Your script include will look like this

var CILookup = Class.create();
CILookup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    getCIDetails: function() {
        var ciArr =[];
        var cis = this.getParameter('sysparm_cis');
        var ci = new GlideRecord('cmdb_ci_server');
        ci.addQuery('sys_id', 'IN', cis);
        ci.query();
        while(ci.next()){
            ciArr.push(ci.used_for.toString());
//if you want the used_for values comma-separated use the line above
//or if you want the used_for values each on one line in the text field
//then use the line below
ciArr.push(ci.used_for.toString() + '\n');
        }
        
        return ciArr.join(',');
 },

type: 'CILookup'
});