After Worklog Create/Update

You can execute a Groovy script just after usual handling of worklog create and update and perform additional logic. When this script is running, work is already logged, so there is no way to stop logging work within this script. If you need to validate and stop logging work, you can use “Worklog Verification Script”.

Removing “Work logged” on a Child Issue from “Remaining Estimate” of Parent Epic

Some organizations prefer to define “Remaining Estimate” an “Original Estimate” required for a development in the parent epic. They log work to contained issues within the epic and these issues don’t have their own “Remaining Estimate” or “Original Estimate”. They want “time spent” for these contained issues to be deducted from “Remaining Estimate” of the parent epic. This script exactly does that. You may need additional logic for your own needs.

import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.security.groups.GroupManager; import com.atlassian.jira.issue.IssueInputParameters; import com.atlassian.jira.bc.issue.IssueService; import com.atlassian.jira.bc.issue.DefaultIssueService; import com.atlassian.jira.bc.issue.worklog.WorklogInputParametersImpl.Builder; import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.issue.IssueInputParameters; import com.atlassian.jira.security.*; import org.slf4j.*; Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class); def issueManager = ComponentAccessor.getIssueManager(); def issueService = ComponentAccessor.getIssueService(); def authenticationContext = ComponentAccessor.getJiraAuthenticationContext(); def customFieldManager = ComponentAccessor.getCustomFieldManager(); def previousTimeSpent = 0; if (update) { previousTimeSpent = previousWorklog.getTimeSpent(); } def currentUser = authenticationContext.getLoggedInUser(); def issue=worklog.getIssue(); def epicLinkCf = customFieldManager.getCustomFieldObjectByName("Epic Link"); def epicIssue = issue.getCustomFieldValue(epicLinkCf); def parentObject=issue.getParentObject(); if(parentObject){ def epicIssueOfObject=parentObject.getCustomFieldValue(epicLinkCf); if(epicIssueOfObject){ //In this case,epic has a child issue and child issue has a subtask.Worklog is done for this subtask. epicIssue = epicIssueOfObject; } if(parentObject.issueType.name == 'Epic'){ //In this case,epic has a subtask.Worklog is done for this subtask. epicIssue=parentObject; } } def issueSpentTime=worklog.getTimeSpent(); def diff = issueSpentTime - previousTimeSpent; if (epicIssue) { log.debug("Subtracting {} seconds from remaining estimate of epic {}", diff, epicIssue.getKey()); def epicEstimate=epicIssue.getEstimate(); Long epicEstimateUpdated=epicEstimate-diff; if (epicEstimateUpdated < 0) { epicEstimateUpdated = 0; } IssueInputParameters issueInputParameters = issueService.newIssueInputParameters(); issueInputParameters.setRemainingEstimate((epicEstimateUpdated/60) + "m"); IssueService.UpdateValidationResult updateValidationResult = issueService.validateUpdate(currentUser, epicIssue.getId(), issueInputParameters); if(updateValidationResult.isValid()) { issueService.update(currentUser, updateValidationResult); } else { log.error("!!!Error in Worklog Update Script:{}", getErrorCollection.getErrorCollection()) } }

Setting the Account Specified with "Account Custom Field" to the Worklog Account In Case the Account Field of the Worklog is edited as 'None'

If there is an account value that is set with "Account Custom Field" to an issue, when creating the worklog to this issue, this value comes to the account field of worklog by default. Even if the account field is selected as None while creating the worklog to this issue, this value is set to worklog's account field. These are behaviours that are provided by WorklogPRO.
Also with this script, Even if account value is changed to 'None' while editing the worklog, this value is still set to account field of worklog.
In brief, If the account value is set for the issue with the "Account Custom Field", this account value is set to account field of worklog in the case that worklog's account field is set to 'None'.

import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.fields.CustomField; import com.atlassian.jira.component.ComponentAccessor; import com.deniz.jira.worklog.accounting.AccountImp; import com.deniz.jira.worklog.accounting.AccountService; import com.deniz.jira.worklog.accounting.*; import com.deniz.jira.worklog.services.*; import com.atlassian.jira.issue.*; import java.util.*; import org.slf4j.*; def accountService = ComponentAccessor.getOSGiComponentInstanceOfType(AccountService.class); def customFieldManager = ComponentAccessor.getCustomFieldManager(); def issue=worklog.getIssue(); def worklogId= worklog.id; def cField = customFieldManager.getCustomFieldObjectByName("Account Custom Field"); def cFieldValue = issue.getCustomFieldValue(cField); def cFieldAccountId=cFieldValue.id; if (!account) { accountService.addAccountToWorklog(worklogId, cFieldAccountId, worklog.startDate) }