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

* 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?


Answers:
author: Tuncay Senturk
body: Hi [@Eis3an](/t5/user/viewprofilepage/user-id/5391064)

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

comments: author: Eis3an
body: 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


author: Tuncay Senturk
body: 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?


author: Eis3an
body: 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)


author: Tuncay Senturk
body: It means that you need to write a script to achieve this.


author: Eis3an
body: 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


author: Tuncay Senturk
body: 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
        }
      })
   });
}

```


author: Anna Hryhoruk _Appfire_
body: Hello [@Eis3an](/t5/user/viewprofilepage/user-id/5391064) ,

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


Tags: cloud

jira-cloud