Create catalog task based on date/time field

RB1
Tera Contributor

Hello Everyone,

I need a help on one issue.

I have two non mandatory variables on catalog item form:

 

Assignment Date (Date field);

Assignment Time(String field to input only time for example 9:00 AM EST).

 

If these fields are blank then, task catalog should create on upcoming monday. 

If these fields have any value for example 26-02-2023 and 9:00:00 then task should create only afte this time. 

Please let me know what to do. 

Thanks in advance

2 REPLIES 2

Fernando Koenra
Kilo Sage

Hi RB1, 

This can be done in various ways, so maybe you could clarify what you have already attempted to try and achieve the desired result? 🙂

 

To avoid users entering a wrong format, maybe you could use the date/time variable type which has the Date and Time in one field. For example: 24-02-2023 14:00:00. Unless you have a specific reason it has to be separated?

To achieve the desired result you could for example:
1) Instead of validating afterwards, give the date/time variable a default value

javascript: new GlideDateTime(gs.daysAgo(-1)).getDisplayValue();

Which would set the date already to tomorrow by default on opening the catalog item. Then the user can modify if if they want a different date/time before submitting the catalog item. 


2) User server-side logic to set the date to tomorrow after submit
If you are already running a specific flow that sets out the actions to create that task at the given date/time - you could include an action in your flow that checks if the datetime variable was left empty: 
In that case you can easily use flow logic to then set your action for 'tomorrow', or alternatively use GlideDateTime

3) Validate and set the date/time using a on-submit client side script;
You could also write an on-submit catalog client script that checks if your date or datetime field is empty and then fill it with the datetime of tomorrow.
Unfortunately GlideDateTime is not available on the client side so I think this has to be done using the javascript Date() object. 
For example: 

function onSubmit() {
  var aDatetime = g_form.getValue('a_datetime'); //Your datetime catalog variable 

  // If the variable is empty - get the current date/time and add 1 day to it
  if (!aDatetime) {
    var now = new Date();
    now.setDate(now.getDate() + 1);

    // Convert the date object to a string in the expected format for a datetime variable (YYYY-MM-DD HH:mm:ss)
    var formattedDate = now.getFullYear() + '-' + padNumber(now.getMonth() + 1) + '-' + padNumber(now.getDate()) + ' ' + padNumber(now.getHours()) + ':' + padNumber(now.getMinutes()) + ':' + padNumber(now.getSeconds());

    // Set the value of the datetime variable
    g_form.setValue('a_datetime', formattedDate);
  }
}

//small function to add leading zeroes if needed, to match the datetime format
function padNumber(num) {
  return num < 10 ? '0' + num : num;
}

 
Hope this helps you get some ideas to try out and get further in your requirements. 

Kind regards,

Fernando Koenraad
iTSM Group

Thanks for your response,

It's not about to setting the variable field. I need a script for timer activity in workflow. CAn you please help me on the script as per the requirment. 

Thanks in Advance