Append database picker value to Summary

Andrew December 14, 2020

I have a Database picker field where I select the second column value of the table and that will need to be added/appended to Summary.

 

I have a code where it copies the value correctly to a custom text field, but change of requirement need to append to summary. Please see below and advise how to tweak the code. 

 

Need change to this:

def oldSummary = issue.getSummary()
final newvalue = oldSummary + " - " +valueForDbPicker?.trim()
mutableIssue.setSummary(newvalue)
issueManager.updateIssue(currentUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false);

 

 

The one which is working with text field : 

 

if (textCf2) {
def changeHolder = new DefaultIssueChangeHolder()
textCf2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(textCf2),valueForDbPicker?.trim()),changeHolder)
}
}

 

1 answer

1 accepted

0 votes
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 14, 2020

Hi @Andrew , the simple solution is to change the code to:

def oldSummary = issue.getSummary()
final newvalue = oldSummary + " - " +valueForDbPicker?.trim()
mutableIssue.setSummary(newvalue)
mutableIssue.store()

But I suggest you to update issue with IssueService:

final IssueInputParametersImpl issueInputParameters = new IssueInputParametersImpl();
issueInputParameters.setSummary(newValue);

IssueService.UpdateValidationResult updateValidationResult = issueService.validateUpdate(executingUser, issueId, issueInputParameters);
if (!updateValidationResult.isValid()) {
log.error("Update is not possible:"+updateValidationResult.getErrorCollection())
return
}

IssueService.IssueResult operationResult = issueService.update(executingUser, updateValidationResult);
if (!operationResult.isValid()) {
log.error("Issue with ID " + issueId + " update has failed:"+ operationResult.getErrorCollection());
}
Andrew December 15, 2020

Thanks Martin - it appears the listener updates only on Edit issue but not at the workflow action where I enter my PR Number. please advise

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.scriptrunner.db.DatabaseUtil
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser;

def issueManager = ComponentAccessor.getIssueManager()
def issue = event.issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def fieldLayoutManager = ComponentAccessor.getFieldLayoutManager()

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
Issue mutableIssue = issueManager.getIssueObject(event.issue.id);

 

//Get Database Picker field value
final customFieldName = 'PR Number'
def textCf1 = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName('PR Number')
//def fieldLayoutManager = ComponentAccessor.getFieldLayoutManager()
def fieldLayoutItem = fieldLayoutManager.getFieldLayout(issue).getFieldLayoutItem(textCf1.id)
def valueForDbPicker = textCf1.getViewHtml(fieldLayoutItem, null, issue) as String

//Get Summary field value
def oldSummary = issue.getSummary()

final newvalue = oldSummary + " " +valueForDbPicker?.trim()
mutableIssue.setSummary(newvalue)
mutableIssue.store()
issueManager.updateIssue(currentUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false);

Andrew December 15, 2020

++ I have selected Event  "Issue update" but tried all events and issue created along with Generic event as it fires this in the workflow.

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 15, 2020

HI @Andrew I can't understand what "it appears the listener updates only on Edit issue" means. Could you describe whole flow? Are you using listeners or postfunctions? Can you rewrite your scripts to use issueService (as I suggested)? Without it you might need to invoke issue reindex...

Andrew December 16, 2020

Hi @Martin Bayer _MoroSystems_ s_r_o__   Sorry for the confusion. let me elaborate. I couldn't use your Issue service suggestion because  I was not able to guess it is in combination of the Mutable Issue or by itself.

1. I created a Single Database Picker field and connected to a resource database connection pulling in a PR number from the results.

2.  The report from database has a Record ID, PR Number and other columns as 1st, 2nd, etc column format.

3. my SQL search string is :

select record_id, PR_Number, [R Status],ProcurementStageID,ProcurementStageDesc from [hidden][dbo].[V_Requisitions]
where lower(PR_Number) like concat(lower(?), '%')
order by PR_Number

 

4. I create a new issue in Jira with default values and then have a transition called PR Number which populates a screen with db picker field.

 

5. I use the dropdown value button and select one of the PR number that was populated from my database connection.

 

6. I save this value. I want the same value to be added to Summary by either clearing existing value or append to it. most preferably clear existing value in the summary and replace with the picker value.

 

7. I am using the Listener because at the workflow transition post function I have error referring to the value no present because the update action is not complete unless I click update right.

 

The challenge here is the Summary is getting updated via the script I sent earlier when I use Edit issue option only,  I want this to be copying to Summary at the time of workflow transition where I select the PR value from screen and not have to edit again to reflect the new  update. It seem to be registering the first column record id vs PR Number at this stage and subsequent edits are updating .

 

Hope I covered much of my requirement, I will be ok if I can just clear the Summary and replace the PR Number to Summary by educating users to make use of the dit button. if the transition does all good.

 

Thanks in advance!

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 17, 2020

Hi @Andrew it should definitelly work in postfunction. When value is filled in on the screen it can be used in postfunction (request is validated first and these values are available in validators too).

I never used the way you are getting custom field's value. Try to replace it with

