Select the Devices into list collector based on Requested For field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2024 12:25 PM
Hi Everyone,
Requiremnt: I have created the catalog item. Once the "Requested For" is selected, the "Assigned Asset" of this "Requested For" will be auto-populated in the "Test" field.
I have written the Below client callable script include and On-change Catalog Client script. However, it is not working as expected. Could you please help me on this?
Script Include:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2024 12:40 PM
What kind of variable is "test"?
I'm assuming the Catalog Client Script is running on change of the "requested_for" variable. Is that correct?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2024 12:44 PM
Hi @Jim Coyne , Thanks for your quick response
“Test” field type is “list collector”.
yes, it’s running on change catalog client script for requested for field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2024 12:58 AM
Oh, I see it in the title of the post now, sorry about that. And sorry for the delay in responding. Did you ever figure it out?
Here's an updated Script Include:
var U_GetAssetDetails = Class.create();
U_GetAssetDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getListOfAssetSysIdsForUser: function() { //more specific function namethat details the return value
var user = this.getParameter("sysparm_user"); //more generic parameter name for use by other scripts
var assetList = [];
if (user) {
var gr = new GlideRecord("alm_asset");
gr.addQuery("assigned_to", user);
//probably should add more filters here
gr.query();
while (gr.next()) {
assetList.push(gr.getUniqueValue());
}
}
//convert to JSON and return the data
return JSON.stringify(assetList); //use the new "stringify" method instead
},
type: 'U_GetAssetDetails'
});
It looks like you've created the Script Include in the Global scope. If so, you need to be careful of the name so that ServiceNow does create a Class with the same name in a new release. It is not a problem if it is in a custom Scoped App as it is yours and you control what is in it. I don't like the "U_" prefix, but it protects your class somewhat, although not a guarantee. You could use you client code as a prefix instead.
You might want to have one or a few Script Includes that contain your functions otherwise you'll end up with a lot of them. Group the functions into Script Includes by what they do or tables they look at.
And I've changed the name of the function to make it clear as to what it will return. And take a look at the comments I've added.
Updated Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
//remove this section if the Requested for Variable is auto-populated
if (isLoading) {
return;
}
//clear the Asset Variable when the Requested for field is changed
g_form.setValue("test", "");
//get the list of assets if a user was selected
if (newValue) {
g_form.showFieldMsg("test", "Loading list of assets..."); //let them know assets are being searched
var ga = new GlideAjax("U_GetAssetDetails");
ga.addParam("sysparm_name", "getListOfAssetSysIdsForUser");
ga.addParam("sysparm_user", newValue);
ga.getXMLAnswer(function(response) {
//change the response into a comma-separated string
//which the List Collector uses
var result = JSON.parse(response).join(",");
if (result) {
g_form.setValue("test", result); //will clear the field message automatically
} else {
g_form.hideFieldMsg("test", true);
}
});
}
}
Again, take a look at the comments within the code. This will work in a Service Portal, but not the regular UI: needs a bit of tweaking the change the contents of the List Collector