Verwenden des DurationCalculators, um ein Fälligkeitsdatum zu berechnen

  • Freigeben Version: Yokohama
  • Aktualisiert 30. Januar 2025
  • 7 Minuten Lesedauer
  • Mit der Skripteinbindung „DurationCalculator“ können Sie ein Fälligkeitsdatum berechnen, indem Sie entweder eine einfache Dauer oder eine relative Dauer basierend auf Zeitplänen verwenden.

    Das folgende Skript demonstriert, wie die globale API verwendet wird Dauerrechner Dient zur Berechnung eines Fälligkeitsdatums. Der erste Teil des Skripts veranschaulicht, wie ein Startdatum/-Uhrzeit mit festgelegt wird SetStartdateTime() Methode und dann verwenden CalcDuration() Methode zum Bestimmen eines Fälligkeitsdatums, das „x“ der kontinuierlichen Zeit (Sekunden) ab dem angegebenen Startdatum und der angegebenen Startzeit beträgt. Die zweite Hälfte des Skripts veranschaulicht die Verwendung Dauerrechner Dient zur Berechnung eines Fälligkeitsdatums basierend auf einem Zeitplan. Mit Zeitplänen können Sie einen „Filter“ auf zukünftige Zeiten anwenden, z. B. nur die Tage in einer Arbeitswoche in die Berechnung einbeziehen. Wenn Sie beispielsweise einen Zeitplan „Wochentage“ (der nur Montag bis Freitag umfasst) auf Ihre Berechnung der Dauer anwenden und die Startzeit Freitag um 17:00 Uhr ist, wenn Sie eine Dauer von zwei Tagen hinzufügen, ist Ihr Fälligkeitsdatum Dienstag um 17:00 Uhr. Wenn Sie keinen Zeitplan verwendet haben, ist Ihr Fälligkeitsdatum Sonntag um 17:00 Uhr. Weitere Informationen zu Zeitplänen finden Sie unter Zeitpläne werden erstellt und verwendet .

    Dieses Skript kann ausgeschnitten und in die Skripthintergrundseite eingefügt und unverändert ausgeführt werden. Es kann auch als Beispiel für die Erstellung von Business-Regeln, UI-Aktionen oder einem anderen verwendeten Ort dienen, an dem serverseitiges Skript erstellt werden kann.
    /**
     * Demonstrate the use of DurationCalculator to compute a due date.
     * 
     * You must have a start date and a duration. Then you can compute a
     * due date using the constraints of a schedule.
     */
     
    gs.include('DurationCalculator');
    executeSample();
     
    /**
     * Function to house the sample script.
     */
    function executeSample(){
     
        // First we need a DurationCalculator object.
        var dc = new DurationCalculator();
     
        // --------------- No schedule examples ------------------
     
        // Simple computation of a due date without using a schedule. Seconds
        // are added to the start date continuously to get to a due date.
        var gdt = new GlideDateTime("2012-05-01 00:00:00");
        dc.setStartDateTime(gdt);
        if(!dc.calcDuration(2*24*3600)){ // 2 days
          gs.log("*** Error calculating duration");
          return;
        }
        gs.log("calcDuration no schedule: " + dc.getEndDateTime()); // "2012-05-03 00:00:00" two days later
     
        // Start in the middle of the night (2:00 am) and compute a due date 1 hour in the future
        // Without a schedule this yields 3:00 am.
        var gdt = new GlideDateTime("2012-05-03 02:00:00");
        dc.setStartDateTime(gdt);
        if(!dc.calcDuration(3600)){
          gs.log("*** Error calculating duration");
          return;
         }
        gs.log("Middle of night + 1 hour (no schedule): "+ dc.getEndDateTime()); // No scheduled start date, just add 1 hour
     
     
        // -------------- Add a schedule to the date calculator ---------------------
        addSchedule(dc);
     
        // Start in the middle of the night and compute a due date 1 hour in the future.
        // Since we start at 2:00 am the computation adds the 1 hour from the start
        // of the day, 8:00am to get to 9:00am
        var gdt = new GlideDateTime("2012-05-03 02:00:00");
        dc.setStartDateTime(gdt);
        if(!dc.calcDuration(3600)){
          gs.log("*** Error calculating duration");
          return;
        }
        gs.log("Middle of night + 1 hour (with 8-5 schedule): " + dc.getEndDateTime()); // 9:00 am
     
        // Start in the afternoon and add hours beyond quiting time. Our schedule says the work day
        // ends at 5:00pm, if the duration extends beyond that, we roll over to the next work day.
        // In this example we are adding 4 hours to 3:00pm which gives us 10:00 am the next day.
        var gdt = new GlideDateTime("2012-05-03 15:00:00");
        dc.setStartDateTime(gdt);
    
        if(!dc.calcDuration(4*3600)){ 
          gs.log("*** Error calculating duration");
          return;}
        gs.log("Afternoon + 4 hour (with 8-5 schedule): " + dc.getEndDateTime()); // 10:00 am.
     
        // This is a demo of adding 2 hours repeatedly and examine the result. This
        // is a good way to visualize the result of a due date calculation.
        var gdt = new GlideDateTime("2012-05-03 15:00:00");
        dc.setStartDateTime(gdt); 
        for(var i=2; i<24; i+=1){
          if(!dc.calcDuration(i*3600)){
            gs.log("*** Error calculating duration");
            return;
          }
          gs.log("add "+ i +" hours gives due date: " + dc.getEndDateTime());
        }
     
        // Setting the timezone causes the schedule to be interpreted in the specified timezone.
        // Run the same code as above with different timezone. Note that the 8 to 5 workday is
        // offset by the two hours as specified in our timezone.
        dc.setTimeZone("GMT-2");
        var gdt = new GlideDateTime("2012-05-03 15:00:00");
        dc.setStartDateTime(gdt);
        for(var i=2; i<24; i+=1){
          if(!dc.calcDuration(i*3600)){ 
            gs.log("*** Error calculating duration");
            return;
          }
            gs.log("add "+ i +" hours gives due date (GMT-2): " + dc.getEndDateTime());
        }
    }
     
    /** 
     * Add a specific schedule to the DurationCalculator object.
     *  
     * @param durationCalculator An instance of DurationCalculator
     */
    function addSchedule(durationCalculator){
        // Load the "8-5 weekdays excluding holidays" schedule into our duration calculator.
        var scheduleName = "8-5 weekdays excluding holidays";
        var grSched = new GlideRecord('cmn_schedule');
        grSched.addQuery('name', scheduleName);
        grSched.query();
        if(!grSched.next()){
          gs.log('*** Could not find schedule "'+ scheduleName +'"');
          return;
        }
        durationCalculator.setSchedule(grSched.getUniqueValue(),"GMT");
    }

    Verwenden des DurationCalculators, um eine einfache Dauer zu berechnen

    Eine einfache Dauer ist die Anzahl von Sekunden zwischen zwei Datumszeiten.

    Wenn kein Zeitplan verwendet wird, handelt es sich um eine einfache Subtraktion des Zeitdatums. Wenn ein Zeitplan verwendet wird, wird der Zeitplan konsultiert, um arbeitsfreie Stunden aus der Berechnung zu entfernen. Angenommen, Zeitplan „8-5 Wochentage ohne Feiertage“ wird verwendet. In diesem Fall beträgt die Anzahl der Arbeitsstunden von Montag bis Dienstag Mittag neun Stunden. Um eine einfache Dauer zu berechnen, initialisieren Sie die globale API Dauerrechner Und rufen Sie an CalcScheduleDuration() Methode.

    Dieses Skript demonstriert die Verwendung Dauerrechner Dient zur Berechnung einer einfachen Dauer.
    /**
     * Sample script demonstrating use of DurationCalculator to compute simple durations
     * 
     */
     
    gs.include('DurationCalculator');
    executeSample();
     
    /**
     * Function to house the sample script.
     */
    function executeSample(){
     
        // First we need a DurationCalculator object.
        var dc = new DurationCalculator();
     
        // Compute a simple duration without any schedule. The arguments
        // can also be of type GlideDateTime, such as fields from a GlideRecord.
        var dur = dc.calcScheduleDuration("2012-05-01", "2012-05-02");
        gs.log("calcScheduleDuration no schedule: " + dur); // 86400 seconds (24 hours)
     
        // The above sample is useful in limited cases. We almost always want to 
        // use some schedule in a duration computation, let's load a schedule.
        addSchedule(dc);
     
        // Compute a duration using the schedule. The schedule
        // specifies a nine hour work day. The output of this is 32400 seconds, or
        // a nine hour span.
        dur = dc.calcScheduleDuration("2012-05-23 12:00:00","2012-05-24 12:00:00");
        gs.log("calcScheduleDuration with schedule: " + dur); // 32400 seconds (9 hours)
     
        // Compute a duration that spans a weekend and holiday. Even though this
        // spans three days, it only spans 9 work hours based on the schedule.
        dur = dc.calcScheduleDuration("2012-05-25 12:00:00", "2012-05-29 12:00:00");
        gs.log("calcScheduleDuration with schedule spaning holiday: " + dur); // 32400 seconds (9 hours)
     
        // Use the current date time in a calculation. The output of this is
        // dependent on when you run it.
        var now = new Date();
        dur = dc.calcScheduleDuration("2012-05-15", new GlideDateTime());
        gs.log("calcScheduleDuration with schedule to now: " + dur); // Different on every run.
    }
     
    /** 
     * Add a specific schedule to the DurationCalculator object.
     *  
     * @param durationCalculator An instance of DurationCalculator
     */
    function addSchedule(durationCalculator){
        // Load the "8-5 weekdays excluding holidays" schedule into our duration calculator.
        var scheduleName = "8-5 weekdays excluding holidays";
        var grSched = new GlideRecord('cmn_schedule');
        grSched.addQuery('name', scheduleName);
        grSched.query();
        if(!grSched.next()){
            gs.log('*** Could not find schedule "'+ scheduleName +'"');
            return;
        }
        durationCalculator.setSchedule(grSched.getUniqueValue());
    }

    Relative Dauer wird verwendet

    Die relative Dauer ähnelt sehr der einfachen Dauer, es sei denn, ein Skript wird verwendet, um zu bestimmen, welche Teile eines Tages aus der Differenzberechnung entfernt werden sollen.

    Dieses Skript wird in der Tabelle „cmn_relative_duration“ gespeichert und kann durch Navigieren zu überprüft werden Systemzeitplaner > Zeitpläne > Relative Daueran. Die sofort einsatzbereite Instanz enthält einige Beispiele für Skripts der relativen Dauer.

    Eine relative Dauer sys_ID wird an die Methode übergeben CalcRelativeDuration() Der globalen API Dauerrechner Klasse nach Initialisierung. Wenn diese Methode aufgerufen wird, wird das Objekt „DurationCalculator“ als Variable an das Skript „relative Dauer“ (gespeichert in Tabelle „cmn_relative_duration“) übergeben Rechner . Daher hat das Skript für relative Dauer, das Sie in cmn_relative_duration schreiben und speichern, über die Variable Zugriff auf den ausführenden DurationCalculator Rechner .

    Das folgende Skript demonstriert die Verwendung Dauerrechner Dient zur Berechnung einer relativen Dauer.
    /**
     * Sample use of relative duration calculation.
     * 
     */
     
    gs.include('DurationCalculator');
    executeSample();
     
    /**
     * Function to house the sample script.
     */
    function executeSample(){
     
        // First we need a DurationCalculator object. We will also use
        // the out-of-box relative duration "2 bus days by 4pm"
        var dc = new DurationCalculator();
        var relDur = "3bf802c20a0a0b52008e2859cd8abcf2"; // 2 bus days by 4pm if before 10am
        addSchedule(dc);
     
        // Since our start date is before 10:00am our result is two days from// now at 4:00pm.
        var gdt = new GlideDateTime("2012-05-01 09:00:00");
        dc.setStartDateTime(gdt);
        if(!dc.calcRelativeDuration(relDur)){
          gs.log("*** calcRelativeDuration failed");
          return;
        }
        gs.log("Two days later 4:00pm: "+ dc.getEndDateTime());
     
        // Since our start date is after 10:00am our result is three days from
        // now at 4:00pm.
        var gdt = new GlideDateTime("2012-05-01 11:00:00");
        dc.setStartDateTime(gdt);
        if(!dc.calcRelativeDuration(relDur)){
          gs.log("*** calcRelativeDuration failed");
          return;
        }
        gs.log("Three days later 4:00pm: "+ dc.getEndDateTime());}
     
    /** 
     * Add a specific schedule to the DurationCalculator object.
     *  
     * @param durationCalculator An instance of DurationCalculator
     */
    function addSchedule(durationCalculator){
        // Load the "8-5 weekdays excluding holidays" schedule into our duration calculator.
        var scheduleName ="8-5 weekdays excluding holidays";
        var grSched =new GlideRecord('cmn_schedule');
        grSched.addQuery('name', scheduleName);
        grSched.query();
        if(!grSched.next()){
          gs.log('*** Could not find schedule "'+ scheduleName +'"');
          return;}
        durationCalculator.setSchedule(grSched.getUniqueValue(),"GMT");}