How to fill in an Insight custom field with current user (customer in JSD)

Deleted user December 3, 2018

Hi guys,

First of all, I have to say I am new in Groovy.

I would like to use Scriptrunner Behaviours to fill in an Insight custom field (named "Persona afectada") with the current user (customer who has an account in JSD), when raising a request in the customer portal.

After browsing a lot, I get this code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.*

if (getActionName() != "Create Issue") {
    return // not the initial action, so don't set default values
}

// set Custom Field Persona afectada
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def person = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Persona afectada")
underlyingIssue.setCustomFieldValue(person,currentUser)

 

However, I get this error:

[Static type checking] - Cannot find matching method

com.atlassian.jira.issue.Issue#setCustomFieldValue(com.atlassian.jira.issue.field

com.atlassian.jira.user.ApplicationUser). PLease check if the declared type is

correct and if the method exists.

@Line 11, column 1.

 

I would appreciate any help. Thanks in advance.

Cheers,

Almu

 

 

 

 

2 answers

1 vote
Vasiliy Zverev
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.
December 3, 2018

Dear Almu, hi!

Try this code for set custom field value:

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser();
CustomField person = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Persona afectada");
underlyingIssue.setCustomFieldValue(person, currentUser); 
Deleted user December 4, 2018

Hi Vasiliy,

Thanks a lot for your help.

This is my code now:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.*

if (getActionName() != "Create Issue") {
    return
}

// set Custom Field Persona afectada
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
CustomField person = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Persona afectada");
underlyingIssue.setCustomFieldValue(person, currentUser.name);

 

The error is:

[Static type checking] - Cannot find matching method

com.atlassian.jira.issue.Issue#setCustomFieldValue(com.atlassian.jira.issue.field

java.lang.String). Please check if the declared type is correct and if the method exists.

@Line 13, column 1.

 

I wonder what import is missing? Or if underlyingIssue is correct, since I need to fill in the custom field in the Create Screen.

Any idea, please?

Cheers,

Almu

Vasiliy Zverev
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.
December 4, 2018

What is the type of Persona afectada? 

If it is a single user type, then you should use: underlyingIssue.setCustomFieldValue(person, currentUser);

Deleted user December 4, 2018

Hi Vasiliy,

If I use "underlyingIssue" , I get the matching method error.

It is an Insight custom field. Now I've got a code without error:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueFieldConstants.*

if (getActionName() != "Create Issue") {
    return
}

// set Custom Field Persona afectada
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
getFieldById("Persona afectada").setFormValue(currentUser)

 

But it seems it does not do anything, I mean, I log into the customer portal, but the "Persona afectada" is not automatically filled in, when raising a request.

Any idea, please?

I appreciate your interest. Thanks a lot.

Cheers,

Almu

Deleted user December 4, 2018

Hi again

I have created a single user custom field, named "Persona" with this code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueFieldConstants.*

if (getActionName() != "Create Issue") {
    return
}

// set Custom Field Persona afectada
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
getFieldById("Persona").setFormValue(currentUser)

 

And I do not get error, but it seems it does not do anything either :-(

Vasiliy Zverev
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.
December 4, 2018
Deleted user December 4, 2018

Hi Vasiliy,

I have good and bad news :-)

Good news -> with this code:

import com.atlassian.jira.component.ComponentAccessor

if (getActionName() != "Create Issue") {
    return
}

// set Custom Field
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def customField = getFieldByName("Persona")
customField.setFormValue(currentUser.name)

 

In the create screen in Jira UI, the "Persona" custom field is automatically  filled in. Good!

On the other hand:

Bad new 1: in the customer portal (this is Jira Service Desk) it does not work, any idea to fix it, please?

Bad  new 2:  with "Persona afectada" Insight (Riada addon) custom field , it does not work, even in the Jira UI. I'll ask Riada team, and Adaptavist team.

Thanks for your patience.

I appreciate any help to move this on.

Cheers,

Almu

Vasiliy Zverev
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.
December 5, 2018

You could try to define class, which this custom fields uses.

For this try to get value for this field on any issue and get it class name.

0 votes
Deleted user December 5, 2018

Hi guys,

import com.atlassian.jira.component.ComponentAccessor

if (getActionName() != "Create Issue") {
    return
}

// set Custom Field
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def customField = getFieldByName("Persona")
customField.setFormValue(currentUser.name)

 In the create screen in Jira UI, the "Persona" custom field is automatically  filled in. Good!

On the other hand:

-in the customer portal (this is Jira Service Desk) it does not work. It seems to me it only works if the user comsumes a license, so this does not work for customers :-(

Or have you got a way, please?

 

-Insight (Riada addon) custom field. Riada support has told me:

All Insight custom fields at the moment regardless of the field type (read only, normal,deprecated,..etc) do not support calculated custom fields on an edit/create screen dialogue. Meaning that if you setup a custom field to auto-populate a value like your example here to auto-populate the user value in an Insight attribute if it's equal to currentUser() the custom field will show this value only on the Jira issue view. So in the Jira UI  (edit and create dialogues) and portal this will not work as you want it.

Cheers,

Almu

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events