Pre Worklog Entry Scripts

These scripts are run just before “Log Work Dialog” or “Log Work Custom Field” is displayed and allows you to define preset values or modify worklog attributes.

 

Hide Worklog Attribute Depending on Issue Custom Field Value

Following script checks value of a “Single Select” custom field and if value of this field is “Development” it removes “Requirement Analysis” value from “Type of Work” worklog attribute.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import com.atlassian.jira.component.*; import com.atlassian.jira.issue.*; import org.slf4j.*; //please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class); def issueManager = ComponentAccessor.getComponent(IssueManager.class); def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager.class); def issue = issueManager.getIssueObject(worklogPreEntryParameters.issueKey); log.debug("issue:{}", issue); def selectCustomField = customFieldManager.getCustomFieldObject(10901L); def selectedOption = issue.getCustomFieldValue(selectCustomField); //instance of com.atlassian.jira.issue.customfields.option.Option log.debug("Selected Option:{}", selectedOption); if (selectedOption.optionId == 10404) { //if you want you can also check selectedOption.value == "Development" log.debug("Removing 'Requirement Analysis' from 'Type of Work' options"); def workTypeAttr = worklogPreEntryParameters.attrTypes.find {it.name == "Type of Work"}; if (workTypeAttr != null) { def reqAnalysisWorkAttr = workTypeAttr.attributeValues.find {it.name == "Requirement Analysis"}; if (reqAnalysisWorkAttr != null) { workTypeAttr.attributeValues.remove(reqAnalysisWorkAttr) } } } return worklogPreEntryParameters;

 

Hide Remaining Estimate

Following script injects a JavaScript to Log Work Dialog and this JavaScript code sets remaining estimate strategy to “Auto” and hides “remaining estimate” section of worklog dialog.

1 2 3 4 5 worklogPreEntryParameters.jsScript = ''' AJS.$("#log-work-adjust-estimate-auto").prop("checked", true); AJS.$("#wp-fg-estimates").hide(); '''; return worklogPreEntryParameters;

 

Set Default Value of Worklog Attribute Depending on User

Following script set default value of “Invoice Number” attribute to “12345” if current user is “admin”.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import com.atlassian.jira.component.*; import com.atlassian.jira.security.*; def authContext = ComponentAccessor.getJiraAuthenticationContext(); def loggedInUser = authContext.getLoggedInUser(); def invoiceNumberAttr = worklogPreEntryParameters.attrTypes.find {it.name == "Invoice Number"}; def defaultInvoice = ""; if (loggedInUser.key == "admin") { defaultInvoice = "12345" } if (invoiceNumberAttr != null) { script = ''' var $invoiceNumber = AJS.$("#wa_%s"); if ($invoiceNumber.val() === "") { $invoiceNumber.val("%s"); } '''; worklogPreEntryParameters.jsScript = String.format(script, invoiceNumberAttr.id, defaultInvoice); } return worklogPreEntryParameters;

 

Depending on User’s Group Sets Default Value of a Worklog Attribute

Assume that we have an attribute “Work Location” that can have one of the two values: On-site or Off-shore. The default is On-site. The following script checks if a user is in the group “all-Onsite” and if not, it sets the attribute value as Off-shore on the dialogue.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 import com.atlassian.jira.component.*; import com.atlassian.jira.issue.*; import org.slf4j.*; import com.deniz.jira.worklog.*; import com.atlassian.jira.security.JiraAuthenticationContext; import com.atlassian.jira.security.roles.ProjectRoleManager; import com.atlassian.jira.security.groups.GroupManager; //please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class); def issueManager = ComponentAccessor.getComponent(IssueManager.class); def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager.class); def issue = issueManager.getIssueObject(worklogPreEntryParameters.issueKey); def workTypeAttrUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getName(); def groupManager = ComponentAccessor.getComponent(GroupManager); if(!groupManager.getGroupNamesForUser(workTypeAttrUser).contains('all-Onsite')) { // assumption is that all-Onsite only contains list of associates who are at onsite log.debug("Offshore team member"); def workTypeAttr = worklogPreEntryParameters.attrTypes.find {it.name == "Work Location"}; if (workTypeAttr != null) { def offSiteAttr = workTypeAttr.attributeValues.find {it.name == "Off-site"}; if (offSiteAttr != null) { offSiteAttr.setDefaultValue(true); } } } return worklogPreEntryParameters;

 

Hide a Worklog Attribute Depending on User’s Group

Following script checks “user group” of current user and show “Invoice Number” worklog attribute only if user is member of “finance-users” user group.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import com.atlassian.jira.component.*; import com.atlassian.jira.issue.*; import org.slf4j.*; import com.deniz.jira.worklog.*; import com.atlassian.jira.security.JiraAuthenticationContext; import com.atlassian.jira.security.roles.ProjectRoleManager; import com.atlassian.jira.security.groups.GroupManager; def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getName(); def groupManager = ComponentAccessor.getComponent(GroupManager); def invoiceNumberAttr = worklogPreEntryParameters.attrTypes.find {it.name == "Invoice Number"}; if (invoiceNumberAttr != null) { if(!groupManager.getGroupNamesForUser(currentUser).contains('finance-users')) { def script = ''' var $invoiceNumber = AJS.$("#wa_%s"); $invoiceNumber.hide(); ''' worklogPreEntryParameters.jsScript = String.format(script, invoiceNumberAttr.id); } } return worklogPreEntryParameters;

 

