Business Rule to calculate the days pending

Annette Kitzmil
Tera Guru

Hello,

I have created a business rule that is supposed to fill a table column on my called Days Pending.  Then I created a Business rule with the script below that needs to calculate todays date minus the lastUpdate.  I need it to return just the number of days to my new column days_pending.  Can someone take a look at the script below and let me know what I am missing to have it prefill the number of days to my table column please?

 

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

    var todaysDate = new GlideDate();
    var lastUpdate = new GlideDateTime(current.sys_updated_on);//This is using the "Updated" field
    var daysPendingCalculation = new GlideDate.subtract(todaysDate, lastUpdate);

    current.days = daysPendingCalculation;
   
})(current, previous);
9 REPLIES 9

Hi Kilo,

 

Sorry, that I didn't also reply to you, but yes, I tried that too and still the field didn't fill.  As I continued researching this, I recognized we have a metric definition that contains the duration from status (state) to state.  So, I started trying to figure out how I could possible pull the information over from the metric_instance an maybe fill the field days_pending using that.  Below is a very rough draft of the previous code and some code that has the metric_instance and the start of some queries.  Would you mind taking a look and seeing if you can put this together?  I will be working on this more today, but any help would be appreciated.

 

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

    var metricGr = new GlideRecord ('metric_instance');
    gr.addQuery('id', current.sys_id);
    gr.addQuery('calculation_complete', false);
    gr.addQuery('definition.type', 'field_value_duration');
    gr.addQuery();
    while(gr.next()){
        var definition = new GlideRecord('metric_definition');
        definition.get(gr.defintion);
        var mi = new MetricInstance(definition, current);


    }


var todaysDate = new GlideDate();

    var startDate = new GlideDate();

/*var metricGr = new GlideRecord ('metric_instance');
    gr.addQuery('id', current.sys_id);
    gr.addQuery('sys_updated_on', current.sys_updated_on);*/

    startDate.setValue(current.sys_updated_on);

    var dur = GlideDate.subtract(startDate, todaysDate);
   
    //gs.info("Date: " + dur.getDayPart());

    days_pending = 5; //dur.getDayPart();
   

})(current, previous);

Here is another draft that is hopefully closer to what is needed:

var metricGr = new GlideRecord ('metric_instance');

    metricGr.query();

    while(metricGr.next()) {
        var getRecordSysId = new GlideRecord('4e359e94878771542f8ecae5dabb35d4');//CDEP table sys_id field
        getMetricId.addQuery('id', current.metricId.sys_id); //gets the current sys_id on the ID field which matches the sys_id on our RDEP table
        getMetricDuration.addQuery('duration', ''); //gets the duration for the record

        if(getMetricId.next()){
            var daysPending = getRecordSysId;

            metricGr.duration = daysPending.days_pending;

            daysPending.update();


        }

Thanks, let me see if this helps with what I did yesterday, I appreciate it.

Jitendra Diwak1
Kilo Sage

Hi @Annette Kitzmil,

 

Please try this below code

 

 

 

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

var todaysDate = new GlideDate();
var lastUpdate = new GlideDateTime(current.sys_updated_on); // This is using the "Updated" field

// Calculate the difference in days between todaysDate and lastUpdate
var daysPendingCalculation = GlideDateTime.subtract(todaysDate, lastUpdate).getNumericValue();

current.days_pending = daysPendingCalculation; // Assign the calculated value to the days_pending field

})(current, previous);

 

Please accept my solution if it resolves your issue and thumps 👍 up 

 

Thanks 

Jitendra 

Please accept my solution if it works for and thumps up.

Hi Jitendra,

So, here is the field that I am trying to get the business rule script to fill to.  The type was integer even though it is showing duration now, but just so you know what the business rule field I am looking to fill with this calculation.

AnnetteKitzmil_0-1718125981820.png

Then here is the business rule I created details and the script at the bottom is where I am at now, but I also tried applying the script above and still, the column isn't showing the days.

AnnetteKitzmil_1-1718126087894.png

 

Script to try and force a fil and it doesn't fill the field either, but you can see what is commented out.  Any other suggestions on what else might be preventing it from filling the days to the field?

 

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

var todaysDate = new GlideDate();

    var startDate = new GlideDate();

    startDate.setValue(current.sys_updated_on);

    var dur = GlideDate.subtract(startDate, todaysDate);
   
    //gs.info("Date: " + dur.getDayPart());

    days_pending = 5; //dur.getDayPart();
   

})(current, previous);