The CreatorCon Call for Content is officially open! Get started here.

find the difference in dates and display the value in the field in the table.

Priya Rao
Tera Contributor

Hi All,

I have a requirement. Based on some trigger conditions, we need to submit a catalog item request. This is done using he subflow and business rules and it's working as expected. 

So now, in the table that we're using for trigger conditions, there's a field 'start_date' and 'end_date'. Based on that dates, when the catalog item request is auto-submitted, we need to calculate the difference in the days (end_date - start_date)

If start_date is 20/09/2023 and end_date is 25/09/2023, the difference should be 5 and this value should be filled in the field 'date_difference'  in the table. This should happen only when the RITM is raised.

How can I achieve this, please let me know.

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Priya Rao 

you can use before insert/update business rule

Use correct field names.

var start = new GlideDateTime(current.start_date);
var end = new GlideDateTime(current.end_date);
var dur = new GlideDuration();
dur = GlideDateTime.subtract(start, end);

var daysPart = dur.getDayPart();
current.fieldName = daysPart;

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

5 REPLIES 5

Saurabh Gupta
Kilo Patron

Hi,
You can calculate using below script

var gdt1 = new GlideDateTime("2011-08-28 09:00:00");
var gdt2 = new GlideDateTime("2011-08-31 08:00:00");
 
var dur = GlideDateTime.subtract(gdt1, gdt2); // Difference between gdt1 and gdt2
gs.info(dur.getDisplayValue());

Thanks and Regards,

Saurabh Gupta

Hi @Saurabh Gupta  

Thank you for your response. 

The above data is in the hr_profile table. (Employment Start Date and Contract End Date)

I need to get that dates and find the difference and add the days in another field (Contract Conversion Date Difference.)

 

Dates.pngDifference.png

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Priya Rao 

you can use before insert/update business rule

Use correct field names.

var start = new GlideDateTime(current.start_date);
var end = new GlideDateTime(current.end_date);
var dur = new GlideDuration();
dur = GlideDateTime.subtract(start, end);

var daysPart = dur.getDayPart();
current.fieldName = daysPart;

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar thank you so much. this is working fine. I'll mark this as solution. But my requirement is slightly changed now. Should i post a new question ?

 

This is the requirement. Based on some trigger conditions, we need to submit a catalog item request. This is done using he subflow and business rules and it's working as expected. 

So now, in the table that we're using for trigger conditions, there's a field 'start_date' and 'end_date'. Based on that dates, when the catalog item request is auto-submitted, we need to calculate the difference in the days (end_date - start_date) not directly from he value in the table. Instead, i need to store the values in the variables in the RITM, then calculate the difference and then store that difference value in the field in the table.

function onLoad() {

    var start = g_form.getValue('contract_conversion_date');
    var end = g_form.getValue('contract_end_date'); 
    var difference = g_form.getValue('contract_conversion_date_difference'); 

        var startDate = new Date(start);
        var endDate = new Date(end);
        var duration = endDate - startDate;

        difference = duration.getRoundedDayPart(); 
        g_form.setValue('contract_conversion_date_difference', difference); 
}

I have used UI policy. Can you please help me with this?