Post script to update assignee if customfield empty or unassigned

David Mallon May 14, 2014

Hi, we have a workflow transition which includes a post function to use "copy from one field to another" and takes the value from a custom field "tester" and populates the field "assingnee"

However when the field "tester" is empty the Jira issue becomes "unassigned". I would like to be able to have the Jira issue, during the same transition, copy the value from the "test manager" field to the "assignee" field but only if the "tester" field is empty. which I guess is the same as if the issue is unassigned. I have tried it with groovy but cant get it to work.

Thanks in advance

8 answers

1 vote
BenjiI
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 Leaders.
May 14, 2014

Hi David,

You can write your own custom groovy script that does all these steps for you. It will probably be something like (can contain some syntax errors):

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue; 
 
 
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
CustomField testerField = customFieldManager.getCustomFieldObjectByName( "tester" );
CustomField testManagerField = customFieldManager.getCustomFieldObjectByName( "testManager" );
 
Object testerValue = issue.getCustomFieldValue(testerField);
Object testManagerValue = issue.getCustomFieldValue(testManagerField);

if(testerValue != null) {
	issue.assignee = testerValue;
} else if(testManagerValue != null) {
	issue.assignee = testManagerValue; 
} else {
	issue.assignee = null;
}

You can use the script by adding a Script Post-Function to your transition that runs the custom script (uploaded to the server).

Hope this helps?!

BenjiI
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 Leaders.
May 14, 2014

It is best to use User Picker custom field for your tester and testManager custom fields. This fields make it easier for the user to select a person (compared to a classic text field) and the value returned is usually already an object (f.e User) instead of a normal String. This can keep your scripts much shorter.

webspin May 15, 2014
Hi Benji  thans so mch, I get this error, what should I do, thanks
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.issue.Issue.getCustomFieldValue() is applicable for argument types: (null) values: [null]
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
Benji Mommen [ACA IT] May 15, 2014

Hi webspin,

Is it possible that the variables testerField and testManagerField are null, because those customfields were never created and/or added to the issue screens?

Also make sure that you are using issue (lowercase, which is a variable) and not Issue (which is a class).

BenjiI
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 Leaders.
May 18, 2014

Hi webspin,

Is it possible that the variables testerField and testManagerField are null, because those customfields were never created and/or added to the issue screens?

Also make sure that you are using issue (lowercase, which is a variable) and not Issue (which is a class).

BenjiI
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 Leaders.
May 20, 2014

Hi David,

If DNM1 is indeed a username string you can try this updated script:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue; 
import com.atlassian.jira.component.ComponentAccessor;
  
  
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
CustomField testerField = customFieldManager.getCustomFieldObjectByName( "tester" );
CustomField testManagerField = customFieldManager.getCustomFieldObjectByName( "testManager" );
  
Object testerValue = issue.getCustomFieldValue(testerField);
Object testManagerValue = issue.getCustomFieldValue(testManagerField);
ApplicationUser defaultAssignee =  ComponentAccessor.getUserManager().getUserByName("DNM1");
 
if(testerValue != null) {
    issue.assignee = testerValue;
} else if(testManagerValue != null) {
    issue.assignee = testManagerValue; 
} else {
    issue.assignee = defaultAssignee.getDirectoryUser();
}

webspin May 21, 2014
Hi Benji still no joy, I appreciate your time
regards
David m
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script221.groovy: 16: unable to resolve class ApplicationUser 
 @ line 16, column 17.
   ApplicationUser defaultAssignee =  ComponentAccessor.getUserManager().getUserByName("DNM1");
                   ^

1 error
BenjiI
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 Leaders.
May 21, 2014

Hi David,

This is because some import statements were missing (f.e ApplicationUser). Can you check this update:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.crowd.embedded.api.User;
   
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
CustomField testerField = customFieldManager.getCustomFieldObjectByName( "tester" );
CustomField testManagerField = customFieldManager.getCustomFieldObjectByName( "testManager" );
   
Object testerValue = issue.getCustomFieldValue(testerField);
Object testManagerValue = issue.getCustomFieldValue(testManagerField);
ApplicationUser defaultAssignee =  ComponentAccessor.getUserManager().getUserByName("DNM1");
  
if(testerValue != null) {
    issue.assignee = testerValue;
} else if(testManagerValue != null) {
    issue.assignee = testManagerValue; 
} else {
    issue.assignee = defaultAssignee.getDirectoryUser();
}

0 votes
BenjiI
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 Leaders.
May 20, 2014

Hi David,

Is DNM1 the name of a user in the form of a String?

