Help on Groovy Script to compare the customfields and update the value of the field in jira

himabindu April 28, 2023

Hi,

Here below is the api Json response to search the project where the Kickoff date is today selection

{

    "status": "OK",

    "data": [

        {

            "projectName": "iEvalAuto_47119",

            "bhuIds": "bhu_04454(FY19)",

            "handOver": null,

            "createdDate": "2019-03-20 08:43:12.0",

            "modifiedDate": "2023-04-14 06:54:23",

            "projectDates": {

                "Kick-Off_startDate": "2023-03-31",

                "Mobilization_startDate": "2023-03-30",

                "Mobilization_endDate": "2023-04-22",

                "Design_startDate": null,

                "Design_endDate": null,

                "Development_startDate": null,

                "Development_endDate": null,

                "IT_startDate": null,

                "IT_endDate": null,

                "PT_startDate": null,

                "PT_endDate": null,

                "UAT_startDate": null,

                "UAT_endDate": null,

                "RT_startDate": null,

                "RT_endDate": null,

                "Production Cut Over_startDate": null,

                "Production Go Live_startDate": null,

                "Warranty_startDate": null,

                "Warranty_endDate": null,

                "Create CR By_startDate": null,

                "Have Final Eval By_startDate": null,

                "KT_startDate": null,

                "KT_endDate": "2023-04-30"

            }

        }

    ]

}

So, Required to help on groovy script to compare each field of json response on the above with each field of jira customfield of the project. if both values are different. then update the customfield in jira project. (Note: if values are similar no need to update)

Please help to solve the issue.

 

Thanks & Regards

Himabindu.P

1 answer

0 votes
Clark Everson
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 28, 2023

Welcome to community! This should get you started but I haven't tested it so I would recommend doing it in stage and building in additional functions and verifying it works before putting it in your environment. I can not be responsible for anything that doesn't work properly.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

// Define the custom field mappings
def customFieldMappings = [
    "Kick-Off_startDate" : "customfield_10000",
    "Mobilization_startDate" : "customfield_10001",
    "Mobilization_endDate" : "customfield_10002",
     // Add the rest of the custom field mappings here
]

// The JSON response from the API
def jsonResponse = '''
{
    "status": "OK",
    "data": [
         {
            // The rest of the JSON data
         }
    ]
}
'''

// Parse the JSON response
def jsonSlurper = new groovy.json.JsonSlurper()
def jsonData = jsonSlurper.parseText(jsonResponse)
def projectDates = jsonData.data[0].projectDates

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueByCurrentKey("PROJECT-KEY") // Replace "PROJECT-KEY" with the relevant project key

if (issue) {
    customFieldMappings.each { jsonKey, customFieldId ->
         def customField = customFieldManager.getCustomFieldObject(customFieldId)
         def apiValue = projectDates[jsonKey]
         def jiraValue = issue.getCustomFieldValue(customField)

          if (jiraValue?.toString() != apiValue) {
              def changeHolder = new DefaultIssueChangeHolder()
              customField.updateValue(null, issue, new ModifiedValue(jiraValue, apiValue),      changeHolder)
         }
    }

    // Reindex the issue after updating custom fields
    issueManager.reindex(issue)
} else {
    println("Issue not found")
}

Suggest an answer

Log in or Sign up to answer