Clarity Service Management

Expand all | Collapse all

Date field validation rule in change order

  • 1.  Date field validation rule in change order

    Posted 10-26-2018 08:52 AM
      |   view attached

    Hi,

     

    I need to validate the execution date in a change order, i have to prevent users from creating change orders if the following rule is not respected according to the type field (see image and also attachments).

     

    Rule needed:

    If type ('Tipo' in image) is Normal the execution time must be at least "Today()+72hs". And if this is not respected the user cannot save the change order.

     

    Change order type:

     

    Execution date:

     

    I think i have to create a spel file with the validation function and a mod file with the event definition but i am not sure how to do this and documentation is not very clear.

    Please, can someone help me with this or give me some tips?

     

    Regards,

    Facundo

    Attachment(s)

    zip
    detail_chg.htmpl.zip   8K 1 version


  • 2.  Re: Date field validation rule in change order

    Posted 10-26-2018 11:14 AM

    Hi,

     

    We actually have a similar requirement, but trigger the check once the change order has passed approval stage, because you can’t be sure that it will be approved within 72 hours, and if the date is in the past after it’s been approved the system automatically transitions the co to “Additional Information Required”. Your use case would simpler to build but I wanted to make sure you thought about the length of the approval process.



  • 3.  Re: Date field validation rule in change order

    Posted 10-26-2018 12:04 PM

    Hi Grant,

     

    Thanks for your answer.

    I understand what you said, but i only need to prevent the user from saving the change order if that condition is not ok.



  • 4.  Re: Date field validation rule in change order

    Posted 10-27-2018 01:20 PM

    Hi,

     

    Please give this a try in a test environment.

     

    Create a file names zCHG.mod and place in nx_root/site/mods/magic with the following contents.

    MODIFY chg PRE_VALIDATE zchg_check_start_date() 50000 FILTER(chgtype == 200 && EVENT("INSERT"));

     

     

    Create a file names zCHG.spl and place in nx_root/site/mods/magic with the following contents.

    chg::zchg_check_start_date(...)
    {
      string method;
      int int_now, int_future, int_scheduled_start_date;
      method="chg::zchg_check_start_date";
     
      int_now = now();
      int_future = int_now + 259200;
      int_scheduled_start_date = (int)sched_start_date;
     
      logf(MILESTONE, "%s started", method);
     
      logf(MILESTONE, "Now: %d, future: %d", int_now, int_future);
      logf(MILESTONE, "sched_start_date: %d", sched_start_date);
      if (int_scheduled_start_date < int_future) {
        set_error(1);
        set_return_data("The Scheduled Start Date must be at least 72 hours in the future.");
        return;
      }
      logf(MILESTONE, "%s ended", method);
    }


  • 5.  Re: Date field validation rule in change order

    Posted 10-29-2018 11:01 AM

    Hi Grant,

     

    I have followed you instructions but Service Desk login stops working when i copied the mod and spl file to the directory and restarted the service.

    I can see in the stdout log some errors:

     

    P_List_dob_by_helper got Init funciton timed out attempting to get crsq:5077
    Connecting to PDM_Text_nxd.
    Error in ATTR_INIT trigger doc_rep.server::set_server for server: AHD04508:Superado el tiempo de espera de la activación doc_rep.server::set_server
    Error in ATTR_INIT trigger doc_rep.servlet_path::set_servlet for servlet_path: AHD04508:Superado el tiempo de espera de la activación doc_rep.servlet_path::set_servlet
    Error in ATTR_INIT trigger doc_rep.server::set_server for server: AHD04508:Superado el tiempo de espera de la activación doc_rep.server::set_server
    Error in ATTR_INIT trigger doc_rep.servlet_path::set_servlet for servlet_path: AHD04508:Superado el tiempo de espera de la activación doc_rep.servlet_path::set_servlet

     

    After deleting the spl and mod file and restarting the service the login is working again.

    Can we try anything else?

     

    Thanks,

    Facundo



  • 6.  Re: Date field validation rule in change order

    Posted 10-29-2018 11:19 AM

    Hi Facundo,

     

    Sorry about that, I'll check the script locally and report back once it works.



  • 7.  Re: Date field validation rule in change order

    Posted 10-29-2018 12:06 PM

    This version appears to work..  Please note that they can change the scheduled start date after the initial activity. You might want to change the filter to include status = RFC.

     

    mod file

    MODIFY chg PRE_VALIDATE zchg_check_start_date() 50000 FILTER(chgtype == 200 && EVENT("INSERT"));

     

    spl file

    chg::zchg_check_start_date(...)
    {
      string method;
      int int_now, int_future, int_scheduled_start_date;
      method="chg::zchg_check_start_date";
     
      int_now = now();
      int_future = int_now + 259200;
      int_scheduled_start_date = (int)sched_start_date;
     
      logf(MILESTONE, "%s started", method);
     
      logf(MILESTONE, "Now: %d, future: %d", int_now, int_future);
      logf(MILESTONE, "sched_start_date: %d", sched_start_date);
      if (int_scheduled_start_date < int_future) {
        set_error(1);
        set_return_data("The Scheduled Start Date must be at least 72 hours in the future.");
        return;
      }
      logf(MILESTONE, "%s ended", method);
    }


  • 8.  Re: Date field validation rule in change order

    Posted 10-29-2018 12:31 PM

    Hi Bruneau,

    in this particular case and just to add to the conversation...

    Will this not make sense to trigger also on update? to prevent  user to change the date right away.

    Of course the validation may have to consider the open_date in that case vs. now().

    My 2 cents,

    /J

    P.S: Your proposal to do it on the workflow stage make more sense to me



  • 9.  Re: Date field validation rule in change order

    Posted 10-29-2018 12:35 PM

    I don't really think this requirement make sense at all. It should be keyed off an approval date or another transition on the change order, but it's their requirement.  If it keyed off of Update it might be too restrictive once the CO has left RFC, that's why I added a comment about checking for status in RFC.



  • 10.  Re: Date field validation rule in change order

    Posted 10-30-2018 07:34 PM

    Grant,

     

    It worked!

    One more question, if i want not to count hours for weekend days, how can i do?

    (It shouldn't count hours for saturdays and sundays)

     

    Regards,

    Facundo



  • 11.  Re: Date field validation rule in change order

    Posted 10-30-2018 10:52 PM

    You may find the built-in spel function "workshift_work2abs" useful:

    workshift_work2abs(<workshift schedule string>, <starting date>, <duration>);

    The function returns the date (in SDM date format) that results from adding a duration to a starting date and taking a particular workshift into account.

     

    So in your function above, you could do something like this:

    string RegWrkshft;  RegWrkshft = "wrkshft:4600";  // 'Regular' workshift persid.
    string RegWkshft_sched;   // 'Regular' workshift schedule string
    duration dur_ETA;  dur_ETA = (duration)259200;
    date dNow;
    date dFuture;

     

    // If we start a task 'now' that has 72 hours duration,

    //  when will it finish in the Regular workshift?
    dNow = now();

    RegWkshft_sched = expand(format("&{'%s' = wrkshft.persistent_id->sched}", RegWrkshft));

    dFuture = workshift_work2abs(RegWkshft_sched, dNow, dur_ETA);

     

    if ((int)sched_start_date < (int)dFuture)

    {

       set_error(1);

       set_return_data("The Scheduled Start Date must be at least 72 hours in the future.");

       return;

    }

    Regards,

    James



  • 12.  Re: Date field validation rule in change order

    Posted 10-30-2018 11:45 PM

    Thanks James, this is an excellent example!

     

    I was looking for an example of  workshift_work2abs but didn't find one, so I wrote a version that calculates the day of week from the unix epoch. HerramientasIT either of these methods should work. If you do use the workshift method you might need to define a new workshift if already using the regular workshift.

     

    chg::zchg_check_start_date(...)
    {
      string string_method, string_alert;
      date date_future;
      int int_now, int_future, int_scheduled_start_date, int_dow, int_epoch_dow, int_secs_per_day;

      string_method="chg::zchg_check_start_date"; //Capture method name for logging
      logf(MILESTONE, "%s started", string_method);

      int_now = now(); //Current time in unix epoch
      int_future = int_now + 259200; //Today plus 72 hours in unix time
      date_future = (date)int_future; //Date object of int_future
      int_epoch_dow = 3; //Day of week the unix epoch started
      int_secs_per_day = 86400; //Seconds in one day
      int_dow = ((int_future / int_secs_per_day) + int_epoch_dow) % 7; //Calculation for the day of the week
     
      //If day of week is Saturday add two days
      if(int_dow == 6){
        int_future += 86400*2;
        date_future = (date)int_future;
      }
      //If day of week is Sunday add one day
      else if(int_dow == 0){
        int_future += 86400;
        date_future = (date)int_future;
      }
     
      logf(MILESTONE, "Dow: %d", (int)int_dow);
     
      logf(MILESTONE, "Date: %s", (string)date_future);
      int_scheduled_start_date = (int)sched_start_date;
     
      logf(MILESTONE, "Now: %d, future: %d", int_now, int_future);
      logf(MILESTONE, "sched_start_date: %d", sched_start_date);
      if (int_scheduled_start_date < int_future) {
        set_error(1);
        string_alert = format("The Scheduled Start Date must be at least 72 hours in the future and not fall on a Saturday or Sunday; please choose date/time after %s.", (string)date_future);
        set_return_data(string_alert);
        return;
      }
      logf(MILESTONE, "%s ended", string_method);
    }