Auto Populate a list collector field

rachelconstanti
Mega Sage

I currently have a Service Catalog Item that does the following:

Catalog Item background

Host Name is a reference variable
System Owner is a reference variable

The System Owner auto populates based on the host name.

Workflow

The System Owner needs to approve the request.

This is all working however I have been asked if I can set this up so that multiple host names can be selected which will in turn auto populate the system owner and then send approvals.

I changed both variables to list collector but once the second host name is entered, it clears out the system owner.

Is this possible?
If not list collector, is there another field type or another recommendation for a solution?

7 REPLIES 7

Mohith Devatte
Tera Sage
Tera Sage

Hello,

if you have changed the type of the variable you need to change the script which is populating the system owner based on the host name .

It should be an on change client script .

can  you share the script so that we can change the format of assigning values for auto population?

This is the onChange client script to auto populate the system owner based on the host name:

 

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

    //getReference for System Owner, then callback and populate
    // autoPopulate is our callback function
    g_form.getReference('host_name', autoPopulate);

}

function autoPopulate(sysOwner) {
    //set values after getting reference
    g_form.setValue('sys_owner', sysOwner.getValue('owned_by'));
    

}

Hello @rachelconstantino 

Please try the below script and steps

1)Create a script include and make sure you check client callable check box

2)Declare a function inside it named as getOwners();

3) Call the script include that you created like below in the onchange client script

 

Client script: // your client script should be on host field .Select field a host and type as on change client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var aj= new GlideAjax("your script include name"); //give your client callable script include name
aj.addParam('sysparm_name', 'getOwners');
aj.addParam('sysparm_hosts', newValue); 
aj.getXML(callback);

}

function callback(response) {
var values = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue("owner_field_bakend_name",values); //give your owners field name here
}

Script include:

getOwners: function() {

var ownersArr = [];

var gr = new GlideRecord("your_Table_backend_name"); //replace your table name
gr.addEncodedQuery('sys_idIN'+this.getParameter('sysparm_hosts'));
gr.query();
while(gr.next()) {
ownersArr.push(gr.owner_field_bakend_name.toString()); //Replace owner field name here
}

return ownersArr.toString();
},

Please mark the answer correct if it helped you

 

SumanthDosapati
Mega Sage
Mega Sage

Hi @rachelconstantino 

You may need to write an onchange client script and a client callable script include as below.

OnChange client script on host field

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

if(newValue == ''){

g_form.clearValue('owner_field_name'); //give your owners field name here

}

var ga = new GlideAjax("ScriptInclude"); //give your client callable script include name
ga.addParam('sysparm_name', 'populateOwners');
ga.addParam('sysparm_hosts', newValue); 
ga.getXML(callback);

}

function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue("owners_field_name",answer); //give your owners field name here
}

 

Client callable script include :

populateOwners: function() {

var arr = [];

var gr = new GlideRecord("table_name"); //update table name 
gr.addQuery('sys_id', 'IN' ,this.getParameter('sysparm_hosts'));
gr.query();
while(gr.next()) {
arr.push(gr.owner_field_name.toString()); //give your owner field name here
}

return arr.join(',');
},

 

Mark as correct and helpful if it solved your query.

Regards,

Sumanth