Forums

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

Need help to setCustomFieldValue for a Date type

Sri Vulli February 7, 2018

I don't get any errors on the ScriptRunner console, but the date on the custom field does not get updated. Please help

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import java.sql.Timestamp

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def dateCf = customFieldManager.getCustomFieldObject("customfield_12112")
def mutableIssue = ComponentAccessor.getIssueManager().getIssueObject("ABC-123")
def updated = mutableIssue.getUpdated()

mutableIssue.setCustomFieldValue(dateCf, new Timestamp((new Date() - 7).time))
mutableIssue.setUpdated(updated)
mutableIssue.store()

 

1 answer

1 accepted

1 vote
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 8, 2018

try like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import java.sql.Timestamp

import com.atlassian.jira.event.type.EventDispatchOption



def customFieldManager = ComponentAccessor.getCustomFieldManager()
def dateCf = customFieldManager.getCustomFieldObject("customfield_12112")
def mutableIssue = ComponentAccessor.getIssueManager().getIssueObject("ABC-123")

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def updated = mutableIssue.getUpdated()

mutableIssue.setCustomFieldValue(dateCf, new Timestamp((new Date() - 7).time))
mutableIssue.setUpdated(updated)
mutableIssue.store()

ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
Sri Vulli February 8, 2018

Thanks a lot Alexey. This worked.

I have about100 Jira issues that I need to update. Can I put this in a JQL and run it?

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 8, 2018

The function will be like that:

def findIssues(String jqlQuery) {
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.jiraAuthenticationContext.user
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)

def query = jqlQueryParser.parseQuery(jqlQuery)
def results = searchProvider.search(query, user, PagerFilter.unlimitedFilter)
results.issues.collect
{ issue -> issueManager.getIssueObject(issue.id) }
}

Then you ca use it like this

def query = jqlQueryParser.parseQuery(jqlQuery)
Sri Vulli February 10, 2018

Can you help me fix the following? I am running this through ScriptRunner console. I am updating a text field but do not want to change the Updated date/time. It updates the text field but also updates the last update time.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import java.sql.Timestamp
import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def name = customFieldManager.getCustomFieldObject("customfield_10217")
def mutableIssue = ComponentAccessor.getIssueManager().getIssueObject("ABC-123")
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def updated = mutableIssue.getUpdated()
mutableIssue.setCustomFieldValue(name, "John")

mutableIssue.setUpdated(updated)
mutableIssue.store()
ComponentAccessor.getIssueManager().updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)

 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 10, 2018

I do not understand exactly what you mean. First of all, if you update an issue with

ComponentAccessor.getIssueManager().updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)

Then Jira will automatically change the update time of the issue. I guess, you do not want it. Then you have to change it back to the time you want. Then your script would look like this.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import java.sql.Timestamp
import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def name = customFieldManager.getCustomFieldObject("customfield_10217")
def mutableIssue = ComponentAccessor.getIssueManager().getIssueObject("ABC-123")
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def updated = mutableIssue.getUpdated()
mutableIssue.setCustomFieldValue(name, "John")
ComponentAccessor.getIssueManager().updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)
mutableIssue.setUpdated(updated)
mutableIssue.store()

 I am not sure, if I understood your question correctly. Such manipulation seems odd, because I have never met such a requirement before. Moreover, if you have an audit division at work, which checks the history of each issue, you could have problems with what you do, because you are changing the history of an issue. Update time in your case.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events