Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Need help with null check and check issuetype with single put request

Suresh Bhandiwad
Contributor
November 14, 2019

Hello Team,

Can some body help on how to do a null check in Jira cloud while using Script runner for Jira cloud on Listener. Also would like to validate if the issue type is CII.

- Issue Link Created Event

- Few fields has to be copied from parent to child when a issue is linked as duplicate

The problem is history is being updated separately for different put requests, is their way we can make it as one update. I am a beginner, however can't make it as one due to null check and need to set up destination value as well to null. 

{noformat}

// Check if the type of the issue link type of the issue is 'duplicate'
if (issueLink.issueLinkType.name == "Duplicate"){

def destinationIssueResponse = get("/rest/api/2/issue/${issueLink.destinationIssueId}").asObject(Map)
def destinationIssueKey = destinationIssueResponse.body.key
logger.info("Child Jira ticket: ${destinationIssueKey}")

def sourceIssueResponse = get("/rest/api/2/issue/${issueLink.sourceIssueId}").asObject(Map)
def sourceIssueKey = sourceIssueResponse.body.key
logger.info("Parent Jira ticket: ${sourceIssueKey}")

def parentcii = get('/rest/api/2/issue/' + destinationIssueKey)
.header('Content-Type', 'application/json')
.asObject(Map)
.body

def parentpriority = parentcii.fields.priority.name
logger.info("Priority field value: ${parentpriority} of parentcii")

def parentissuekey = parentcii.key
logger.info("Jira id: ${parentissuekey} of parentcii")

def parentissuestatus = parentcii.fields.status.name
logger.info("Issue status: ${parentissuestatus} of parentcii")

def parentissuetype = parentcii.fields.issuetype.name
logger.info("Issue type: ${parentissuetype} of parentcii")

//assignee
if (parentcii.fields.assignee != null){
def parentissueassignee = parentcii.fields.assignee.accountId
logger.info("Assignee is : ${parentissueassignee} of parentcii")

def childassignee = put('/rest/api/2/issue/' + sourceIssueKey)
.header('Content-Type', 'application/json')
.body([
fields: [
assignee:[id:parentissueassignee]
]
])
.asString()
} else {

def childassignee = put('/rest/api/2/issue/' + sourceIssueKey)
.header('Content-Type', 'application/json')
.body([
fields: [
assignee: null
]
])
.asString()
}

//Duplicate JIRA ETA & Code Drop ID – Planned
if (parentcii.fields.customfield_11961 != null){
def CodeDropIDPlanned = parentcii.fields.customfield_11961.value
logger.info("Code Drop ID - Planned: ${CodeDropIDPlanned} of parentcii")

def dupETAupdate = put('/rest/api/2/issue/' + sourceIssueKey)
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_12130:CodeDropIDPlanned,
customfield_11961:[value:CodeDropIDPlanned]
]
])
.asString()
} else {

def dupETAupdate = put('/rest/api/2/issue/' + sourceIssueKey)
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_12130: null,
customfield_11961: null
]
])
.asString()
}

//Defect Category
if (parentcii.fields.customfield_11200 != null){
def DefectCategory = parentcii.fields.customfield_11200.value
logger.info("Defect Category: ${DefectCategory} of parentcii")

def Estdateupdate = put('/rest/api/2/issue/' + sourceIssueKey)
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_11200:[value:DefectCategory]
]
])
.asString()
} else {

def Estdateupdate = put('/rest/api/2/issue/' + sourceIssueKey)
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_11200: null
]
])
.asString()
}

//Estimated Close Date
if (parentcii.fields.customfield_11402 != null){
def EstimatedCloseDate = parentcii.fields.customfield_11402.value
logger.info("Estimated Close Date: ${EstimatedCloseDate} of parentcii")

def Estdateupdate = put('/rest/api/2/issue/' + sourceIssueKey)
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_11402:EstimatedCloseDate
]
])
.asString()
} else {

def Estdateupdate = put('/rest/api/2/issue/' + sourceIssueKey)
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_11402: null
]
])
.asString()
}

//Initiating Dept
if (parentcii.fields.customfield_11403 != null){
def InitiatingDept = parentcii.fields.customfield_11403.value
logger.info("Initiating Dept: ${InitiatingDept} of parentcii")

def deptupdate = put('/rest/api/2/issue/' + sourceIssueKey)
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_11403:[value:InitiatingDept]
]
])
.asString()
} else {

def deptupdate = put('/rest/api/2/issue/' + sourceIssueKey)
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_11403: null
]
])
.asString()
}

//Lifecycle
if (parentcii.fields.customfield_11903 != null){
def Lifecycle = parentcii.fields.customfield_11903.value
logger.info("Lifecycle: ${Lifecycle} of parentcii")

def Lifecycleupdate = put('/rest/api/2/issue/' + sourceIssueKey)
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_11903:[value:Lifecycle]
]
])
.asString()

} else {

def Lifecycleupdate = put('/rest/api/2/issue/' + sourceIssueKey)
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_11903: null
]
])
.asString()
}

def childciiupdate = put('/rest/api/2/issue/' + sourceIssueKey)
.header('Content-Type', 'application/json')
.body([
fields: [
priority:[name:parentpriority],
customfield_12128:parentissuekey,
customfield_12129:parentissuestatus,
customfield_12133:parentissuetype
]
])
.asString()
}

{noformat}

1 answer

0 votes
Kristian Walker _Adaptavist_
Community Champion
November 14, 2019

Hi Suresh,

Thank you for your question.

I can confirm that each put rest API call your script will add a separate record into the issue change history and that this is how Jira Cloud is designed to work.

Currently, as you are making your put requests based on If/else statements then it will not be possible to have one single put request to update all the fields as the values you are setting depends on the value returned by each if statement.

This means that you will have to leave your script as it is and will have to have multiple entries in the change history for each update due to the fact that this is how Jira Cloud is designed.

As for making a null check, I can confirm that the way you are doing it with an If statement to check if the field value equals null is the correct approach to use.

Finally, in order to check if the issue type is CII then I can confirm that you should be to check the Issue map for the issue that you return when you make a request to the /rest/api/2/issue/ API.

This map will contain a parameter called issuetype which is the issue type of the issue can check if this value matches the CII issue type that you require.

Finally, I can confirm one trick to check what fields are returned by an issue is to run the Get Issue Fields example script which you will see in the  examples section on the Script Console page as this will allow you to easily see what fields are returned for an issue so you can see how to check their values.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer