- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 10:53 PM
Hi Team,
We have a requirement to set a reference field on load. We have done it through sys id using set value, but our client has suggested to use it via GlideAjax instead of hardcoded "sys_id". Hence, I have written a on load client script and script include as shown below but its not setting the reference field value and throwing an alert "[object HTMLCollection]" when I try to get the "answer" value.
Client Script:
function onLoad() {
var gr = new GlideAjax('x_acal_ahc.AHC_getServiceProvider');
gr.addParam('sysparm_name', 'getProviderName');
gr.addParam('sysparm_serviceProvider', g_form.setValue('service_provider', 'lion'));
gr.getXML(getResponse);
}
function getResponse(response) {
var answer = response.responseXML.documentElement.getElementsByTagName('result');
alert(answer); //getting "[object HTMLCollection]" instead of "lion"
g_form.setValue('service_provider', answer);
}
Script Include:
var AHC_getServiceProvider = Class.create();
AHC_getServiceProvider.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getProviderName: function() {
var result = this.newItem("result");
var service_provider = this.getParameter('sysparm_serviceProvider');
var gr = new GlideRecord("u_zoo"); //zoo table to get value
gr.get(service_provider);
result.setAttribute("providerName", gr.getValue("sys_id")); //trying to return Lion sys_id
},
type: 'AHC_getServiceProvider'
});
Thanks in Advance,
Abhishek
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 11:25 PM
Hi,
why not use default value for this in the variable configuration?
you can store the sys_id of the record in system property as string and then call that in default value
Easier to maintain going forward
Just change the sys_id later on if required
javascript: var sysId = gs.getProperty('propertyName'); sysId;
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 11:42 PM
Can you please share your updated scripts?
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 11:25 PM
Hi,
why not use default value for this in the variable configuration?
you can store the sys_id of the record in system property as string and then call that in default value
Easier to maintain going forward
Just change the sys_id later on if required
javascript: var sysId = gs.getProperty('propertyName'); sysId;
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2022 12:42 AM
Thank you for marking my response as helpful.
If you follow the approach I shared you need not create client script and Script include and it would be easier to maintain and as per best practice.
If my response helped you please mark it correct to close the question so that it benefits future readers as well.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2022 12:47 AM
Sure Ankur 🙂