0 votes
webspin May 20, 2014
 
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
 ...;
 ....;
 ...;
 .....;
 ....;
 ....;
 ....; 
 
 
  = .().();
  = .(;
  = .(;
 
  = .();
  = .()
 
{ ( != ) {
	. = ;
}  ( != ) {
	. = ; 
}  {
	. = ;
}
}
Stack Trace:
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script210.groovy: 24: expecting ')', found '' @ line 24, column 2.
   }
    ^

1 error
 
Hi, can anyone please let me know what is wrong with this
Thanks
David m
0 votes
BenjiI
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 Leaders.
May 15, 2014

Hi webspin,

Is it possible that the variables testerField and testManagerField are null, because those customfields were never created and/or added to the issue screens?

Also make sure that you are using issue (lowercase, which is a variable) and not Issue (which is a class).

0 votes
Benji Mommen [ACA IT] May 15, 2014
Hi webspin, Try using issue instead if Issue (lower case)
0 votes
webspin May 15, 2014
Hi Benji  thans so mch, I get this error, what should I do, thanks
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.issue.Issue.getCustomFieldValue() is applicable for argument types: (null) values: [null]
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
0 votes
David Mallon May 14, 2014

Workflow post function

The field Assignee will take the value from Tester. Source and destination issue are the same.

Update parameters of the Script Post-Function Function for this transition.

Update parameters of the Script Post-Function Function for this transition.
Selected Script: Fast-track transition an issue : If the condition is met, automatically transition this issue to another status
Script Info

If the condition is met, automatically transition this issue to another status
Loaded from: bundle://133.0:1/com/onresolve/jira/groovy/canned/workflow/postfunctions/FasttrackTransition.groovy (View Source)

Condition
 
1
 
. == .

Enter the condition for which this function will fire. Blank will evaluate to "true". You can click one of the examples below, or see the wiki page for further examples.
Action <select name="cannedScriptArgs_FIELD_ACTION"> <option value="831 Send To Quality Analysis"> Send To Quality Analysis (831)</option> <option value="21 Accept">Accept (21)</option> <option value="101 Accept Amendment">Accept Amendment (101)</option> <option value="91 Amend">Amend (91)</option> <option value="1081 Applied to prod - close">Applied to prod - close (1081)</option> <option value="891 Assign Tester / Testing Estimate">Assign Tester / Testing Estimate (891)</option> <option value="1011 Attach Prod Patch File">Attach Prod Patch File (1011)</option> <option value="1101 BI, DI or DW Build">BI, DI or DW Build (1101)</option> <option value="771 Backlog">Backlog (771)</option> <option value="781 Backlog">Backlog (781)</option> <option value="811 Backlog">Backlog (811)</option> <option value="841 Build Admin Edit">Build Admin Edit (841)</option> <option value="991 Build Admin edit">Build Admin edit (991)</option> <option value="1031 Build admin edit">Build admin edit (1031)</option> <option value="1061 Build admin edit">Build admin edit (1061)</option> <option value="1171 Build admin edit">Build admin edit (1171)</option> <option value="821 Build admin edit">Build admin edit (821)</option> <option value="1191 Close - In Production">Close - In Production (1191)</option> <option value="2 Close Issue">Close Issue (2)</option> <option value="701 Close Issue">Close Issue (701)</option> <option value="31 Complete">Complete (31)</option> <option value="121 Completed">Completed (121)</option> <option value="1 Create Issue">Create Issue (1)</option> <option value="761 Current Iteration">Current Iteration (761)</option> <option value="791 Current Iteration">Current Iteration (791)</option> <option value="1111 DBA/Build Notes">DBA/Build Notes (1111)</option> <option value="1121 DBA/Build Notes">DBA/Build Notes (1121)</option> <option value="1131 DBA/Build Notes">DBA/Build Notes (1131)</option> <option value="1141 DBA/Build Notes">DBA/Build Notes (1141)</option> <option value="1151 DBA/Build Notes">DBA/Build Notes (1151)</option> <option value="1161 DBA/Build Notes">DBA/Build Notes (1161)</option> <option value="1251 DBA/Build Notes">DBA/Build Notes (1251)</option> <option value="831 DBA/Build Notes">DBA/Build Notes (831)</option> <option value="841 DBA/Build Notes">DBA/Build Notes (841)</option> <option value="851 DBA/Build Notes">DBA/Build Notes (851)</option> <option value="861 DBA/Build Notes">DBA/Build Notes (861)</option> <option value="871 DBA/Build Notes">DBA/Build Notes (871)</option> <option value="881 DBA/Build Notes">DBA/Build Notes (881)</option> <option value="81 Discontinue">Discontinue (81)</option> <option value="721 Done">Done (721)</option> <option value="841 Done">Done (841)</option> <option value="1241 Failed QA and Return to Developer">Failed QA and Return to Developer (1241)</option> <option value="791 Failed QA and Return to Developer">Failed QA and Return to Developer (791)</option> <option value="821 Failed QA and Return to Developer">Failed QA and Return to Developer (821)</option> <option value="751 Further developer work required">Further developer work required (751)</option> <option value="961 Further developer work required">Further developer work required (961)</option> <option value="741 In Progress">In Progress (741)</option> <option value="751 In Progress">In Progress (751)</option> <option value="1271 Passed QA and Send to BI Build ">Passed QA and Send to BI Build (1271)</option> <option value="801 Passed QA and Send to BI Build">Passed QA and Send to BI Build (801)</option> <option value="801 Passed QA and Send to Build ">Passed QA and Send to Build (801)</option> <option value="1291 Passed QA and Send to Notes Build ">Passed QA and Send to Notes Build (1291)</option> <option value="811 Passed QA and Send to Notes Build">Passed QA and Send to Notes Build (811)</option> <option value="1231 Passed QA and Send to e-Sys Build">Passed QA and Send to e-Sys Build (1231)</option> <option value="781 Passed QA and Send to e-Sys Build">Passed QA and Send to e-Sys Build (781)</option> <option value="1281 Passed QA and Send to eBiz Build ">Passed QA and Send to eBiz Build (1281)</option> <option value="791 Passed QA and Send to eBiz Build ">Passed QA and Send to eBiz Build (791)</option> <option value="941 Prod Patch Applied">Prod Patch Applied (941)</option> <option value="1261 Quality Analysis ">Quality Analysis (1261)</option> <option value="811 Quality Analysis ">Quality Analysis (811)</option> <option value="771 Quality Analysis">Quality Analysis (771)</option> <option value="51 Re-Draft">Re-Draft (51)</option> <option value="131 Re-Open">Re-Open (131)</option> <option value="61 Re-Open">Re-Open (61)</option> <option value="41 Reject">Reject (41)</option> <option value="1071 Release Manager Edit">Release Manager Edit (1071)</option> <option value="3 Reopen Issue">Reopen Issue (3)</option> <option value="11 Request Acceptance">Request Acceptance (11)</option> <option value="1041 Resolve Issue">Resolve Issue (1041)</option> <option value="111 Resolve Issue">Resolve Issue (111)</option> <option value="5 Resolve Issue">Resolve Issue (5)</option> <option value="971 Return to Build Admin">Return to Build Admin (971)</option> <option value="1001 Return to Developer">Return to Developer (1001)</option> <option value="731 Return to Developer">Return to Developer (731)</option> <option value="951 Return to Developer">Return to Developer (951)</option> <option value="1021 Return to with build for Prod Patch">Return to with build for Prod Patch (1021)</option> <option value="741 Send to BI Build ">Send to BI Build (741)</option> <option value="1221 Send to BI Build">Send to BI Build (1221)</option> <option value="1091 Send to BPM Build ">Send to BPM Build (1091)</option> <option value="831 Send to BPM Build ">Send to BPM Build (831)</option> <option value="1211 Send to Notes Build">Send to Notes Build (1211)</option> <option value="731 Send to Notes Build">Send to Notes Build (731)</option> <option value="721 Send to Testing">Send to Testing (721)</option> <option value="751 Send to Testing">Send to Testing (751)</option> <option selected="selected" value="711 Send to e-Sys Build">Send to e-Sys Build (711)</option> <option value="1201 Send to ebiz Build">Send to ebiz Build (1201)</option> <option value="721 Send to ebiz Build">Send to ebiz Build (721)</option> <option value="4 Start Progress">Start Progress (4)</option> <option value="301 Stop Progress">Stop Progress (301)</option> <option value="741 Testing Complete">Testing Complete (741)</option> <option value="761 Testing Complete">Testing Complete (761)</option> <option value="921 Testing Passed Apply to Prod">Testing Passed Apply to Prod (921)</option> <option value="901 To Build for Prod Patch">To Build for Prod Patch (901)</option> <option value="931 To DBA for Prod Patch">To DBA for Prod Patch (931)</option> <option value="911 To Testing for Prod Patch">To Testing for Prod Patch (911)</option> <option value="981 UAT Prod Patch ">UAT Prod Patch (981)</option> <option value="1181 UAT Prod Patch">UAT Prod Patch (1181)</option> <option value="771 Update Restricted Fields">Update Restricted Fields (771)</option> <option value="781 Update Restricted Fields">Update Restricted Fields (781)</option> <option value="1201 Withdraw from Build">Withdraw from Build (1201)</option> <option value="821 close with edit">close with edit (821)</option> <option value="1051 purge resolution">purge resolution (1051)</option> </select>
ID of the workflow action that will be applied to this issue.
Additional code
 
1
 
.()

Additional script to run before the issue is stored. Use this to modify fields or add a comment etc.
0 votes
Bhushan Nagaraj
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 Leaders.
May 14, 2014

Hi David,

Could you please share your current script?

Suggest an answer

Log in or Sign up to answer