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

How can update profield field value (status)

Karim Belhadj
Contributor
December 12, 2018

Hello ,   After installing the plug_in Profields , and creating a Status Field with name(Statut) and added it to my  project , so now i would like that when any issue transition from status (TO do) to (IN PROGRESS) , the fields of plug-in profield (Statut) must be changed to (in progress) also .  So i create a post function and i create my script there , it's not working , maybe i have a problem in the class field .Can you help me to solve it please?

 

 

 

 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.deiser.jira.profields.api.field.FieldService
import com.deiser.jira.profields.api.value.ValueService
import com.deiser.jira.profields.api.field.status.StatusField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.Project
import com.deiser.jira.profields.api.field.text.TextField
import com.deiser.jira.profields.api.value.ValueService

@WithPlugin("com.deiser.jira.profields")

 

Issue issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issue.getKey())

def valueService = ComponentAccessor.getOSGiComponentInstanceOfType(ValueService.class)
def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)

Project project = ComponentAccessor.getProjectManager().getProjectByCurrentKey("KP");

Field statusField = fieldService.get("9") //ID of field Status
log.warn(statusField.toString())

def karim = valueService.getValue(issue.projectObject,(StatusField) statusField)
log.warn(karim.toString())

valueService.setValues(project,statusField , "rafik")

 

 

 

 

 

 

 

2 answers

1 accepted

4 votes
Answer accepted
carlos.fernandez
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 12, 2018 edited

Hi Karim

I'm from DEISER and currently working in Profields product.

You have to make some changes in the script. I wrote the script from memory I hope it will work. I comment inline the changes inline, if you don't understand something, tell me please.

 

import com.atlassian.jira.component.ComponentAccessor
import com.deiser.jira.profields.api.field.item.status.StatusItemService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.deiser.jira.profields.api.field.FieldService
import com.deiser.jira.profields.api.value.ValueService
import com.deiser.jira.profields.api.field.status.StatusField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.Project
import com.deiser.jira.profields.api.field.text.TextField
import com.deiser.jira.profields.api.value.ValueService

@WithPlugin("com.deiser.jira.profields")



Issue issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issue.getKey())

def valueService = ComponentAccessor.getOSGiComponentInstanceOfType(ValueService.class)
def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)

// You should use the StatusItemService to get the status item
def statusItemService = ComponentAccessor.getOSGiComponentInstanceOfType(StatusItemService.class)

Project project = ComponentAccessor.getProjectManager().getProjectByCurrentKey("KP");

def statusField = (StatusField)fieldService.get("9") //ID of field Status
log.warn(statusField.toString())

def karim = valueService.getValue(issue.projectObject,(StatusField) statusField)
log.warn(karim.toString())

// You need to get the existing status item with the text "rafix"
def newStatusItem = statusItemService.getItemByText((StatusField)statusField, "rafik")

// Call setValue(...) instead of setValues(...) and pass the status item as third parameter
valueService.setValue(project,(StatusField)statusField,newStatusItem)

 

Regards.

Karim Belhadj
Contributor
December 12, 2018

Hello Carlos

nice to meet you ,  i tried to to excute this code , but i stopped in this line , 

// You need to get the existing status item with the text "rafix"
def newStatusItem = statusItemService.getItemByText((StatusField)field, "rafik")

The error is  [Static type checking] - The variable [field] is undeclared.

carlos.fernandez
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 12, 2018 edited

Oh sorry, change it to "statusField". I've modified the script in order to other people won't fall in the same error.

Like • Fede Baronti -Deiser- likes this
Karim Belhadj
Contributor
December 13, 2018

import com.atlassian.jira.component.ComponentAccessor
import com.deiser.jira.profields.api.field.item.status.StatusItemService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.deiser.jira.profields.api.field.FieldService
import com.deiser.jira.profields.api.value.ValueService
import com.deiser.jira.profields.api.field.status.StatusField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.Project
import com.deiser.jira.profields.api.field.text.TextField
import com.deiser.jira.profields.api.value.ValueService

@WithPlugin("com.deiser.jira.profields")

Issue issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issue.getKey())
def valueService = ComponentAccessor.getOSGiComponentInstanceOfType(ValueService.class)
def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)

// You should use the StatusItemService to get the status item
def statusItemService = ComponentAccessor.getOSGiComponentInstanceOfType(StatusItemService.class)


Project project = ComponentAccessor.getProjectManager().getProjectByCurrentKey("KP");
//return project.name

def statusField = (StatusField)fieldService.get(9)//ID of field Status
log.warn("statusField =" + statusField.name)

def statusvalue = valueService.getValue(issue.projectObject,(StatusField) statusField)
log.warn("value=" + statusvalue.toString())// Value of field Status

// YTheou need to get the existing status item with the text "EN COURS"
def newStatusItem = statusItemService.getItemByText((StatusField) statusField, "EN COURS")

