How to populate custom field value based on if else criteria using Automation
Asked by Laxmi Swamy:

Hi Community Members,

I have created two custom fields, customfield_1 (which can store single value from the list - High, Medium, Low) and another customfield_2.

I am trying to populate customfield_2 (number data type) using the Automation based on the values stored in customfield_1.

Automation rule is successfully running without any errors but customfield_2 is not getting populated with the required output value.

Please let me know the correct syntax or any other way that I can achieve the required logic without going for any third party plugins.

Thanks in advance.

Here is the alternate JSON codes tried in Advance field editing:

{ "fields": {

"customfield_2": "{{#if(equals(issue.fields.customfield_1, \\"High\\"))}}50{{/}} {{#if(equals(issue.fields.customfield_1, \\"Medium\\"))}}25{{/}} {{#if(equals(issue.fields.customfield_1, \\"Low\\"))}}10{{/}} {{#else}}0{{/}}"

}

}

-----

{ "fields": {

"customfield_2": "{{#if(equals(issue.fields.customfield_1, "High"))}}50{{/}} {{#if(equals(issue.fields.customfield_1, "Medium"))}}25{{/}} {{#if(equals(issue.fields.customfield_1, "Low"))}}10{{/}} {{#else}}0{{/}}"

}

}

-----

{ "fields": {

"customfield_2": "{{#if(equals(issue.customfield_1, \\"High\\"))}}50{{/}} {{#if(equals(issue.customfield_1, \\"Medium\\"))}}25{{/}} {{#if(equals(issue.customfield_1, \\"Low\\"))}}10{{/}} {{#else}}0{{/}}"

}

}

-----

{ "fields": {

"customfield_2": "{{#if(equals(issue.customfield_1, "High"))}}50{{/}} {{#if(equals(issue.customfield_1, "Medium"))}}25{{/}} {{#if(equals(issue.customfield_1, "Low"))}}10{{/}} {{#else}}0{{/}}"

}

}


Answers:
author: Tommaso Gionfriddo _Teklada_
body: Hi [@Laxmi Swamy](/t5/user/viewprofilepage/user-id/2704783)

Welcome to the Atlassian Community!

I would also recommend avoiding using the advanced field json, however I think that the most future proof option is to use the (relatively new) lookup table functionality to define the relationship between the value in customfield_1 and customfield_2.

This allows for up to 200 possible values in customfield_1 in only 3 steps.

1. **When** : Value changes for *customfield_1*
2. **Then** : Create lookup table  
   ![Screenshot 2024-06-23 at 10.58.17.png](https://community.atlassian.com/t5/image/serverpage/image-id/331274i09EE1747FAC6F26F/image-dimensions/251x243?v=v2 "Screenshot 2024-06-23 at 10.58.17.png")
3. **Then** : Edit issue.
   1. Field: customfield_2
   2. Value: {{relationship.get(issue.customfield_1)}}

comments: author: Laxmi Swamy
body: Thanks [Tommaso,](https://community.atlassian.com/t5/user/viewprofilepage/user-id/3696717) tried the above solution it works like a charm. The only thing I need to cover is if I want to assign '0' value in case if the customfield_1 has blank/empty value (instead of High, Medium, Low values) as it is an optional field for the users to enter.


author: Tommaso Gionfriddo _Teklada_
body: Hi [@Laxmi Swamy](/t5/user/viewprofilepage/user-id/2704783) ,

For this base case you can simply add a default to the smart value, as per [this documentation](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-issues/#Default-values). The value will be output when nothing is returned by relationship.get().

Your edit issue value should now be {{relationship.get(issue.customfield_1)\|0}}


author: Laxmi Swamy
body: Thanks [Tommaso](https://community.atlassian.com/t5/user/viewprofilepage/user-id/3696717), for your support.


author: Jovin
body: Hi [@Laxmi Swamy](/t5/user/viewprofilepage/user-id/2704783)

Can I recommend something a bit simpler? Rather than putting the code directly into the JSON, use more of the visual aspects to create automation flows. Here is what I would do for your solution.

1. **Trigger:**Whatever you desire
2. **Condition: IF ELSE**
   1. **IF Condition:** Issue fields condition
      1. Field: customfield_1
      2. Condition: equals
      3. Value: High (it should be a dropdown for you)
   2. **THEN: Add an action:** Edit issue
      1. Field to edit: customfield_2
      2. Value: 50
   3. **ELSE Condition:** Issue fields condition
      1. Field: customfield_1
      2. Condition: equals
      3. Value: Medium (it should be a dropdown for you)
   4. **THEN: Add an action:** Edit issue
      1. Field to edit: customfield_2
      2. Value: 25
   5. **ELSE Condition:** Issue fields condition
      1. Field: customfield_1
      2. Condition: equals
      3. Value: Low (it should be a dropdown for you)
   6. **THEN: Add an action:** Edit issue
      1. Field to edit: customfield_2
      2. Value: 10

Following this your customfield_2 will be updated :)

comments: author: Laxmi Swamy
body: Thanks [Jovin,](https://community.atlassian.com/t5/user/viewprofilepage/user-id/5104734) for your support. The above solution is working as expected.


author: Joseph Chung Yin
body: [@Laxmi Swamy](/t5/user/viewprofilepage/user-id/2704783) -

To supplement [@Jovin](/t5/user/viewprofilepage/user-id/5104734) suggested, can you verify that the customfield2 is exposed to your project in question (meaning the context for the custom field is configurated properly).

I would recommend the triggering event should be "Field value changed" for all issue operations. In your rule, then setup the If/else condition to check for customfield1 value and then edit issue option to set the customfield2 value.

Hope this also helps.

Best, Joseph Chung Yin

comments: author: Laxmi Swamy
body: Thanks Joseph, I will use the same. Does it also covers the use case for Issue Creation as well.


author: Joseph Chung Yin
body: It should. However, you may want to add "Re-fetch issue data" action before your edit issue action.

Hope this helps. If my suggestion helped you, please click on "Accept answer" when you have a chance, so others with similar asks will see this posting as an answered one.

Best, Joseph


Tags: cloud

jira-service-management

jira-service-management-cloud