Making WorklogPRO’s “Log Work” Dialog Very Similar To Jira’s Own “Log Work” Dialog

In this script we are removing every WP specific items such as “issue picker”, “user selector”, “period selector”, “navigator” and “copy comment to issue” feature. Note that, you don’t need to remove “Account” and “Worklog Attributes” because they are only visible if you create them. So, if you don’t want them, don’t create them.

1 2 3 4 5 6 7 8 9 worklogPreEntryParameters.jsScript = ''' var $dialog = AJS.$("#add-worklog-dialog-timesheet"); $dialog.find("nav").remove(); $dialog.find("#wp-fg-issue").remove(); $dialog.find("#wp-fg-author").remove(); $dialog.find("#wp-fg-period").remove(); $dialog.find("#wp-fg-copy-to-comments").remove(); '''; return worklogPreEntryParameters;

Change Default Value of Account if Issue Has Specific Key

This script changes account of an issue if issue key has specific value. You can modify it to check user, user-group, project, roles etc. It only searches accounts at first level for simplicity. If you want to set a child account, you need to make a recursive search for the account. If you now account ID, you can directly use it instead of searching it.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import com.atlassian.jira.component.*; import com.atlassian.jira.issue.*; import org.slf4j.*; //please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class); def issueManager = ComponentAccessor.getComponent(IssueManager.class); def issue = issueManager.getIssueObject(worklogPreEntryParameters.issueKey); log.debug("issue:{}", issue); if (issue.key == "UT-7") { def account = worklogPreEntryParameters.accounts.find {it.name == "IT Spendings"} if (account != null) { log.debug("found account = " + account); def script = ''' var $accountSelect = AJS.$("#wp-account-select"); $accountSelect.val(%s); $accountSelect.trigger("change") ''' worklogPreEntryParameters.jsScript = String.format(script, account.id); } } return worklogPreEntryParameters;

Setting Default Values for Account & Attributes Depending on Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 import com.atlassian.jira.component.*; import com.atlassian.jira.issue.*; import org.slf4j.*; //please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class); def script = ''' var projectKeys = ["ERP"]; function getProjectKey() { var issueKey=AJS.$("#log-work-issue-picker").val()[0]; if (issueKey) { return issueKey.split("-")[0].trim(); } return "NO Project Key"; } function setDefaults() { var projectKey = getProjectKey(); if (projectKeys.indexOf(projectKey) == -1) { AJS.log("Non-special project:" + projectKey + "!"); return; } AJS.log("handling project:"+projectKey); //setting default account var $accountSelect = AJS.$("#wp-account-select"); var primaSolutionStandardAccountId = $accountSelect.find("option:contains(Prima Solutions Standard)").val(); $accountSelect.val(primaSolutionStandardAccountId); $accountSelect.trigger("change") //seting Chargeability var $chargeabilitySelect = AJS.$("#wa_8"); var noChargeId = $chargeabilitySelect.find("option:contains(Non-Charge)").val(); $chargeabilitySelect.val(noChargeId); $chargeabilitySelect.trigger("change") } AJS.$(document).on("change", "#log-work-issue-picker-field", function(evt) { setTimeout(setDefaults, 10); }); ''' worklogPreEntryParameters.jsScript = script; return worklogPreEntryParameters;

 

Make Copy to Issue Comments Checked Depending on Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 import com.atlassian.jira.component.*; import com.atlassian.jira.issue.*; import org.slf4j.*; //please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class); def script = ''' var projectKeys = ["ERP", "ITSD"]; //Change this line and add your project keys here. function getProjectKey() { var issueKey=AJS.$("#log-work-issue-picker").val()[0]; if (issueKey) { return issueKey.split("-")[0].trim(); } return "NO Project Key"; } function setDefaults() { var projectKey = getProjectKey(); if (projectKeys.indexOf(projectKey) == -1) { AJS.log("Non-special project:" + projectKey + "!"); return; } AJS.log("handling project:"+projectKey); AJS.$("#wp-copy-to-issue-comments").click(); } AJS.$(document).on("change", "#log-work-issue-picker-field", function(evt) { setTimeout(setDefaults, 10); }); ''' worklogPreEntryParameters.jsScript = script; return worklogPreEntryParameters;

Set Time Spent Field to a Default Value

1 worklogPreEntryParameters.timeSpent = "10m"

 

Get Issue Details Every Time When Issue Picker Changes

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 def script = ''' function issueChanged() { var issueKey=AJS.$("#log-work-issue-picker").val()[0]; console.log("Issue key:" + issueKey); fetch("/rest/api/2/issue/ERP-75").then(function(response) { if (response.status === 200) { response.json().then(function(data) { console.log(data); }); } }); } AJS.$(document).on("change", "#log-work-issue-picker-field", function(evt) { setTimeout(issueChanged, 10); }); ''' worklogPreEntryParameters.jsScript = script; return worklogPreEntryParameters;