// Call setValue(...) instead of setValues(...) and pass the status item as third parameter
valueService.setValue(project,(StatusField)statusField,newStatusItem)

 

 

 

When i excute this code in Console it work fine , and the statut field change , but when i make this code in a post function . Nothing happened. Can you please help me !

carlos.fernandez
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 13, 2018

In which transition is the postfunction and the order between the other postfuncions?

Karim Belhadj
Contributor
December 13, 2018

I make it in The transition start progress 

The order to my post function is 5.

Karim Belhadj
Contributor
December 13, 2018

i think the problem is in the issue key , but i dont know how to solve it .

carlos.fernandez
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 13, 2018 edited

I think you don't need this line, it's redundant:

Issue issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issue.getKey())

You already have the issue in the context 

The project "KP" is the current issue project?

Karim Belhadj
Contributor
December 13, 2018

i think that because now im executing my code in a script field , so i fix my issue to test on it , but when i left to post function script profields it dosen't work .

Have you other explication !

 

Regards

carlos.fernandez
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 13, 2018

As I edit the comment, probably you don't see this question: The project "KP" is the current issue project?

Karim Belhadj
Contributor
December 13, 2018

yes the project KP is the current issue project. but in the other hand , i would like to get project by key or other thing , because i will use this post-function in many projects.

carlos.fernandez
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 13, 2018

Ok, if you want to set the value in other project different than the issue project and you are in a postfunction, the script is like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.deiser.jira.profields.api.field.FieldService
import com.deiser.jira.profields.api.field.item.status.StatusItemService
import com.deiser.jira.profields.api.field.status.StatusField
import com.deiser.jira.profields.api.value.ValueService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.deiser.jira.profields")

def valueService = ComponentAccessor.getOSGiComponentInstanceOfType(ValueService.class)
def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)
def statusItemService = ComponentAccessor.getOSGiComponentInstanceOfType(StatusItemService.class)

// Gets the issue project, is not used for anything
Project issueProject = issue.projectObject

// Gets other project
Project project = ComponentAccessor.projectManager.getProjectObjByKey("KP")

// Gets the status field
def statusField = (StatusField) fieldService.get(9)

// This line is not used for anything
def statusValue = valueService.getValue(issueProject, (StatusField) statusField)

// Gets the status item base on a text
def newStatusItem = statusItemService.getItemByText((StatusField) statusField, "EN COURS")

// Set the value in the "other" project
valueService.setValue(project, (StatusField) statusField, newStatusItem)

 

You can see that you don't use the issue for anything, only for get the status value in its project, but you don't make anything with this value. I comment it inline.

 

Regards

Karim Belhadj
Contributor
December 13, 2018

Carlos before passing to the project key , now im blocked because i can not excute this correct code in a post function !

Some solution to that.

 

Regards

carlos.fernandez
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 13, 2018

Don't understand this:

before passing to the project key

 

And, why you can't execute the code in a postfunction? Any error?

Karim Belhadj
Contributor
December 13, 2018

Carlos can we organize a teamviwer session ! to explain to you my problem !!

carlos.fernandez
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 13, 2018

Well I can see you've created a ticket in our Jira, best we continue there

Ignacio Flores May 19, 2021

@carlos_fernandez 

Hi! 

How I can get the ID of a profield field ?

Thanks a lot!

carlos.fernandez
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, 2021

Hi @Ignacio Flores 

If you go to Profields > Fields and you hover the name of any field, a tooltip is shown with the field's ID.

Regards.

Leo Diaz _ DEISER
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 20, 2021
Ignacio Flores May 20, 2021 edited

@carlos_fernandez @Leo Diaz _ DEISER 

Thanks a lot! 

I've another question, Im tryng get the value from a GroupMultipleField, but the result only gives me this and not the value ... can you helpme?

[runner.AbstractScriptRunner]: Cel value: [com.atlassian.crowd.embedded.impl.ImmutableGroup@33916d5]

 

Here is my code: 

 

def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)
def valueService = ComponentAccessor.getOSGiComponentInstanceOfType(ValueService.class)

def project = ComponentAccessor.getProjectManager().getProjectByCurrentKeyIgnoreCase(pkey)
log.warn("project: "+project)
def CelField = fieldService.get(41)
def CelValue = valueService.getValue(project, (GroupMultipleField) CelField).toString()

carlos.fernandez
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, 2021

Hi @Ignacio Flores 

The value of a GroupMultipleField is a list of Jira Group objects.

If you need to do something with each group you can do this:

CelValue.each{ group ->

    // do something with the group

    print(group.name)

}

If you need to print the values separated by commas, you can do this:

print CelValue.collect{group-> group.name}.join(", ")

 

Hope it helps

Regards

Ignacio Flores May 24, 2021

@carlos_fernandez @Leo Diaz _ DEISER 

I tried what you mentioned, but I get the following:

 

java.png

Ignacio Flores May 24, 2021

