Assets || How To Compare Two Attributes Which Are Of 'Date' Value Type

Platform Notice: Data Center - This article applies to Atlassian products on the Data Center platform.

Note that this knowledge base article was created for the Data Center version of the product. Data Center knowledge base articles for non-Data Center-specific features may also work for Server versions of the product, however they have not been tested. Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

The native functionalities currently lack validation and comparison options for attributes, especially for Date type. Below is a solution that enables the comparison of two date attributes for an object during its creation or update, and allowing for appropriate actions to be taken.

Sample Scenario: This solution is particularly useful when you need to compare the Start Date and End Date of an employee object, ensuring that the End Date does not precede the Start Date.


warning This solution utilizes a Groovy script, and the sample script provided below serves as a reference. Please ensure to modify it as needed and conduct thorough testing in your TEST environment prior to deployment in any critical environment, as Atlassian support does not extend to the development of custom scripts or it's implementation.

Environment

5.12.14

Solution

  1. Identify a suitable location on the JSM (Assets) server to store the Groovy script. Once you have found the location, save the scripts provided below, ensuring to make any necessary modifications beforehand.

    1. Note information  : The script currently verifies whether the object's Start Date precedes the End Date, and adding a comment accordingly. This action can be customized to send notifications, update additional attributes which can be used to trigger another automation to perform further actions as needed. 


      1. sample
        import com.atlassian.jira.component.ComponentAccessor
        import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
        import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade
        import com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory
        import com.riadalabs.jira.plugins.insight.services.model.ObjectAttributeBean
        import com.riadalabs.jira.plugins.insight.services.model.ObjectTypeAttributeBean
        import com.riadalabs.jira.plugins.insight.services.model.CommentBean
        
        def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade)
        def objectTypeAttributeFacade =ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeAttributeFacade)
        def objectAttributeBeanFactory = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectAttributeBeanFactory)
        int startDateAttrId = 120  // Replace with your actual start-date attribute ID
        int endDateAttrId = 121    // Replace with your actual end-date attribute ID
        // Load the object attributes for start-date and end-date
        def startDateAttribute = objectFacade.loadObjectAttributeBean(object.getId(), startDateAttrId)
        def endDateAttribute = objectFacade.loadObjectAttributeBean(object.getId(), endDateAttrId)
        if (startDateAttribute && endDateAttribute) {
            def startDateValue = startDateAttribute.getObjectAttributeValueBeans()[0].getValue() as Date
            def endDateValue = endDateAttribute.getObjectAttributeValueBeans()[0].getValue() as Date
            // Compare the dates
            if (endDateValue.after(startDateValue)) {
                logMessage="The start-date is correct which is prior to endDate for object ID: ${object.getId()}."
                log.info(logMessage)
            } else {
                logMessage="The start-date is INCORRECT as the end-date before the startDate for object ID: ${object.getId()}."
                log.warn(logMessage)
                // handle the logic or exception as needed. 
            }
        //adding a comment, you may opt to handle the scenario differently
        def commentBean = new CommentBean()
        commentBean.setComment(logMessage)
        commentBean.setObjectId(object.getId())
        objectFacade.storeCommentBean(commentBean)
        } else {
            log.warn("One or both date attributes are missing for object ID: ${object.getId()}.")
        }


  2. To add the script to the Allow List, navigate to Administration → Manage apps →  Insight allowlist and input the complete path. 


  3. Create an Assets automation rule (Schema → Configure → Automation), set it to trigger on object creation or update events. This rule should validate the object type condition and subsequently initiate the execution of a Groovy script.
  4. Expected results on the Object and in the logs;
    1. log
      2025-02-25 14:53:33,635 [insight-event-0] | Got rule and event for insightObjectAsyncEvent: null, id: 1 - InsightObjectEvent eventType: OBJECT_CREATED, rule id: 1, name: test-validate-date, isActive true, objectRuleEvent id: 1 and object: My Test (TESTITSM-116)
      2025-02-25 14:53:33,642 [insight-event-0] | doAction(), called for event class: InsightObjectCreatedEvent
      2025-02-25 14:53:33,642 [insight-event-0] | Object name: My Test, id: 116
      2025-02-25 14:53:33,642 [insight-event-0] | Execute Rule action (AutomationRuleGroovyScriptAction): Start, id: 1, event [id: 1, aql: No aql], condition: objectType = Employees
      2025-02-25 14:53:33,642 [insight-event-0] | GroovyScriptAction, got absFilePath: /var/atlassian/application-data/jira/shared/script/test.groovy
      2025-02-25 14:53:33,643 [insight-event-0] | Execute groovy script: /var/atlassian/application-data/jira/shared/script/test.groovy
      2025-02-25 14:53:33,851 [insight-event-0] | The start-date is INCORRECT as the end-date before the startDate for object ID: 116.
      2025-02-25 14:53:33,857 [insight-event-0] | Execute Rule action (AutomationRuleGroovyScriptAction), Result: Groovy script (test.groovy) executed.
      2025-02-25 14:53:33,857 [insight-event-0] | Execute Rule action (AutomationRuleGroovyScriptAction): Done, id:1, event [id: 1, aql: No aql, condition: objectType = Employees
      
      
      2025-02-25 14:53:49,807 [insight-event-1] | Got rule and event for insightObjectAsyncEvent: null, id: 2 - InsightObjectEvent eventType: OBJECT_UPDATED, rule id: 1, name: test-validate-date, isActive true, objectRuleEvent id: 2 and object: My Test (TESTITSM-116)
      2025-02-25 14:53:49,810 [insight-event-1] | doAction(), called for event class: InsightObjectUpdatedEvent
      2025-02-25 14:53:49,811 [insight-event-1] | Object name: My Test, id: 116
      2025-02-25 14:53:49,811 [insight-event-1] | Execute Rule action (AutomationRuleGroovyScriptAction): Start, id: 1, event [id: 2, aql: No aql], condition: objectType = Employees
      2025-02-25 14:53:49,811 [insight-event-1] | GroovyScriptAction, got absFilePath: /var/atlassian/application-data/jira/shared/script/test.groovy
      2025-02-25 14:53:49,811 [insight-event-1] | Execute groovy script: /var/atlassian/application-data/jira/shared/script/test.groovy
      2025-02-25 14:53:49,929 [insight-event-1] | The start-date is correct which is prior to endDate for object ID: 116.
      2025-02-25 14:53:49,933 [insight-event-1] | Execute Rule action (AutomationRuleGroovyScriptAction), Result: Groovy script (test.groovy) executed.
      2025-02-25 14:53:49,933 [insight-event-1] | Execute Rule action (AutomationRuleGroovyScriptAction): Done, id:1, event [id: 2, aql: No aql, condition: objectType = Employees


Disclaimer:

The information and methods described in this article are not part of our standard operating procedures and should be considered as alternative solutions. We strongly recommend testing these methods in a controlled, non-production environment before considering any implementation in a live production setting. This is to ensure compatibility with your existing systems and to prevent any unintended disruptions. Use this information at your own risk, and always ensure you have proper backups and recovery plans in place before proceeding.

Last modified on Mar 24, 2025

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.