Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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
TAGS
Community showcase
Published in Jira Software

How to create Jira issus from Excel file?

When to use CSV importer When managing your processes in Jira, there are many occasions where you need to create a lot of tasks. Creating them one by one will cost you a lot of time and effort and i...

4,633 views 22 33
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you