Forums

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

Register work from transition script

Pedro J. Acosta
May 22, 2020

Hello everyone,
I am creating a script that allows a certain user to register jobs on behalf of another user (Scrennshot_1), this script seems to work correctly but when defining the user, this is not displayed in the same way as when the user is the That logs the job in the request detail (Screenshot_2), what am I doing wrong to get users to show up like that?

The script I am running is as follows:

import com.atlassian.jira.issue.worklog.WorklogImpl
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.security.JiraAuthenticationContext

def componentManager = ComponentManager.getInstance()
def componentAccesor = ComponentAccessor.getIssueManager()
def workLogManager = ComponentAccessor.getWorklogManager()
def issueManager = ComponentAccessor.getIssueManager()

//def issue = componentAccesor.getIssueObject("TELEFIN-1")

def logsForIssue = workLogManager.getByIssue(issue)

def size = 0

if(logsForIssue == null || logsForIssue.size() > 0){
size = logsForIssue.size()-1
}

def workloggs = workLogManager.getById(logsForIssue[size].id)

CustomField cfUsuario = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Usuario")
JiraAuthenticationContext jiraAuthenticationContext = ComponentManager.getComponentInstanceOfType(JiraAuthenticationContext.class)
ApplicationUser user = jiraAuthenticationContext.getLoggedInUser()

def usuario = issue.getCustomFieldValue(cfUsuario)?(ApplicationUser)issue.getCustomFieldValue(cfUsuario):user

workLogManager.delete(user, workloggs, null, false)

def wli = new WorklogImpl(null, issue, 0, usuario.name, null, workloggs.startDate, null, null, workloggs.getTimeSpent())
workLogManager.create(user, wli, null, false)

issue.setCustomFieldValue(cfUsuario, null)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false);

 

 

Screenshot_2.pngScreenshot_1.png

Regards.

1 answer

1 accepted

1 vote
Answer accepted
PD Sheehan
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 Champions.
May 22, 2020

Try usuario.key instead of usuario.name in the WorklogImpl constructor.

With newer versions of jira, user.key and user.name are not always the same and the contructor says it;s expecting "authorKey"

https://docs.atlassian.com/software/jira/docs/api/8.5.1/com/atlassian/jira/issue/worklog/WorklogImpl.html

Pedro J. Acosta
May 25, 2020

Thank you very much Peter, it works correctly with the change you have suggested to me.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events