- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2024 03:34 AM - edited 03-01-2024 03:46 AM
Hi All,
I have tried to write a very simple client script and script include which will simply show a message when the priority value changes on an incident record. This is just for testing purposes.
Could anyone point out what I have got wrong here as the alert always provides an answer of null and the log in the script include is not seen.
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Check if the updated field is priority
// Call the server-side function to handle priority update
var ga = new GlideAjax('IncidentScriptInclude');
ga.addParam('sysparm_name', 'priorityUpdated');
ga.getXMLAnswer(priorityanswer);
}
function priorityanswer(answer) {
alert('Message from server: ' + answer);
}
Script include:
var IncidentScriptInclude = Class.create();
IncidentScriptInclude.prototype = {
initialize: function() {
},
// Function to be called when priority field is updated
priorityUpdated: function() {
gs.info('Priority field updated on incident record');
return "success";
},
type: 'IncidentScriptInclude'
};
Any help would be greatly appreciated
Alex
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2024 03:59 AM
this will fix it
1) your script include is marked client callable but the syntax it has is for server side i.e. it has initialize function
2) update as this -> remove initialize()
var IncidentScriptInclude = Class.create();
IncidentScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
priorityUpdated: function() {
gs.info('Priority field updated on incident record');
return "success";
},
type: 'IncidentScriptInclude'
});
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2024 03:55 AM - edited 03-01-2024 03:57 AM
Your script include is not client callable, make it client callable
There is a check box on the script include page
And the Script should look like this
var IncidentScriptInclude = Class.create();
IncidentScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
priorityUpdated: function() {
gs.info('Priority field updated on incident record');
return "success";
},
type: 'IncidentScriptInclude'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2024 04:08 AM
@Alex319 Was something missing in my response?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2024 03:57 AM
Your client script and script include code seems mostly correct in structure, but there are a few key points and potential issues that might be causing the problem of receiving a null response and not seeing the log from the script include:
1. Client Script getXMLAnswer Callback
You are correctly using getXMLAnswer to asynchronously fetch the answer from your Script Include. However, make sure the callback function priorityanswer is defined correctly and is accessible in the scope where getXMLAnswer is called. Your current setup seems correct, but double-check for any typos or scope issues.
2. Script Include Access and Security
Ensure your Script Include is set to be accessible from client scripts. In the ServiceNow instance, the Script Include should have the "Client Callable" box checked. If this is not set, the client script cannot invoke methods from the Script Include.
3. Logging Visibility
The gs.info logging statement in your Script Include will log messages to the system log. Ensure you are checking the correct log (System Logs > Transactions) in your ServiceNow instance. Also, verify that your user account has the necessary permissions to view these logs.
4. Correct Use of GlideAjax Parameters
Your use of GlideAjax and the parameters seems correct. Make sure the name of the Script Include ('IncidentScriptInclude') and the method name ('priorityUpdated') exactly match what's defined in your code, considering case sensitivity.
If you've checked all the above and the issue persists, consider adding more debug logs both on the client and server sides to trace the execution flow. You can also use the browser's developer tools to inspect network requests and responses to ensure the GlideAjax call is made correctly and to see the response from the server which might give you more insights into where the issue lies.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2024 03:59 AM
this will fix it
1) your script include is marked client callable but the syntax it has is for server side i.e. it has initialize function
2) update as this -> remove initialize()
var IncidentScriptInclude = Class.create();
IncidentScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
priorityUpdated: function() {
gs.info('Priority field updated on incident record');
return "success";
},
type: 'IncidentScriptInclude'
});
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2024 04:04 AM
Thanks Ankur,
Appreciate the spot there
Alex