Need help with below script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2024 01:29 PM
Hello All,
I have below script which works great to generate asset tags based on today's Julian date. It also has the logic to check the generate tags in asset table and skip the ones already present.
However there is a potential issue we have identified. It might be a possibility that multiple agents might be using the catalog to generate asset tags. With the current logic it will generate the same asset tags for multiple agents on the same day/same time unless the assets are created with the generated asset tags.
Can we modify the logic to generate unique asset tags per request by checking the RITM's created TODAY and appending a unique number as suffix.
Please advise.
(function execute(inputs, outputs) {
var today = new Date();
var startOfYear = new Date(today.getFullYear(), 0, 1);
var diff = today - startOfYear;
var dayOfYear = Math.floor(diff / (1000 * 60 * 60 * 24)) + 1;
var julianDate = today.getFullYear().toString().slice(2) + dayOfYear.toString().padStart(3, '0');
var baseNumber = parseInt(julianDate + '0001', 10);
var count = inputs.countasttag;
var uniqueCount = 0; // To keep track of unique numbers
var numberArray = [];
var i = 0;
while (uniqueCount < count) {
var generatedNumber = baseNumber + i;
// Check if this number exists in the alm_asset table
var assetGR = new GlideRecord('alm_asset');
assetGR.addQuery('asset_tag', generatedNumber.toString());
assetGR.query();
if (!assetGR.hasNext()) { // If number is not found, it's unique
numberArray.push(generatedNumber);
uniqueCount++; // Add to unique count
}
i++; // Move to the next number
}
outputs.generated_tags = numberArray.join(', ');
gs.info('Generated numbers: ' + numberArray.join(', '));
})(inputs, outputs);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 06:20 PM
Try this:
(function execute(inputs, outputs) {
var today = new Date();
var startOfYear = new Date(today.getFullYear(), 0, 1);
var diff = today - startOfYear;
var dayOfYear = Math.floor(diff / (1000 * 60 * 60 * 24)) + 1;
var julianDate = today.getFullYear().toString().slice(2) + dayOfYear.toString().padStart(3, '0');
var baseNumber = parseInt(julianDate + '0001', 10); //123
var count = inputs.countasttag;
var uniqueCount = 0; // To keep track of unique numbers
var numberArray = [];
var i = 0;
while (uniqueCount < count) {
var generatedNumber = baseNumber + i; //123
// Check if this number exists in the alm_asset table
var assetGR = new GlideRecord('alm_asset');
assetGR.addQuery('asset_tag', generatedNumber.toString());
assetGR.query();
if (!assetGR.hasNext()) { // If number is not found, it's unique
var gr = new GlideRecord("sc_req_item"); //123, 124
gr.addQuery("item", "CATALOG NAME");
gr.addQuery("state", "!=", "3"); //active RITM's
gr.orderBy("sys_created_on"); //asc to desc
gr.query();
while(gr.next()){
if(gr.variables.asset_tag.toString() == generatedNumber.toString()){ //replace with the actual variable name
i++;
generatedNumber = baseNumber + i; //124
}
}
numberArray.push(generatedNumber);
uniqueCount++; // Add to unique count
}
i++; // Move to the next number
}
outputs.generated_tags = numberArray.join(', ');
gs.info('Generated numbers: ' + numberArray.join(', '));
})(inputs, outputs);
(not tested)
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 01:57 PM
Hi Murthy, Thanks for your response however this is not working.
I dont see any output now.
Checked the flow execution and it shows error "Error: The undefined value has no properties.,Detail: The undefined value has no properties."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 02:12 PM
Also, i am not storing the output (generated asset tags) in any variable. I am just passing them in worknotes of RITM.
so i believe below conditon might be causing the issue.
if(gr.variables.asset_tag.toString() == generatedNumber.toString()){ //replace with the actual variable name
Is there a way we can still validate this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 03:05 PM
Modified my script a bit. However i am still stuck. I am storing the generated tags in RITM description and trying to match the generated tags with description. However still no luck.