- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 11:06 PM
my script is returning 15-01-2024 in my background script and the same code is returning 13-01-2024 in script include which I need to use for catalog item.
Background script:
var todayDate = new GlideDateTime();
var localDate = todayDate.getDisplayValue();
var latestDate = localDate.split(' ')[0];
var totalRecordsWithLatestDate = 0;
var continueProcessing = true;
gs.log("today's date - "+todayDate);
gs.log("local date -"+localDate);
gs.log("latest date - "+latestDate);
while (continueProcessing) {
var gr = new GlideRecord('alm_hardware');
gr.addQuery('depreciation_date', '>=', latestDate + ' 00:00:00');
gr.addQuery('depreciation_date', '<=', latestDate + ' 23:59:59');
gr.query();
while (gr.next()) {
var recordDate = gr.getDisplayValue('depreciation_date').split(' ')[0];
if (recordDate == latestDate) {
totalRecordsWithLatestDate++;
}
}
gs.print("Total Records with Latest Date - " + totalRecordsWithLatestDate);
if (totalRecordsWithLatestDate >= 2) {
var nextDay = new GlideDateTime(latestDate);
var hours = 60 * 60 * 30;
nextDay.addSeconds(hours);
latestDate = nextDay.getDate().getDisplayValue();
todayDate.addSeconds(hours);
gs.print("Moving to Next Day: " + latestDate);
totalRecordsWithLatestDate = 0; // Reset the counter for the new date
} else {
gs.print("Latest Date: " + latestDate);
continueProcessing = false; // Set the flag to exit the loop
}
}
-------------------------------------------------------------------------------
Output:
===============================================================
==========================================================
Script include:
var aggregateUtils = Class.create();
aggregateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
findDateWithLessRecords: function() {
var todayDate = new GlideDateTime();
var localDate = todayDate.getDisplayValue();
var latestDate = localDate.split(' ')[0];
var totalRecordsWithLatestDate = 0;
var continueProcessing = true;
// gs.log("today's date - "+todayDate);
// gs.log("local date -"+localDate);
// gs.log("latest date - "+latestDate);
while (continueProcessing) {
var gr = new GlideRecord('alm_hardware');
gr.addQuery('depreciation_date', '>=', latestDate + ' 00:00:00');
gr.addQuery('depreciation_date', '<=', latestDate + ' 23:59:59');
gr.query();
while (gr.next()) {
var recordDate = gr.getDisplayValue('depreciation_date').split(' ')[0];
if (recordDate == latestDate) {
totalRecordsWithLatestDate++;
}
}
//gs.print("Total Records with Latest Date - " + totalRecordsWithLatestDate);
if (totalRecordsWithLatestDate >= 2) {
var nextDay = new GlideDateTime(latestDate);
var hours = 60 * 60 * 24;
nextDay.addSeconds(hours);
latestDate = nextDay.getDate().getDisplayValue();
todayDate.addSeconds(hours);
//gs.print("Moving to Next Day: " + latestDate);
totalRecordsWithLatestDate = 0; // Reset the counter for the new date
return latestDate;
} else {
//gs.print("Latest Date: " + latestDate);
continueProcessing = false; // Set the flag to exit the loop
return latestDate;
}
}
},
type: 'aggregateUtils'
});
==================================================
Client Script:
function onLoad() {
var aggregateUtils = new GlideAjax('aggregateUtils');
aggregateUtils.addParam('sysparm_name', 'findDateWithLessRecords');
aggregateUtils.getXML(DateDetails);
function DateDetails(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
//alert(answer);
g_form.setValue('schedule_the_date_of_your_asset_refresh', answer);
//next_slot_available_date
} else {
//alert('No date found with less than 10 records.');
}
}
}
---------------------------------------------------------------------------------
Output:
Attaching Bachground script output and script include output. Pls help me with the fixing.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 11:30 AM
Hello @ROS4349
I wanted to check in regarding the response I provided. If my suggestions were beneficial in addressing your query or helped in resolving your issue, would you mind marking it as helpful, accepting the solution, and closing the thread? Your acknowledgment not only shows appreciation for the assistance but also assists future readers who might come across a similar problem.
Thank you for your consideration!
Best regards,
Aniket.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 11:30 AM
Hello @ROS4349
I wanted to check in regarding the response I provided. If my suggestions were beneficial in addressing your query or helped in resolving your issue, would you mind marking it as helpful, accepting the solution, and closing the thread? Your acknowledgment not only shows appreciation for the assistance but also assists future readers who might come across a similar problem.
Thank you for your consideration!
Best regards,
Aniket.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 08:42 PM
Hi Aniketha,
Your code is not working on Date,time field so created a date type field and customized.
Anyways I got help from your code. Thank you...