Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Behaviours Script/script post function to set a insight custom field based on part of the summary

Florian Schüller
Contributor
October 23, 2018

dear community,

 

i need your help to write a behaviour script or a custom script post-function which will set a insight custom field based on the first part of the summary when ticket is created. 

 

The summary looks like "CI-52005 is disabled."

 

CI-52005 is the key from the insight field which will be set.

Does anyone have some ideas or similar requests?

 

best regards,

florian

2 answers

0 votes
Florian Schüller
Contributor
October 25, 2018

hi @Gundlupet Sreenidhi

 

First i tried it with an insight field (but i think this will not work)

So i decided to create a new customfield (type string single line) but it didnt work.

 

customfield.PNG

Florian Schüller
Contributor
October 29, 2018

hi @Gundlupet Sreenidhi

 

today i tried it again with a normal string field. unfortunately it didnt work.

the custom script  post function is set in the workflow transition "create" after the function "create issue" (screenshot)

 

i dont know whats wrong.. hmhm

 

workflow_transistion.PNG

0 votes
Gundlupet Sreenidhi
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.
October 23, 2018

Try this. 

 

Lets assume customfield_11111 is the ID for your customfield

if(getFieldById("summary").getValue().toString().startsWith("CI-52005")){
   getFieldById("customField_11111").setFormValue("CI-52005")
}

 The above code snippet works only for your example shown above.

 

If you would like to genaralise it for any summary ,first retrieve the substring from the summary you want to set, then set the value - shown below

 

String identifiedValue = getFieldById("summary").getValue().toString().subString(0, getFieldById("summary").getValue().toString().indexOf(" "))
getFieldById("customfield_11111").setFormValue(identifiedValue)

 

What I am doing here is getting the substring from the summary - from position 0 (start of string) till the space is encounters (i.e: the first word. Then assigning it to the form value of your custom field. 

Hope this helps. 

Florian Schüller
Contributor
October 23, 2018

Thanks a lot!

 

unfortunately i got an error after the first ticket was created.

I created a custom script post function on the workflow transition "create" with your script.

Then i tried to create a ticket but this error message returned:

 

Time (on server): Tue Oct 23 2018 14:22:19 GMT+0200 (Mitteleuropäische Sommerzeit)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-10-23 14:22:19,853 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-10-23 14:22:19,855 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: LC-187, actionId: 1, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getFieldById() is applicable for argument types: (java.lang.String) values: [summary]
 at Script136.run(Script136.groovy:1)

 

Something i forgot to do?

Gundlupet Sreenidhi
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.
October 23, 2018

What I provided is a behavior script - not a post-function.

getFieldById("summary") should work on a behavior. Can you validate?

Gundlupet Sreenidhi
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.
October 23, 2018

If you would like it to be part of the post-function, try this:

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue

import com.atlassian.jira.component.ComponentAccessor

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()


CustomField setValueField= customFieldManager.getCustomFieldObject("customfield_11111")
String setValueFieldVal = issue.getCustomFieldValue(setValueField) as String
log.info "current Field Value :"+setValueFieldVal

String summary = issue.getSummary()
String identifiedValFromSummary = summary.substring(0,summary.indexOf(" "))
issue.setCustomFieldValue(setValueField, identifiedValFromSummary);

 

Florian Schüller
Contributor
October 23, 2018

Thanks!

 

This Script is running, but the field wasnt set. The custom field is an insight field. I also tried it with a normal jira custom field.

Do you have some ideas why the value wasnt set?

Gundlupet Sreenidhi
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.
October 23, 2018

Can you explain a little of what is an insight field? This is not part of JIRA (nor any plug-in I have worked with).

Is it something provided by some other plug-in?

Try to implement logging to see where it is going wrong.

I have update the same post-function snipped above with logging:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue

import com.atlassian.jira.component.ComponentAccessor

import org.apache.log4j.Category

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()


CustomField setValueField= customFieldManager.getCustomFieldObject("customfield_11111")
String setValueFieldVal = issue.getCustomFieldValue(setValueField) as String
log.info "current Field Value :"+setValueFieldVal

String summary = issue.getSummary()
log.info "Summary: "+summary

String identifiedValFromSummary = summary.substring(0,summary.indexOf(" "))
log.info "identifiedValFromSummary : "+ identifiedValFromSummary

issue.setCustomFieldValue(setValueField, identifiedValFromSummary);


 

Once you have the logger in place, you should be able to see the logs in the post-function. 

(After you perform the action / transition, open the postfunction in edit mode to see the log)

image.png

 

Click on any of the status (shows only latest 15 logs) to see the details:

 

image.png

Florian Schüller
Contributor
October 23, 2018

thanks for the log.

 

i have updated the script and created a ticket. the custom field wasnt set.

Then i showed the execution informations about the logs.

there were no logs found.

 

Insight custom Fields: 

Fields which are connected to the addon Insight (Riada)

But its also ok if i could set the value in a normal custom field.

 

log.PNG

Gundlupet Sreenidhi
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.
October 23, 2018

Did you initialize the logging functionality for your script?

Can you show what you have in your script?

Florian Schüller
Contributor
October 24, 2018

hi!

sorry for the circumstances, but i am really new into that. Thank u for your help!

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()


CustomField setValueField= customFieldManager.getCustomFieldObject("customfield_11009")
String setValueFieldVal = issue.getCustomFieldValue(setValueField) as String
log.info "current Field Value :"+setValueFieldVal

String summary = issue.getSummary()
log.info "Summary: "+summary

String identifiedValFromSummary = summary.substring(0,summary.indexOf(" "))
log.info "identifiedValFromSummary : "+ identifiedValFromSummary

issue.setCustomFieldValue(setValueField, identifiedValFromSummary);

Gundlupet Sreenidhi
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.
October 24, 2018

Hi @Florian Schüller

Do you know what type of field the insight field is ?

Your code looks similar to mine.

The one reason I guess it may not be working is if the type is a mismatch. 

What I have does so far is to try and set a String value to a custom field(assuming it accepts Strings). If that is not the case, then we need to set the exact type of value to our custom field. 

Do you by chance know what type it is?

One way to find out: go to Administration --> Issues --> Custom Fields

Search for the Insight field and send see the type.

 

Example: 

image.png

Suggest an answer

Log in or Sign up to answer