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;

Assigning The Selected Option of Select List(single choice) Custom Field to Single Select Type Worklog Attribute Value as a Default

This script gets the select list(single choice) type custom field value defined for the issue then assign to the single select attribute value as a default value in WorklogPro dialog. Custom field id and attribute name in the corresponding lines of script are needed to be specified. This script runs in 'Before Worklog Dialog Display' script type.

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 import com.atlassian.jira.component.*; import com.deniz.jira.worklog.data.attr.AttrType; import com.deniz.jira.worklog.data.attr.AttrValue; import com.deniz.jira.worklog.services.attr.AttrTypeService; import com.deniz.jira.worklog.data.attr.AttrValueImp; import com.deniz.jira.worklog.scripting.WorklogPreEntryParameters; import com.deniz.jira.worklog.services.*; import com.atlassian.jira.issue.*; import java.util.*; import org.slf4j.*; def issueManager = ComponentAccessor.getComponent(IssueManager.class); def issue = issueManager.getIssueObject(worklogPreEntryParameters.issueKey); def attrTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(AttrTypeService.class); def attrTypes = ComponentAccessor.getOSGiComponentInstanceOfType(AttrType.class); def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager.class); def selectCustomField = customFieldManager.getCustomFieldObject(10400); //Getting custom field with custom field id def selectedOption = issue.getCustomFieldValue(selectCustomField); def singleSelectAttrId = attrTypeService.getAttrTypeWithName("Single Select Attribute").get().ID; //Getting attribute with attribute name def singleSelectAttr = worklogPreEntryParameters.attrTypes.find {element -> element.id == singleSelectAttrId}; def singleSelectAttrValues=singleSelectAttr.attributeValues; def attrNames=singleSelectAttrValues.collect {it -> it.name}; for(option in attrNames){ def AttrOption=option.toString(); if(AttrOption == selectedOption.toString()){ def attrImp=singleSelectAttrValues.find {it -> it.name==AttrOption}; attrImp.setDefaultValue(true); } }

The script above is assigning the value of the custom field of the issue to the relevant attribute values as a default before the worklog dialog was opened. However, if the issue is changed in the issue picker section while the worklog dialog was open, these values are not updated according to the selected issue. To update the attribute values according to the selected issue when the user changes the issue while the dialog is open ,the script below is needed to be used. The script below assigns the custom fields values specified with the IDs customfield_10500 and customfield_10501 to the attributes as a default by assigning the corresponding select type html elements which have IDs 'wa_10' and 'wa_11'. As with the script above, the important part here is that both the custom field and the attribute have the same values.

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.deniz.jira.worklog.scripting.WorklogPreEntryParameters; import java.util.*; def script = ''' function issueChanged() { var issueKey=AJS.$("#log-work-issue-picker").val()[0]; fetch(`/rest/api/2/issue/${issueKey}`).then(function(response) { if (response.status === 200) { response.json().then(function(data) { console.log("DATA:", data); if(data.fields.customfield_10500 !== null){ var selectedOptionDevelopmentBucket=data.fields.customfield_10500.value.toString(); console.log("selected option development bucket:", selectedOptionDevelopmentBucket); var devBucId =AJS.$("option:contains('" + selectedOptionDevelopmentBucket + "')").addClass("on").val(); console.log("devBucId:", devBucId); AJS.$("#wa_10").val(devBucId).prop('selected', true); AJS.$("#wa_10").trigger("change"); }else{ AJS.$("#wa_10").val('').prop('selected', true); AJS.$("#wa_10").trigger("change"); } if(data.fields.customfield_10501 !== null){ var selectedOptionProductBucket=data.fields.customfield_10501.value.toString(); console.log("selected option product bucket:", selectedOptionProductBucket); var prodBucId =AJS.$("option:contains('" + selectedOptionProductBucket + "')").addClass("on").val(); console.log("prodBucId:", prodBucId); AJS.$("#wa_11").val(prodBucId).prop('selected', true) AJS.$("#wa_11").trigger("change"); }else{ AJS.$("#wa_11").val('').prop('selected', true) AJS.$("#wa_11").trigger("change"); } }); } }); } AJS.$(document).on("change", "#log-work-issue-picker-field", function(evt) { setTimeout(issueChanged, 10); }); ''' worklogPreEntryParameters.jsScript = script; return worklogPreEntryParameters;