Populate variable dynamically servicenow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2024 05:51 PM
Hello developers, can you please help me on how to populate lookup select box/select box dynamically from the answer i get from script include .Any help is appreciated. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2024 06:50 PM - edited 01-26-2024 06:51 PM
@servicenow14710 : Could you share the below configurations that you used to answer better.
1. What is the lookup value field that you used in your variable?
2. What is the value that you are returning from your script include? (Ex: Object, String etc..)
In general, we will use,
g_form.setValue('YOUR_VARIABLE_NAME', 'VALUE_RETURNED_FROM_SCRIPT_INCLUDE');
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2024 05:51 AM
Hello @Sainath N : i tried the code
g_form.setValue('YOUR_VARIABLE_NAME', 'VALUE_RETURNED_FROM_SCRIPT_INCLUDE');
this returns the sys ids , i need to return the table columns is it possible. Similar to how we give reference qualifier to table but dynamically. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2024 07:41 PM
You should look at the response received from the Client Callable Script Include.
If you are expecting a single sys_id in response that's fine, but if it is multiple sys_ids, they should be comma seperated. In either case you can use g_form.setValue('field_name', response);
For example, for the following client callable script include, it queries a table and returns the sys_ids of all the records that were fetched as a comma seperated string.
var SampleUtils = Class.create();
SampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getSomeResponse: function(){
var tblGr = new GlideRecord('Table_name');
tblGr.addEncodedQuery('Some_Query');
tblGr.query();
var response = []; //An array to store all sys_ids from the query
while(tblGr._next()){
response.push(tblGr.getUniqueValue()); //Insert the sys_id into the array
}
return response.join(','); //Return the array as comma seperated string
},
type: 'SampleUtils'
});
The client script should be like this,
var ga = new GlideAjax('SampleUtils');
ga.addParam('sysparm_name', 'getSomeResponse');
ga.getXMLAnswer(processResponse);
function processResponse(answer){
if(answer){
alert(answer); // This is for debugging purpose, remove this later
g_form.setValue('field_name', answer);
} else {
alert('Invalid response'); // You can also remove this
}
Please mark my answer helpful and accept as a solution if it helped 👍✅
Anvesh