Catalog Form - Set end date to always Friday

nic10
Tera Contributor

Hi, we have a requirement wherein the end date should always fall on Friday. I am newbie in terms of scripting, asking advices on how to do catalog client script. I tried researching and can use the dayofWeek function? 

Can anyone help me? 

4 REPLIES 4

Mark Manders
Mega Patron

It sounds like a very strange requirement. What are the exact parameters so we can help you? 

Is it always the next Friday (so created anywhere from Friday to Thursday, it's the first Friday or is there other logic?

Have a client script calculating this for you, can be done, but are you sure the requirement is valid? In a normal flow of ticket handling, the end date is not set to a certain day, but based on a certain duration. It takes time to resolve something. 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hi Mark, 

 

We have start date wherein end user will enter for future date and duration is 1 week / within the week. 

For example: I have entered today's date on the start date (Oct 8), the end date will be on Friday (October 11) 

 

If ever it falls on Friday, let say they entered start date will be on (Oct 11), it will fall on next Friday (October 18).  

Assuming it's date fields you have as variables, you could try it with the below onChange Catalog Client Script. You may need to add some logging to troubleshoot the calculation if it doesn't work (and validate the field names!!)

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    // Get the start_date value from the form
    var startDateStr = g_form.getValue('start_date'); // Replace 'start_date' with your actual variable name!!
    var startDate = new Date(startDateStr);

    if (!isValidDate(startDate)) {
        return; 
    }
    // Calculate the day of the week (0 is Sunday, 6 is Saturday, 5 is Friday)
    var dayOfWeek = startDate.getDay();
    // Calculate how many days to add to get the next Friday
    var daysToAdd = (dayOfWeek === 5) ? 7 : (5 - dayOfWeek + 7) % 7;
    // Add the calculated days to start_date
    startDate.setDate(startDate.getDate() + daysToAdd);
    // Set the end_date field value
    var endDateStr = startDate.toISOString().split('T')[0]; 
    g_form.setValue('end_date', endDateStr); // Replace 'end_date' with your actual variable name!!
}
// Utility function to check if a date is valid
function isValidDate(d) {
    return d instanceof Date && !isNaN(d);
}

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Najmuddin Mohd
Mega Sage

Hi @nic10 ,
Here you go.

You need to create a script include and call it from Client script to validate the day selected.

Script Include Name: checkWeekDay
Client callable : True

Script Include:

var checkWeekDay = Class.create();
checkWeekDay.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    weekDay: function() {

		

        var day1 = this.getParameter('dateSelected');
        var dayWithTime = day1 + " 12:00:00";
        var gcdt = new GlideDateTime(dayWithTime); // fetch date

        
        var day = gcdt.getDayOfWeekLocalTime(); // return the local day number
        gs.addInfoMessage("Day:" + day);
        if (day == 5) {
            // gs.addInfoMessage("Friday");
            return true;

        } else {
           // gs.addInfoMessage("Not Friday");
            return false;

        }
    },

    type: 'checkWeekDay'
});



Client script:

An onLoad client script, 'u_date' is the backend field value.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below

    var date = g_form.getValue('u_date');  

	
	

    
        var ga = new GlideAjax('global.checkWeekDay'); //Scriptinclude name
        ga.addParam('sysparm_name', 'weekDay'); //Method of scriptInclude
        ga.addParam('dateSelected', date); //Sending date
        ga.getXML(callback);
    

    function callback(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer == 'false') { // Friday
            g_form.clearValue('u_date'); // [the name of the date field]
            g_form.addInfoMessage('Select only fridays');
        }
       // g_form.addInfoMessage(answer);
    }

}



NajmuddinMohd_0-1728385414821.png

 

NajmuddinMohd_1-1728385447918.png



If the above information helps you, Kindly mark it as Helpful and Accept the solution.

Regards,
Najmuddin.