Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to calculate duration between two dates in business rule?

chenglo
Tera Contributor

Hi Experts,

I would to have your support for my issue. I'm creating a business rule to get data into another table for reports purpose. I want to calculate the duration between two dates that I have in my table. I'm currently using gs.dateDiff(); but seems the output isn't correct. Can any one please help me on this?

eg: opendate is "28-Aug-2018" // get from creation date

      supdate is "30-Aug-2018" // I get from supply team approval date

      Result = 14 // which is the number of days but in fact it doesn't correct. It's only 2 days, so the result I need to get 2.

 

(function executeRule(current, previous /*null when async*/) {
 
 // Add your code here
  
  if(current.u_vendor_aging_r_s =='Supply Completed'){
   
   // Query from sysapproval and insert to the table
   var recSysapproval = new GlideRecord('sysapproval_approver');
   recSysapproval.addQuery('sysapproval', current.sys_id);
   recSysapproval.addQuery('state', "Approved");
   recSysapproval.orderByDesc('sys_created_on');
   recSysapproval.setLimit(1);
   recSysapproval.query();
   
   while (recSysapproval.next()){
    var grvendor = new GlideRecord('u_vendor_master_data_aging_report');
    
    grvendor.initialize();
    
    grvendor.u_tsc_number = current.sys_id;
    grvendor.u_supply_appr_date = recSysapproval.sys_updated_on;
    grvendor.u_supply_approver = recSysapproval.approver.getDisplayValue();
    grvendor.u_company_code = current.variables.Company.getDisplayValue();
    grvendor.u_vendor_name = current.variables.vendor_name.getDisplayValue();
    grvendor.u_vendor_code = current.variables.vendor_code_by_TSC.getDisplayValue();
    grvendor.u_currency = current.variables.Currency.getDisplayValue();
    grvendor.u_type = current.variables.re_type.getDisplayValue();
    grvendor.u_request_date = current.variables.re_date.getDisplayValue();
    grvendor.insert();
    
    gs.addInfoMessage('The record has been inserted to Table');

     var opendate = current.u_request_date.getDisplayValue();
     var supdate = grvendor.u_supply_appr_date.getGlideObject();
     grvendor.u_aging_sup = gs.dateDiff(supdate.getDisplayValue(),opendate.getDisplayValue(),false);
     grvendor.update();
     gs.addInfoMessage('Effected the Aging');

    }
  
 })(current, previous);

 

Thanks for your support,

Cheng

1 ACCEPTED SOLUTION

HI,

I am confused here with this var opendate = current.u_request_date.getDisplayValue();

 

 from where u_request comes from.

Thanks,
Ashutosh

View solution in original post

11 REPLIES 11

Hi Ashutosh,

They are Date type.

The u_aging_sup field to record a duration between these two dates I set as Integer type.

 

Regards,

Cheng

HI,

I am confused here with this var opendate = current.u_request_date.getDisplayValue();

 

 from where u_request comes from.

Thanks,
Ashutosh

Hi Ashutosh,

 

Thank you very much for your help, it's working for me now. 🙂

Really appreciated.

Regards,

Cheng

Thanks Brother

Anurag Tripathi
Mega Patron
Mega Patron

This works fine for me

 

var gr=new GlideRecord('TABLENAME');
gr.get('RECORD SYS ID');
var diff = gs.dateDiff(gr.sys_created_on, gr.sys_updated_on, false);
gs.print(diff)

-Anurag