Auto Populate a list collector field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2022 07:27 AM
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?
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2022 07:45 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2022 11:45 AM
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'));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2022 12:38 AM
Hello
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2022 08:39 AM
Hi
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