Catalog client script

larissaribe
Tera Contributor

I need help creating a catalog client script with these functions:

 

The field name is date_of_movement

 

If the current date is between the 1st and the 19th, the transaction date can be any date within the current month. (Limited to business days)

 

If the current date is greater than or equal to the 20th, the transaction date can only be from the month following the current month onwards. (Limiting to business days)

 

Translated with DeepL.com (free version)

8 REPLIES 8

kaushal_snow
Mega Sage

Hi @larissaribe ,

 

By writing a catalog onChange client script on date_of_movement that computes todaya date, checks whether the day is ≤ 19 or ≥ 20, and depending on that either restricts the selected date to business days within the current month or only dates from the next month onward (while rejecting weekends), and additionally include a matching onSubmit script to enforce the same logic before submission so invalid dates are blocked even if the onChange check is bypassed...

 

Let me know if you need code for this...!!

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

TejasSN_LogicX
Tera Contributor

Hi @larissaribe ,

 

try this catalgoue client script simple logics

 

function onLoad() {
   // your field name and gets iits control
   var name = 'date_of_movement';
  var control = g_form.getControl(name) || g_form.getControl('variables.'+name);
  //alert("control == "+control);

  var today = new Date();
  var d = today.getDate();

 //checks its weekly
  function isWeekday(dt){ var day = dt.getDay(); return day !== 0 && day !== 6; }

  // get first weekday of month
  var first = new Date(today.getFullYear(), today.getMonth(), 1);
  while(!isWeekday(first)) first.setDate(first.getDate()+1);

//checking date in betn 1 to 19 or not 

  if (d >= 1 && d <= 19) {
    // allow this month's business days only
    var last = new Date(today.getFullYear(), today.getMonth()+1, 0);
    while(!isWeekday(last)) last.setDate(last.getDate()-1);
    control.setAttribute('min', first.toISOString().slice(0,10));
    control.setAttribute('max', last.toISOString().slice(0,10));
    g_form.showFieldMsg(name, 'Allowed: business days this month', 'info', false);
  } else {
    // allow from first business day of next month onwards
    var nextFirst = new Date(today.getFullYear(), today.getMonth()+1, 1);
    while(!isWeekday(nextFirst)) nextFirst.setDate(nextFirst.getDate()+1);
    control.setAttribute('min', nextFirst.toISOString().slice(0,10));
    control.removeAttribute('max');
    g_form.showFieldMsg(name, 'Allowed: business days from next month onwards', 'info', false);
  }
   
}

 

Let me know if this not working or  share sceenshorts of error you facing 

or let me know if you want onchange or onsubmit client scripts. 

tejas1111_0-1758942925509.png

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@larissaribe 

so what did you start with and where are you stuck?

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

I need the script, please