User should be able to fil the current month's timesheet only rest days should be readonly.

VIKAS MISHRA
Tera Contributor

As per requirement user should be able to fill the current month's days only rest days fields should be read only.

we have a field "weeks start on" there user has to select the first date of week.

If current date is 31/mar/2025 and weeks starts with 31 mar only , then user should be able to fill the field "monday" only rest all other days should be read only.

and if current date is 01/apri/2025 and field  week starts on 31 mar, in this case user should be able to fill all the days except Monday as Monday is from previous month hence monday should be read only. 

Please suggest how to achieve the same.

VIKASMISHRA_0-1744114513404.jpeg

 

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@VIKAS MISHRA 

you can use onLoad client script and Display business rule on that table

Will week always start on Monday?

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

Can you please provide me the script which i am unable to make

@VIKAS MISHRA 

check script shared by @Kieran Anson 

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

Kieran Anson
Kilo Patron

Hi,

Yes this is possible with an onLoad Client Script. However, you have to deactivate both Data Policies on time_card as UI Policies/Data Policies run AFTER a client script, so undoes what the client script applies

 

function onLoad() {

    //get the week field and turn into a date obj
    var weekStartOn = g_form.getValue('week_starts_on');
    var userDateFormat = g_user_date_format;
    var dateTimeNumber = getDateFromFormat(weekStartOn, userDateFormat);
    var weekStart = new Date(dateTimeNumber); //assuming this is always a Sunday...could add code to validate that

    for (var i = 0; i < 7; i++) {
		//get the next date
        var nextDate = new Date(weekStart);
        nextDate.setDate(weekStart.getDate() + i);

		//check if we move into a new month
        if (weekStart.getMonth() !== nextDate.getMonth()) {

			//get the long name, which matches the field name
            var targetField = nextDate.toLocaleDateString('en-US', {
                weekday: 'long'
            }).toLowerCase();

            g_form.setMandatory(targetField, false);
            g_form.setReadOnly(targetField, true);
        } else {
            g_form.setMandatory(targetField, true);
        }

    }
}