Can someone help in below script to compare date [if " last discovered " is greater than 30 days of the current date]

Sachin Gavhane
Giga Guru

I wanna check if "last discovered" date is greater than 30 days of the current date. please help me to rectify below script. will client script applied on database instance class is applicable to all its child classes? i wanna make it applicable to all its child classes. @shloke04 can you help

find_real_file.png

1 ACCEPTED SOLUTION

Well, you can use the business rule as follows;

When to run

find_real_file.png

find_real_file.png

 

Actions

find_real_file.png

 

Advanced

find_real_file.png

 

I believe that you are looking for a way to schedule a GlideRecord on this table and set the status of those records which have not been discovered for more than 30 days.

In case, if this is your requirement then you need to implement a scheduled job as follows;

find_real_file.png

Also make sure to adjust Run and Time fields as per your requirements.

 

Hopefully this will help you.

View solution in original post

9 REPLIES 9

Muhammad Khan
Mega Sage

Hi Sachin,

 

I have written the following script in the Background-Script and it is working as expected, you might need to create a script include to find and return the difference between the dates and call that script include via GlideAjax  in the client script;

var last_discovered = new GlideDateTime('2022-02-27');
gs.print('Last Discovered: ' + last_discovered);

var today = new GlideDateTime();
gs.print('Today: ' + today);

var time_elapsed = gs.dateDiff(last_discovered, today, true); // Calculates the difference between two dates (including time) and returns the result in seconds
gs.print('Time Elapsed: ' + time_elapsed);

if(time_elapsed >= 2592000) // Difference is greater than or equals to 2,592,000 seconds (means exactly 30 days)
    gs.print('30 days have been passed');
else
    gs.print('30 days have not been passed');


// You can utilize this script as per your requirements in script include.

 

For reference see the following images of output;

find_real_file.png

find_real_file.png

 

And to answer your second question, if by child-classes you mean child-tables then you need to check the inherited check-box in the client script so that it will work on the child tables of currently selected table, in this case Database Instanse [cmdb_ci_db_instance].

 

Hopefully this will help you resolve your queries.

am very new to this so can you please help me with script include please along with its respective client script.

or by anychance can we do this with BR?

but i guess Script include sounds better.

please help

Well, you can use the business rule as follows;

When to run

find_real_file.png

find_real_file.png

 

Actions

find_real_file.png

 

Advanced

find_real_file.png

 

I believe that you are looking for a way to schedule a GlideRecord on this table and set the status of those records which have not been discovered for more than 30 days.

In case, if this is your requirement then you need to implement a scheduled job as follows;

find_real_file.png

Also make sure to adjust Run and Time fields as per your requirements.

 

Hopefully this will help you.

shloke04
Kilo Patron

@Sachin Gavhane 

Suggestion will be to do it on change of field itself rather than waiting for submission to happen.You do not need to have a heavy script here and can be done with a UI Policy itself rather than using a Client Script.

Create a UI Policy on Database Instance class and configure the condition as below and if you need to throw an alert you can make use of Run Script in your UI Policy itself to inform user as shown below:

For applying to all child classes make sure to check the "Inherit" checkbox as true to apply it to all child classes:

find_real_file.png

find_real_file.png

If you still want to proceed with script approach then follow steps below:

1) Instead of doing it via o Submit approach would suggest use a Business Rule on your table as in an on Submit script waiting for a response from script include will be difficult to handle and the form may get submitted without waiting for the result from Script Include.

BR Details:

Table Name: Database Instance

When: Before Insert and Update

Condition: Most Recent Discovery Changes

Script:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var last_discovered = new GlideDateTime(current.last_discovered);
    var getCurrentDate = new GlideDateTime();

    var time_diff = gs.dateDiff(last_discovered, getCurrentDate, true);
    if (time_diff >= 2592000) {
        gs.addErrorMessage('30 days have crossed');
        current.setAbortAction(true);
    }


})(current, previous);

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke