Update System Field Without Updating History

Joshua Kohn July 8, 2021

Does anyone know how via groovy I can update in bulk a ton if ticket without it affecting the Last Updated date field. I have the below code that DOES update the last updated date.

Can you please help me figure this out.

Thank you!
_____________
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def issueManager = ComponentAccessor.getIssueManager()

def ProjectKeytoUpdate = "TPA"

for (int key = 22104; key <=22104; key++) {
Issue issueObject = issueManager.getIssueObject(ProjectKeytoUpdate + "-" + key)

if(issueObject && issueObject.key.take(ProjectKeytoUpdate.length()) == ProjectKeytoUpdate){

CustomField RequesterField = customFieldManager.getCustomFieldObjects(issueObject).find {it.name == "Requester"}

ApplicationUser RequesterUser = issueObject.getCustomFieldValue(RequesterField) as ApplicationUser

if(RequesterField){
ApplicationUser ReporterUser = issueObject.reporter as ApplicationUser

if (ReporterUser != RequesterUser){
try{
issueObject.setReporter(RequesterUser)

issueManager.updateIssue(RequesterUser, issueObject, EventDispatchOption.DO_NOT_DISPATCH, false)

def changeHolder = new DefaultIssueChangeHolder()
ComponentAccessor.getComponent(IssueIndexingService).reIndex(issueObject)

log.warn issueObject.key + " | Copyed field " + RequesterField.name + " to Reporter with value of " + RequesterUser + " (Orignal Reporter Value: " + ReporterUser + ")"
}catch (all){
log.warn issueObject.key + " | Failed to copy field " + RequesterField.name + " to Reporter with value of " + RequesterUser
}
} else {
log.warn issueObject.key + " | Fields have the same values. Requester: " + RequesterUser + " to Reporter with value of " + RequesterUser + " (Orignal Reporter Value: " + ReporterUser + ")"
}
}
}
}

 

It is my understanding that I should be using the below code to do so but this is to update a custom field and not a system field.

// custom field reference
CustomField myCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(MY_CUSTOM_FIELD_ID);

// construct the new value
ModifiedValue newValue = new ModifiedValue(issue.getCustomFieldValue(myCustomField), "new value");
// update the field to "new value"
myCustomField.updateValue(null, issue, newValue, new DefaultIssueChangeHolder());

2 answers

1 accepted

2 votes
Answer accepted
Joshua Kohn July 13, 2021

So I opened a ticket with Atlassian and Adaptavist. Basically the answer is that this can't be done through scripting. Since the Reporter field is tied to the ticket itself if you update the field you update the ticket. Since custom fields are there own identify and just liked to the ticket you can update the field without updating the ticket itself.

At the end of the day, I updated the database and then ran a re-index on the tickets that I updated. (I provided the code below. (But if you need to use my code to copy my work, then maybe you shouldn't be updating the database.))

Answer from Adaptavist:

I understand that you like to bulk update the system field value on Jira issues without updating the issue history and the Updated Date. Please correct me if I'm wrong.

The sample script in our library here shows bulk updating the value of the system field for all issues returned by a JQL query, but it's updating the issue history as well.

To update the system field without updating the issue history, the alternative I could think of is using the mutableIssue.store() method, which is deprecated. You can refer to the sample script in this community post.

--------------------------------

Atlassian provides few ways to update an issue using Jira Java API. Each method will save the changes to the history, except for the CustomField.updateValue() method, while this method updates the issue instead of stores the issue in the data store. However, as you're aware, this method can be used for setting custom fields' values only.

Here is the simple comparison between the Java API methods for your references:

Jira Java API to update issues

getIssueManager().updateIssue()

You can set the values of the system fields and the custom fields with this method, and the changes will be saved to the issue history. This method stores the provided issue to the Jira datastore and performs no permission checks. 

CustomField.updateValue()

You can only set the values of the custom fields with this method while the changes will not be saved to the issue history. This method updates the issue in the data store.

IssueService.update()

You can set the values of the system fields and the custom fields with this method, and the changes will be saved to the issue history. This method stores the provided issue to the Jira datastore. 

This method makes sure that all of Jira's business rules are enforced in which the permissions and data validation will be checked, proper events will be fired, and notifications will be triggered when dealing with Jira Issue's. 

 

MSSQL Code:

UPDATE
ji
SET
ji.REPORTER = cfv.STRINGVALUE
-- SELECT ji.ID as 'Ticket ID', p.pkey, CONCAT(p.pkey,'-',ji.issuenum) AS 'Ticket Number', ji.REPORTER, cfv.STRINGVALUE AS 'Requester'
FROM
[Jira].[dbo].[jiraissue] ji
INNER JOIN [Jira].[dbo].[project] p
ON ji.PROJECT = p.ID
INNER JOIN [Jira].[dbo].[customfieldvalue] cfv
ON cfv.ISSUE = ji.id
INNER JOIN [Jira].[dbo].[customfield] cf
ON cf.ID = cfv.CUSTOMFIELD
WHERE
p.pkey in ('xxx')
and cf.ID = 10307
and ji.id = cfv.ISSUE
and cfv.STRINGVALUE NOT LIKE 'ID%'
and ji.REPORTER <> cfv.STRINGVALUE
and cfv.STRINGVALUE is not null
ORDER BY cfv.STRINGVALUE ASC

 

Saurabh Gupta July 18, 2022

Thanks for the detailed answer.

0 votes
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 9, 2021

Hi @Joshua Kohn I know about oldschool method which stores an issue. I have no idea whether it updates the Updated date and it is deprecated but if you're on test environment, just give it a try and let me know :)

https://docs.atlassian.com/software/jira/docs/api/8.18.0/com/atlassian/jira/issue/MutableIssue.html#store--

You should remove issueManager.updateIssue of course.

Suggest an answer

Log in or Sign up to answer