CA Service Management

Expand all | Collapse all

Difference Between Two Date Attributes

  • 1.  Difference Between Two Date Attributes

    Posted Jun 02, 2016 07:57 AM

    Hi Team,

     

    On detail_cr/detail_in form, I want to calculate the difference between two date attributes (let's say open date and close date or open date and acknowledge date) and store the result in another attribute based on either of the following type of conditions:

     

    1. Neither of the two date attributes is null

    2. Value of any flag variable is set

     

    The format should be like hh:mm:ss or any other format possible.

     

    Kindly suggest how it can be achieved.

     

    Regards,

    Balram Singh Deswal



  • 2.  Re: Difference Between Two Date Attributes

    Posted Jun 02, 2016 08:27 AM

    Hi Balram,

    This would probably require some spelcode, and possibly some javascript, which is not something that we can assist with from a support standpoint.  Its possible that some folks out here on the communities have gotten this to work, and they may be willing to share their setup with you.

    Jon I.



  • 3.  Re: Difference Between Two Date Attributes

    Posted Jun 02, 2016 08:42 AM

    Hi Jon,

     

    I will wait for the updates from them.

    Thanks for your response!

     

    Regards,

    Balram Singh Deswal



  • 4.  Re: Difference Between Two Date Attributes

    Posted Jun 02, 2016 01:29 PM

    Hi Balram,

     

    I had a similar requirement which Gutis helped me fulfill with spel code.  Check out the previous discussion linked below, it includes a .spl and .mod file that you can modify for your needs.

     

    Add custom derived read only date field



  • 5.  Re: Difference Between Two Date Attributes

    Posted Jun 09, 2016 02:09 PM

    The method mentioned in my earlier post doesn't persist the date value to the database.  It would only be useful to display the value as readonly or for use in notification templates.  We've added a duration field which stores the difference between the outage start and end dates on a CR for use in incident communication.  I wouldn't recommend my example as it seems you need to store the value in the database.



  • 6.  Re: Difference Between Two Date Attributes

    Posted Jun 02, 2016 09:20 AM

    Hello Balram,

     

    The topic is interesting. In most cases I have seen such requirements are balanced by reporting techniques as that's much easy to achieve. Now if you want to incorporate the feature in SDM here is the go :

    Consideration : Calculate the Response time on Incident

    1. Create a z_field under Call_Req table with datatype as "duration". [ zResponse (duration) ] using Web Screen Painter.

    2. Save and Publish the changes on database.(pdm_publish).

    3. Create a new display ietm in detail_in.htmpl form to bind the new database field.[ <PDM_MACRO name=dtlReadonly hdr="Response Time" attr=zResponse> ]

    3. Create a MOD file to trigger on Post_Validate on cr object if there is a status change from Open-->Acknowledge. [  MODIFY cr POST_VALIDATE zCalculate(open_date,last_mod_dt) 222 FILTER ( status { -> 'ACK'} && type {->'I'} ) ]

    4. Write a simple spel file to get the difference of these two dates  and store in a variable(zDiff). As open_date and last_mod_dt  stored in UNIXtimeformat, you will always get the difference result in seconds. Now use send_wait() method along with SURE_SET parameter to insert that zDiff value in database.

    5. Recycle CA ServiceDesk services to make the SPEL functional.

    6. The form label display of duration should come automatically in hh:mm:ss format.

     

    Try this out and let me know how it works .

    Thanks

    ArunavaS



  • 7.  Re: Difference Between Two Date Attributes

    Posted Jun 02, 2016 01:27 PM

    Hi Arunava,

     

    Thanks for the detailed steps.

     

    Can you please elaborate a bit more on how to create the spel file to get the difference along with send_wait() method usage?

     

    Regards,

    Balram Singh Deswal



  • 8.  Re: Difference Between Two Date Attributes

    Posted Jun 02, 2016 06:10 PM

    Hi, Balram I don't think that there is send_wait method for this, but I would suggest You to use workshift_abs2work method, since this method allows you calculate difference between dates taking workshift in to account, here is usage example:

     

    duration  resol_dur;
    resol_dur = workshift_abs2work("Mon - Fri { 8:00 am - 5:00 pm }", open_date, resolve_date);
    

     

    there is also another function that allows calculate date, from given start date and duration it  is called workshift_work2abs

    workshift_work2abs("Mon - Fri { 8:00 am - 5:00 pm }", open_date, resol_dur);
    

    Anyway your code should look like this

     

     uuid who; 
      duration  resol_dur;
      resol_dur = workshift_abs2work("Mon - Fri { 8:00 am - 5:00 pm }", open_date, resolve_date);
      send_wait(0,top_object(), "call_attr", "cnt", "current_user_id"); 
      who=msg[0]; 
      send_wait(0, top_object(), "call_attr", "api", "update_object_super", who, persistent_id, 0, "zresolution_time", resol_dur);
    


  • 9.  Re: Difference Between Two Date Attributes

    Posted Jun 03, 2016 03:14 AM

    Agreed with Gutis. workshift_abs2work is the CA defined method and dedicated for such calculation.



  • 10.  Re: Difference Between Two Date Attributes

     
    Posted Jun 08, 2016 05:53 PM

    Hi Balram - Did any of the responses help answer your question? If so please mark it as Correct Answer. Thanks!



  • 11.  Re: Difference Between Two Date Attributes

    Posted Jun 09, 2016 08:24 AM

    Hi everybody,

     

    I can share javascript codes for calculate seconds of workhours between two date, maybe its usefull for you

     

    // Simple function that accepts two parameters and calculates the number of hours worked within that range
    function workingHoursBetweenDates(startDate, endDate, dayStart, dayEnd, includeWeekends) 
    {
        // Store minutes worked
        var minutesWorked = 0;
      
        // Validate input
        if (endDate < startDate) { return 0; }
        
        // Loop from your Start to End dates (by hour)
        var current = startDate;
    
    
        // Define work range
        var workHoursStart = dayStart;
        var workHoursEnd = dayEnd;
    
    
        // Loop while currentDate is less than end Date (by minutes)
        while(current <= endDate){      
            // Store the current time (with minutes adjusted)
            var currentTime = current.getHours() + (current.getMinutes() / 60);
                 
            // Is the current time within a work day (and if it occurs on a weekend or not)                   
            if(currentTime >= workHoursStart && currentTime < workHoursEnd && (includeWeekends ? current.getDay() !== 0 && current.getDay() !== 6 : true)){
                  minutesWorked++;
            }
             
            // Increment current time
            current.setTime(current.getTime() + 1000 * 60);
        }
    
    
        // Return the number of hours
    
        if (minutesWorked == 0) 
      return 0 
      else 
      return ((minutesWorked * 60));
    }