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.

Set date to 1st day of next month based on other field

Chenab Khanna
Tera Expert

Hi,

I have 2 date/time fields (say, A & b) and need to set value on 1 field based on other.

So, whatever date/time value user selects manually in field A, then field B should be auto populated with 1st day of next month.

For eg, if user selects value of A as 4th april,2021 23:00:00, then value of B should be 1st May,2021 00:00:00.

1st will be constant for every value of A, only the month will change depending on month selected in A. Time will also remain constant as 00:00:00AM

Can someone help me with this?

6 REPLIES 6

Sai Kumar B
Mega Sage

Hi @Chenab Khanna,

1.) Write onChange client script call script include via GlideAjax() by passing A field as parameter

2.) In Script include get the A field month as 

var month = new GlideDateTime(A field parameter);

var getMonthValue = month.getMonth() //you'll get a numeric value

Add +1 to that getMonthValue and use this "addMonths(after incrementing month value)" to add months value and pass that to client script.

Also follow this link 
Date/Time scripts

Please mark my answer as helpful, if applicable

Best Regards,

Sai Kumar

Hi,

Thanks for your response. I already did that part actually, please find the script include - 

find_real_file.png

The thing i am not understanding is how do i set the day to 1st and time to 00:00:00AM and return the complete value back to my client script?

can you try this 

var date = new GlideDateTime(accountingDate);

var month = date.addMonthsLocalTime(1);

var day = date.getDayOfMonth();

var firstOfMonth = new GlideDateTime(date)

firstOfMonth.addDaysLocalTime( -1 * (day-1));

Please mark my answer as helpful/correct, if applicable

Best Regards,

Sai Kumar

var PopulateReversalDate = Class.create();
PopulateReversalDate.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
setDate : function(){
var accountingDate = this.getParameter('sysparm_accounting');
var date = new GlideDateTime(accountingDate);
var getMonth = date.addMonthsLocalTime(1);
var day = date.getDayOfMonthLocalTime();
var firstOfMonth = new GlideDateTime(date);
var reversalDate = firstOfMonth.addDaysLocalTime(-1*(day - 1));
gs.addInfoMessage("reversal date "+reversalDate);
return reversalDate;

},
type: 'PopulateReversalDate'
});

 

This is my script include, but 'reversal date' is coming as undefined