//Get Database Picker field value
final customFieldName = 'PR Number'
def prNumberCF = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName(customFieldName).get(0)
def valueForDbPicker = prNumberCF.getValue(issue)

You have to use issue property which is in context of the postfunction by default.

I think complete code for postfunction should be something like this and if you put it before reindexing issue you don't even need to store it with store() method:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser;

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def fieldLayoutManager = ComponentAccessor.getFieldLayoutManager()

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

//Get Database Picker field value
final String customFieldName = 'PR Number'
def prNumberCF = customFieldManager.getCustomFieldObjectsByName(customFieldName).get(0)
def valueForDbPicker = prNumberCF.getValue(issue)

//Get Summary field value
def oldSummary = issue.getSummary()

final newvalue = oldSummary + " " +valueForDbPicker?.trim()
issue.setSummary(newvalue)

Just give it a try, check the log file in case it does not work and get back to me with the message :) 

Andrew December 18, 2020

Guess I am missing something. when I used your code in the post function script editor, it shows few red x. so I added few more line to make it go away ( I am not versed with programing, gave random lines)  but overall it wont append or replace to the Summary.

script error.PNG

 

script error2.PNG

import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.user.ApplicationUser; import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.MutableIssue def issueManager = ComponentAccessor.getIssueManager() def customFieldManager = ComponentAccessor.getCustomFieldManager() def fieldLayoutManager = ComponentAccessor.getFieldLayoutManager() def issue = issue as MutableIssue ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(); //def event = event as IssueEvent //def issue = event.issue def customFieldObjects = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)

 

//Get Database Picker field value final String customFieldName = 'PR Number' def prNumberCF = customFieldManager.getCustomFieldObjectsByName(customFieldName).get(0) def valueForDbPicker = prNumberCF.getValue(issue)

 

//Get Summary field value def oldSummary = issue.getSummary() final newvalue = oldSummary + " " +valueForDbPicker?.trim() mutableIssue.setSummary(newvalue) mutableIssue.store() issueManager.updateIssue(currentUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false);

 

Error message:

2020-12-18 10:24:40,035 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue


workflowMode=live&workflowName=TAD40%3A+Procurement+Workflow+%28Alternate%29&descriptorTab=postfunctions&workflowTransition=451&highlight=4 groovy.lang.MissingPropertyException: No such property: event for class: Script425 at Script425.run(Script425.groovy:11)
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 18, 2020

@Andrew please try to use MY code...red lines are only static code check errors but you can save it and publish the workflow. Just give it atryand let me know 🙂

Andrew December 18, 2020

Thanks Martin, this did append the value to summary same like JSU utility copy/replace post function option but if you observe my screenshot, it is not capturing the exact value that is recorded in the selection. The database query on the database picker field could be the issue but couldn't change the combinations.

script error.PNGscript error2.PNG

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 18, 2020

Can you add

log.error valueForDbPicker.grtClass()
log.error valueForDbPicker

and send the log content? What App do you use to use DB custom field? 

Andrew December 18, 2020

I think this might be the issue for me where the database was picking up the id rather than the text value. couldnt guess how to apply

https://productsupport.adaptavist.com/browse/SRJIRA-4496  

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 18, 2020

aaah, sorry @Andrew I did't ask you for CF type before, so yeah, it looks like the workaround must be used to get the value. Solution should be something like:

import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser;

def issueManager = ComponentAccessor.getIssueManager()
customFieldManager = ComponentAccessor.getCustomFieldManager()
fieldLayoutManager = ComponentAccessor.getFieldLayoutManager()

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

//Get Database Picker field value
final String customFieldName = 'PR Number'
def prNumberCF = customFieldManager.getCustomFieldObjectsByName(customFieldName).get(0)
def valueForDbPicker = getDBValue(prNumberCF, issue)

//Get Summary field value
def oldSummary = issue.getSummary()

final newvalue = oldSummary + " " + valueForDbPicker
issue.setSummary(newvalue)

def getDBValue(dbPicker, issue){
def idValue = issue.getCustomFieldValue(dbPicker) log.error("idValue = " + idValue) FieldLayoutItem fieldLayoutItem = fieldLayoutManager.getFieldLayout(issue).getFieldLayoutItem(dbPicker.id) //if the field has a value on the issue
if
(idValue) { def valueForDbPicker = dbPicker.getViewHtml(fieldLayoutItem, null, issue) as String return valueForDbPicker?.trim() } else {
return "N/A"
}
}
Andrew December 21, 2020

guess there is a problem in the code, please screen.script error3.PNG

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 21, 2020

@Andrew sorry there was missing 'new line'. i fixed the code

Andrew December 21, 2020

Thanks Martin, guess I have new error. sorry for the trouble :)

script error3.PNG

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 22, 2020

HI @Andrew it is enough to add import of the class to the beginning of the script:

import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutManager

I adjusted my script above ...

Andrew December 22, 2020

Excellent, Thank you so much Martin, this worked :)

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 23, 2020

hi @Andrew happy to help, it was hard one :D

Suggest an answer

Log in or Sign up to answer