Scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-06-2023 02:05 AM
How to add new value to indicator through client script on specific interval of time ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-06-2023 02:13 AM
Hello @Sunshine_aashi_ ,
To add a new value to an indicator through client script on a specific interval of time in ServiceNow, you can follow these general steps:
1. Create a Client Script:
- Navigate to System Definition > Client Scripts.
- Click on "New" to create a new client script.
2. Set the Script Type:
- Choose the appropriate script type (e.g., onChange, onLoad, etc.) based on when you want this script to trigger.
3. Write the Script:
- In the script field, you'll need to write JavaScript code that accomplishes your goal. To add a new value to an indicator, you may need to use GlideAjax or GlideRecord to interact with the indicator record.
4. Use JavaScript Timing Functions:
- To trigger the script at specific intervals, you can use JavaScript timing functions like setInterval() or setTimeout().
- For example, if you want to run the script every 5 minutes, you would use setInterval().
Here's a simplified example using setInterval():
function updateIndicatorValue() {
// Add code to update the indicator value here using GlideAjax or GlideRecord.
// Example:
// var gr = new GlideRecord('indicator_table');
// gr.addQuery('indicator_field', 'value_to_match');
// gr.query();
// if (gr.next()) {
// gr.indicator_field = 'new_value';
// gr.update();
// }
}
// Run the function every 5 minutes (300000 milliseconds)
setInterval(updateIndicatorValue, 300000);
Remember to replace 'indicator_table', 'indicator_field', 'value_to_match', and 'new_value' with your actual table, fields, and values.
5. Test and Debug:
- Test the script in a controlled environment and debug any issues that arise.
6. Save and Activate:
- Save the client script and ensure it's activated.
7. Monitor and Maintain:
- Keep an eye on the script's performance and make adjustments if necessary.
Please note that this is a general guide and specifics might vary depending on your exact use case and ServiceNow instance setup. Always consult ServiceNow documentation and consider best practices when implementing custom scripts.
Mark helpful or correct if applicable.
Thanks & Regards,
Sayali Gurav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-06-2023 03:39 AM
HI @Sunshine_aashi_ ,
I trust you are doing great.
Please find below code for the usecase
1. Client script with GlideAjax
// This script will run every 5 minutes (300000 milliseconds)
setInterval(function() {
// Use GlideAjax to call a Script Include
var ga = new GlideAjax('MyScriptInclude');
ga.addParam('sysparm_name', 'addIndicatorValue');
ga.getXMLAnswer(function(response) {
// Handle the response if needed
console.log(response);
});
}, 300000);
2. Script Include
var MyScriptInclude = Class.create();
MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
addIndicatorValue: function() {
// Your logic to add a new value to the indicator goes here
// For example:
var gr = new GlideRecord('indicator_table_name'); // Replace 'indicator_table_name' with your table name
gr.newRecord();
gr.value = 'Your Value'; // Replace 'Your Value' with the value you want to add
gr.insert();
return 'Value added successfully';
},
type: 'MyScriptInclude'
});
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi