Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do i duplicate to another custom field an action that was performed manually using post function

Eis3an June 27, 2024
  • an action was performed. ie a value added to a field 
  • this value must be added to another field automatically. 
  • cannot use copy custom field value since that copies all the values but i only want the change copied or the new action implemented to be duplicated.
  • is there a post function template that can be used or do i have to script it, how to go about the scripting?

2 answers

1 vote
Tuncay Senturk
Community Champion
June 28, 2024

Hi @Eis3an 

I'm sorry, but I'm having trouble understanding your exact needs. Could you please provide some solid examples to clarify?

Eis3an June 30, 2024

Hello,
Supposing i have a custom field of multi select. It contains 5 values (users) at the time of creation. Afterwards I edit this field at a certain transition to add 2 or more value to the field. Now i need a post function on this transition that will copy only the added users to another custom field.
This other custom field has a list of users (users working on the issue) already that should not be changed, only appended. 

I hope am clear. Thanks for the response

Tuncay Senturk
Community Champion
July 1, 2024

I see,

Is the other custom field a text field where you only need an explanation of the change, or is it another multi-select field that only contains the newly added options? How about removing items? Lastly, if this transition is called multiple times, will it override?

Eis3an July 2, 2024

the other custom field is a list that contains some permanent users that are working on this field that should not be bothered. in addition to these permanent users optional users that are added further during transitions should get appended or removed based on the initial custom field that list of new users invited to work. adding users has been solved using post function copy field value.

removing users is the tricky part. 

 

yes this transition can be called multiple times. 

(any users invited can decline even after they have accepted so their names once they removed from initial field must be automatically removed from other field without disturbing the permanent and other values in the second field)

 

Tuncay Senturk
Community Champion
July 3, 2024

It means that you need to write a script to achieve this.

Eis3an July 3, 2024

yes my good sir i figured as much. i need help with the scripting in post functions. 

mainly how to reference deleted field values. then compare it with another list to calculate the difference between the two lists then append 

Tuncay Senturk
Community Champion
July 3, 2024

Hi,

I can't say I understand 100% but, from my understanding, I tried to scratch something which might help you start with.

I hope it helps...


async function updateUserList(issue) {
const initialField = issue.fields['customfield_XXXXX'] || [];
const secondaryField = issue.fields['customfield_YYYY'] || [];

const permanentUsers = ["permanent_user_1", "permanent_user_2"];

async function getUserDetails(userKey) {
const response = await api.asApp().requestJira(`/rest/api/3/user?key=${userKey}`);
return await response.json();
}

const permanentUserDetails = await Promise.all(permanentUsers.map(user => getUserDetails(user)));

const initialUserSet = new Set(initialField.map(user => user.accountId));
const secondaryUserSet = new Set(secondaryField.map(user => user.accountId));
const permanentUserSet = new Set(permanentUserDetails.map(user => user.accountId));

const usersToAdd = [...initialUserSet].filter(user => !secondaryUserSet.has(user));
const usersToRemove = [...secondaryUserSet].filter(user => !initialUserSet.has(user) && !permanentUserSet.has(user));

const updatedSecondaryUsers = [
...permanentUserDetails, // Add permanent users
...secondaryField.filter(user => !usersToRemove.includes(user.accountId)), // Keep existing users minus those to remove
...usersToAdd.map(user => ({ accountId: user }))
];

await api.asApp().requestJira(`/rest/api/3/issue/${issue.key}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
fields: {
[secondaryFieldId]: updatedSecondaryUsers
}
})
});
}

 

0 votes
Anna Hryhoruk _Appfire_
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.
July 3, 2024

Hello @Eis3an ,

Do you have the Power Scripts app? If not, would you consider using that one for this use case? I think it`s doable using Power Scripts and SIL scripting language. It can be done as a post function or even SIL Listener that reacts to field value change (without connection to any particular transition). Please let me know if that`s something you are interested in, and I`ll help you with the script itself.

Best regards,
Anna

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
AUG Leaders

Atlassian Community Events