Need support with script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 11:12 AM
Hello All,
I need some support with the script.
I am planning to create a new catalog item "Generate AST Tags". it will have 2 variables
1. Count of AST Tags (Integer) - This will be editable and used by requester to update the integer count of tags.
2. Output Tags (String) - This will be readonly and will just display the results once the request is submitted.
Upon form submission, I want a logic in place to generate the AST tags.
Logic: Let's assume requester provided count of AST tags as 50.
The script should generate first tag as Julian number of today's date (24255) and append 0001.
then script should query the alm_hardware table to make sure there is no existing asset with this tag number.
so first tag would be 242550001 and then a loop will run 50 times and increment the tag by 1.
242550002
242550003
242550004.....
242550050
All these tags will then be posted to Output Tags (String) and RITM will be closed completed.
Appreciate your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2024 05:06 AM
After Insert BR of RITM
(function executeRule(current, previous /*null when async*/) {
// Check if the catalog item is the one we are interested in
if (current.cat_item.name == 'Generate AST Tags') {
var count = current.variables.count_of_ast_tags; // Get the count from the variable
var tags = [];
// Generate Julian date for today
var julianDate = new GlideDateTime().getJulian();
// Loop to generate tags
for (var i = 1; i <= count; i++) {
var tag = julianDate + String('0000' + i).slice(-4); // Format tag number
// Check if the tag already exists in alm_hardware
var gr = new GlideRecord('alm_hardware');
gr.addQuery('tag_number', tag); // Assuming tag_number is the field where the tag is stored
gr.query();
if (!gr.hasNext()) {
tags.push(tag);
}
}
// Update the Output Tags variable
current.variables.output_tags = tags.join('\n');
// Complete the RITM
current.state = 'complete'; // Assuming 'complete' is the state value for completion
current.update(); // Save the RITM with updated output tags
}
})(current, previous);