@carlos_fernandez @Leo Diaz _ DEISER 

Thanks a lot!
I have another question, 

I will explain what I am doing:

I have a custom field where users enter the release, and from that field I want to complete another field with profield information.

From that release id I enter the project and with the project id I will look for the "profield" field, what is my problem? To validate all the possible release id and bring the "profield" field for each one of them, I must use a "FOR" which is not allowing me to save all the values ​​as one. Sorry if I don't explain myself well, thank you very much!

Here is my results and my code:

 

Results: 

TEAM_TERMINAL_FINANCIERO

TEAM_OPTIMUS

 

Expected results:

TEAM_TERMINAL_FINANCIERO , TEAM_OPTIMUS 
or

[TEAM_TERMINAL_FINANCIERO , TEAM_OPTIMUS ]

 

Here is the code:

@WithPlugin("com.deiser.jira.profields")

def ProjectManager = ComponentAccessor.projectManager
def VersionManager = ComponentAccessor.versionManager
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("DPG-1189")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cf1 = customFieldManager.getCustomFieldObjects(issue).find {it.name =="Proyecto/Release"}
def cf1val = issue.getCustomFieldValue(cf1) as List
log.warn("el valor de Celula/Release es:"+cf1val)


List<Integer> listIntegers = new ArrayList<Integer>(cf1val.size());
for(String current:cf1val){
listIntegers.add(Integer.parseInt(current));
}
int nInts = listIntegers.size();
List<Long> longs = new ArrayList<Long>(nInts);
for (int i=0;i<nInts;++i) {
longs.add(listIntegers.get(i).longValue());
}

for (int i in longs )
{
def versionObject = VersionManager.getVersion(i)
def pid = versionObject.projectId
def p = ProjectManager.getProjectObj(pid)
def pkey = p.getKey()
log.warn("pkey: "+pkey)

def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)
def valueService = ComponentAccessor.getOSGiComponentInstanceOfType(ValueService.class)

def project = ComponentAccessor.getProjectManager().getProjectByCurrentKeyIgnoreCase(pkey)

log.warn("project: "+project)
def CelField = fieldService.get(41)
def CelValue = valueService.getValue(project, (GroupMultipleField) CelField)


//log.warn("Cel value: "+CelValue)
// CelValue.each{ group ->
//log.warn(group.name)

//}

log.warn("join: "+CelValue.collect{group-> group.name}.join(", "))


}

carlos.fernandez
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 25, 2021

Hi @Ignacio Flores 

I can't see where do you try to save the value.

Please add this piece of code in order to understand the script.

Please, indicate the "type" of each Profields field and Jira custom fields to know what kind of values they store.

Regards

Ignacio Flores May 25, 2021

@carlos_fernandez @Leo Diaz _ DEISER 

Thank you very much for your prompt reply

This is a field in the issue where I will look for the release id.

 CustomField cf1 in the code:

--->

RELEASE_FIELD.png

This is the cell field, currently this field is manual and you can choose one or more of the values ​​it contains (for example: TEAM_KOMEX)

This field brings the same values ​​as the "Cell / s" field in profields, therefore this is where I want to save the value that I get from profields through the release id.

Currently I still do not implement it in the code since I still do not get the values ​​correctly from the profield field

 

--->

CELULA_FIELD.png

and this would be the profield field that I want to obtain and replicate in my issue field "Cells / s", it is of the type "group multple field" since it only brings groups in jira.

CelValue in the code.

To understand a little more the context of what is being done, the cell is the project work team, therefore we want to obtain that "cell" is working in "x" release

 

--->

PROFIELD_FIELD.png

 

so my idea is to save this result "CelValue.collect{group-> group.name}.join(", ")"  in a variable to assign it to the issue field "Cell" as List. 

But at the moment I am only rescuing one value of more possible in the "FOR loop"

I hope I have made myself understood well, thanks you!

Ignacio Flores May 25, 2021

in resume:

the problem its in CelValue.collect{group-> group.name}.join(", ")" 

The join its not working :/ only shows one value

Ignacio Flores June 9, 2021

@carlos_fernandez @Leo Diaz _ DEISER 

any comment ? thanks!

Leo Diaz _ DEISER
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 10, 2021

Hi Ignacio!

I think we are dealing with a very specific issue, could you raise a ticket so we can give you better support? 

Thanks!

Leo

0 votes
Monika Agarwal December 1, 2019

@Karim Belhadj - Is this issue resolved. I am also facing the same issue.

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, ace, atlassian community event, donation, girls who code, women in tech, malala fund, plan international, kudos, community badge, badge, atlassian badge, International Women’s month, International Women’s Day, women's month, women's day

10 for Change at Atlassian Community Events

Show up and give back by attending an Atlassian Community Event: we’ll donate $10 for every event attendee in March!

Join an Atlassian Community Event!
AUG Leaders

Upcoming Apps & Integrations Events