Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Configure function field to get date difference

Not applicable

when I'm creating a report I had to get date difference between current date and table value. 

I used configure function field to do that, can anyone help me to do the calculation to get the value. 

u_access_last_verified is the table column

pathumharsh_0-1768387275223.png

 

7 REPLIES 7

@Community Alums 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Tejas Adhalrao
Kilo Sage

Hi @Community Alums  ,

Your syntax should look like this:

glidefunction:datediff(now(), u_access_last_verified)

 

If you want the number to be positive (e.g., "10 days ago"), and the date in your table is in the past, you might need to swap them: glidefunction:datediff(u_access_last_verified, now())

Its_Sagnic
Giga Guru

Hi @Community Alums

(function() {

    var today = new GlideDateTime(); 
    
    var gr = new GlideRecord('sn_grc_indicator');
    gr.addEncodedQuery('item.sys_class_name=sn_risk_risk^ORitemISEMPTY^category=Risk Indicator^passed=failed^u_failed_dateISNOTEMPTY');
    gr.query();

    while (gr.next()) {
        gs.info('Processing GRC Indicator: ' + gr.number);

        var failedDate = new GlideDateTime(gr.u_failed_date); // Captured Indicator failed date
        var duration = GlideDateTime.subtract(failedDate, today);
        var diffInDays = duration.getDayPart();
        
        gs.info('The diffInDays is: ' + diffInDays);
        
    }
})();

@Community Alums ,

I used a same kind of code in a Scheduled job to get the diffrence between start date and failed date in an Indicator.

So the code that I used is returning the day differnce between start and failed date. You may use the same code and modify it as per your use case.

(function() {

    var today = new GlideDateTime(); 
    
    var gr = new GlideRecord('sn_grc_indicator');
    gr.addEncodedQuery('item.sys_class_name=sn_risk_risk^ORitemISEMPTY^category=Risk Indicator^passed=failed^u_failed_dateISNOTEMPTY');
    gr.query();

    while (gr.next()) {
        gs.info('Processing GRC Indicator: ' + gr.number);

        var failedDate = new GlideDateTime(gr.u_failed_date); // captured the failed date from indicator
        var duration = GlideDateTime.subtract(failedDate, today);
        var diffInDays = duration.getDayPart();
        
        gs.info('The diffInDays is: ' + diffInDays);
        
    }
})();


Change the  " u_failed_date "  and put your end date field "u_access_last_verified" In place of it and you will get your desired Output.

If you find it helpful for you please mark it as helpful and accept the solution.

Regards,

Sagnic