Database Picker Field - Scriptrunner
Asked by Jeyanth Kumar:

Hello,

I am trying to create a DB picker field which has multiple values. Currently I need these values to restrict based on projects. Is there any way to do this ?


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Jeyanth Kumar](/t5/user/viewprofilepage/user-id/5542211)

Welcome to the community!

In your description, you mentioned:-
> I am trying to create a DB picker field which has multiple values. Currently I need these values to restrict based on projects. Is there any way to do this ?

I am going to assume that you want the DB Picker options to be filtered according to the Project it is applied in. Please clarify if this is what you are trying to achieve.

If yes, this is doable; the Best approach would be to use ScriptRunner's Behaviour.

Let me know so I can provide an example.

Thank you and Kind regards,  
Ram

comments: author: Jeyanth Kumar
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

Thanks for replying. I am aware of using scriptrunner behaviour but I need to use Database picker field and I have nearly 500 projects in Jira. So the purpose is basically, I need 500 values from DB picker field and this is again filtered in each project. So basically the DB picker should show either 1 or 2 values for each project.

Hope my requirement is clear for you. Thanks in advance

Best regards

Jeyanth


author: Jeyanth Kumar
body: I have to use an SQL like this.

select a.col5

from a.tab1, b.tab2

where a.col3=b.col5 and a.col2 = 12406 and b.col3 = {project}

order by a.col5

Here in this SQL col3 from table b is projectkey which is based dynamically.

Hope this helps.


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Jeyanth Kumar](/t5/user/viewprofilepage/user-id/5542211)

Please share a screenshot of your DB picker configuration.

From the query structure you have shared this will not work on the Retrival and Search SQL field.

It's more for the display value.

Thank you and Kind regards,

Ram


author: Jeyanth Kumar
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

Please find the screenshot.

![SR 2.png](https://community.atlassian.com/t5/image/serverpage/image-id/337196i2F664E2435F97D15/image-size/large?v=v2&px=999 "SR 2.png")![SR 1.png](https://community.atlassian.com/t5/image/serverpage/image-id/337197i114EF764D4B1EE3B/image-size/large?v=v2&px=999 "SR 1.png")

This is my current configuration in DB picker field. But I am able to see all the values but when I choose the value. It says missing ID in preview.

Best regards,

Jeyanth


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Jeyanth Kumar](/t5/user/viewprofilepage/user-id/5542211)

Thank you for getting back and sharing the screenshots.

I can confirm the approach you are taking will not work.

Firstly, the structure of both the SQL Queries in the Retrieval / Validation and Search SQL Fields must be the same, i.e. if the Retrieval / Validation fields query starts with:-

```
SELECT customValue, ID from CUSTOMFIELDOPTION
```

the Search SQL field must also start with the same structure. Else it will not work.

If you intend to modify the displayed output, then you will need to use the Configuration Script field, i.e. [Customizing the Search SQL](https://docs.adaptavist.com/sr4js/latest/features/script-fields/built-in-script-fields/database-picker/database-picker-customizations#customizing-the-search-sql) to manage the display of the output.

If you need more info, let me know, and I'll prepare an example.

Thank you and Kind regards,  
Ram


author: Jeyanth Kumar
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

Thanks again for replying back. It would be great if you can help me writing the below query in both retrival/validation and search SQL.

Basically, I have 2 tables and they have a similar column col3 and col5 and adding 2 conditions col2 = fixed value and col3 from table 2 = fixed value from the project

select a.col5

from a.tab1, b.tab2

where a.col3=b.col5 and a.col2 = 12406 and b.col3 = {project}

order by a.col5

Best regards,

Jeyanth


author: Jeyanth Kumar
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015)

Please find the exact query that needs to be used in both retrival/validation and search SQL below

select pt.value  
from AO_00B950_AOPROJECT_VALUE pt, PROJECT pj  
where FIELD_ID=5 and pj.ID=pt.PROJECT_ID and pj.pkey={project}  
order by pt.value

Best regards

Jeyanth


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Jeyanth Kumar](/t5/user/viewprofilepage/user-id/5542211)

In your last comment, you mentioned:-
> Basically, I have 2 tables and they have a similar column col3 and col5 and adding 2 conditions col2 = fixed value and col3 from table 2 = fixed value from the project

What do you intend to display in the DB Picker?

The query you shared in your last comment will not work.

The Select statement must have 2 columns. Ideally, the first will be an ID column, and the Second will be the value you intend to display in the DB Picker field.

I am looking forward to your feedback.

Thank you and Kind regards,  
Ram


author: Jeyanth Kumar
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

Let me give an example here. Lets say I have a DB picker field called "Countries". Where it as 10 countries as values. But I have 'n' of projects where I use this field. which should display specific value for specific project. For project A it should display UK, US and for project B it should display India, China. The same is applicable for every project in Jira.

So in my case I need DB picker to display restricted value based on projects.

Best regards,

Jeyanth


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Jeyanth Kumar](/t5/user/viewprofilepage/user-id/5542211)

For your requirement, using the Configuration Script of the DB picker is the best approach.

Below sample working code taken from [Adaptavist's code snippets](https://bitbucket.org/Adaptavist/workspace/snippets/Lz75Bg/example-db-picker-configuration-script-to).  

```
import com.atlassian.jira.issue.Issue
import com.onresolve.scriptrunner.canned.jira.fields.editable.database.SqlWithParameters

def columnName = 'Country_Name'

getSearchSql = { String inputValue, Issue issue, originalValue ->

    if (issue.projectObject.name == 'Mock') {
        new SqlWithParameters("""
        select ID, COUNTRY_NAME from COUNTRIES
        where ${columnName} in (?,?,?)
        """, ['India', 'United States', 'United Kingdom'])

    } else if (issue.projectObject.name == 'Example') {
        new SqlWithParameters("""
        select ID, COUNTRY_NAME from COUNTRIES
        where ${columnName} in (?,?)
        """, ['Malaysia', 'Singapore'])
    } else if (issue.projectObject.name == 'Sample') {
        new SqlWithParameters("""
        select ID, COUNTRY_NAME from COUNTRIES
        where ${columnName} in (?,?)
        """, ['Japan', 'Australia'])
    }
}

renderViewHtml = { displayValue, row ->
    displayValue
}
```

Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.

Below is a screenshot of the DB Picker Scripted Field Configuration:-

![db_picker_config.png](https://community.atlassian.com/t5/image/serverpage/image-id/338859iD1F9EFBC1D557B95/image-size/large?v=v2&px=999 "db_picker_config.png")

Below is the sample database table that I am using for testing:-

|----|----------------|---------------|
| id | country_name   | capital_city  |
| 1  | India          | New Delhi     |
| 2  | United States  | Washington DC |
| 3  | United Kingdom | London        |
| 4  | Malaysia       | Kuala Lumpur  |
| 5  | Singapore      | Singapore     |
| 6  | Japan          | Tokyo         |
| 7  | Australia      | Canberra      |

Below are a couple of test screenshots for your reference:-

1. If the MOCK project is selected on the options India, United States and United Kingdom are available in the Countries field (DB Picker) as shown in the screenshot below:-

![test1.png](https://community.atlassian.com/t5/image/serverpage/image-id/338862i074F537E89D1C642/image-size/large?v=v2&px=999 "test1.png")

2. If the Example project is selected, only the options Malaysia and Singapore are available from the DB Picker as shown in the screenshot below:-

![test2.png](https://community.atlassian.com/t5/image/serverpage/image-id/338861i7A15A093ED62D179/image-size/large?v=v2&px=999 "test2.png")

3. Finally, if the Sample project is selected on the options Japan and Australia are available in the DB Picker as shown in the screenshot below:-

![test3.png](https://community.atlassian.com/t5/image/serverpage/image-id/338863i78E6F3C2CF1C178F/image-size/large?v=v2&px=999 "test3.png")

I hope this helps to solve your question. :-)

Thank you and Kind regards,  
Ram

Senior Support Engineer


author: Jeyanth Kumar
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

Thanks for sharing the script. You got my point correctly. Unfortunately I get an error with respect to permissions. Should enable anything in the shared path ?

The script could not be compiled:

```
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:\sewd-jira07\G\sharedhome\scriptrunner\classes\Script93.class (Access is denied)\sewd-jira07\G\sharedhome\scriptrunner\classes\Script93$_run_closure1.class (Access is denied)2 errors
```

Best regards

Jeyanth


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Jeyanth Kumar](/t5/user/viewprofilepage/user-id/5542211)

Are you on a DC single instance or a clustered environment with Multiple nodes?

If it's the former, you don't need to use the shared path. Otherwise, you must ensure that those folders have full read-write permission.

You can refer to this [community post](https://community.atlassian.com/t5/Data-Center-questions/Datacenter-Shared-Home-Folder-permissions/qaq-p/587041) for more information.

Also, has the error occurred previously?

I am looking forward to your feedback and clarification.

Thank you and Kind regards,  
Ram


author: Jeyanth Kumar
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

I am on DC with multiple nodes. The error didn't occur previously. This has happened after adding the configuration script.

Best regards

Jeyanth


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Jeyanth Kumar](/t5/user/viewprofilepage/user-id/5542211)

Please share a screenshot of your configuration so I can review it and provide some feedback.

Thank you and Kind regards,  
Ram


author: Jeyanth Kumar
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

Here you go.

import com.atlassian.jira.issue.Issue  
import com.onresolve.scriptrunner.canned.jira.fields.editable.database.SqlWithParameters

def columnName = 'VALUE'

getSearchSql = { String inputValue, Issue issue, originalValue -\>

if (issue.projectObject.name == 'ServiceNow') {  
new SqlWithParameters("""  
select ID, VALUE from AO_00B950_AOPROJECT_VALUE  
where ${columnName} in (?,?,?)  
""", \['1000 Core Transport Network Sweden', '1015 Fixed Switch', '4459 Service Now'\])

} else if (issue.projectObject.name == 'Confluence Administration') {  
new SqlWithParameters("""  
select ID, VALUE from AO_00B950_AOPROJECT_VALUE  
where ${columnName} in (?,?)  
""", \['4461 IT Dev Tools - Recharge'\])  
} else if (issue.projectObject.name == 'Jira Administration') {  
new SqlWithParameters("""  
select ID, VALUE from AO_00B950_AOPROJECT_VALUE  
where ${columnName} in (?,?)  
""", \['4461 IT Dev Tools - Recharge'\])  
}  
}

renderViewHtml = { displayValue, row -\>  
displayValue  
}

Also, I could see that we have 4 nodes (4 servers) where sharedfolder is available on only one node. As verified, I could see the permission is full for the same.

Best regards

Jeyanth


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Jeyanth Kumar](/t5/user/viewprofilepage/user-id/5542211)

Thank you for getting back and providing your feedback.

If you read carefully through the code, you have

In the first SQL Query you updated, i.e.:-  

```
new SqlWithParameters("""
select ID, VALUE from AO_00B950_AOPROJECT_VALUE
where ${columnName} in (?,?,?)
""", ['1000 Core Transport Network Sweden', '1015 Fixed Switch', '4459 Service Now'])
```

You are putting 3 question marks and, accordingly, 3 field names. Hence this works as expected.

However, in your 2nd and 3rd SQL Queries, i.e.:-  

```
new SqlWithParameters("""
select ID, VALUE from AO_00B950_AOPROJECT_VALUE
where ${columnName} in (?,?)""", ['4461 IT Dev Tools - Recharge'])
```

and  

```
new SqlWithParameters("""
select ID, VALUE from AO_00B950_AOPROJECT_VALUE
where ${columnName} in (?,?)""", ['4461 IT Dev Tools - Recharge'])
```

You put 2 question marks but only 1 column name, i.e. **4461 IT Dev Tools - Recharge**. Hence it fails.

If you are only putting 1 column name, you must only put 1 question mark for the query to work.

Please correct your code accordingly to:-  

```
import com.atlassian.jira.issue.Issue
import com.onresolve.scriptrunner.canned.jira.fields.editable.database.SqlWithParameters

def columnName = 'VALUE'

getSearchSql = { String inputValue, Issue issue, originalValue ->

    if (issue.projectObject.name == 'ServiceNow') {
        new SqlWithParameters("""
        select ID, VALUE from AO_00B950_AOPROJECT_VALUE
        where ${columnName} in (?,?,?)
        """, ['1000 Core Transport Network Sweden', '1015 Fixed Switch', '4459 Service Now'])

    } else if (issue.projectObject.name == 'Confluence Administration') {
        new SqlWithParameters("""
        select ID, VALUE from AO_00B950_AOPROJECT_VALUE
        where ${columnName} in (?)""", ['4461 IT Dev Tools - Recharge'])
    } else if (issue.projectObject.name == 'Jira Administration') {
        new SqlWithParameters("""
        select ID, VALUE from AO_00B950_AOPROJECT_VALUE
        where ${columnName} in (?)""", ['4461 IT Dev Tools - Recharge'])
    }
}

renderViewHtml = { displayValue, row ->
    displayValue
}
```

I hope this helps to solve your question. :-)  

I am looking forward to your feedback.

Thank you and Kind regards,  
Ram


author: Jeyanth Kumar
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

That's a nice learning for me. I was not aware of that but unfortunately the same error persists with respect to shared folder. I am in different node but the sharedfolder with respect scriptrunner is available in another node. When verified the permissions. I could see the permission is enabled fully. Let me know if I am looking it wrong.

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/339614iD13BD41A9FDF6097/image-size/large?v=v2&px=999 "image.png")

Best regards

Jeyanth


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Jeyanth Kumar](/t5/user/viewprofilepage/user-id/5542211)

This seems to be something related to the permission on the NFS Server.

Try to temporarily grant all permission to Everyone and see if it makes a difference.

I suggest reading through this [troubleshooting doc](https://learn.microsoft.com/en-us/troubleshoot/windows-server/networking/nfs-server-file-permissions) from Microsoft for more info.

Thank you and Kind regards,  
Ram


author: Jeyanth Kumar
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

Good day. I was able to fix the shared file permissions. But sadly I get error this time, I am using 1 table right with 2 columns selected, instead of 2 tables. Please find my screenshot below.

![Screenshot 2024-08-04 095622.png](https://community.atlassian.com/t5/image/serverpage/image-id/339870i3A9FCAA91BF6BEA0/image-size/large?v=v2&px=999 "Screenshot 2024-08-04 095622.png")

As you could see, 3 options are getting listed as per the specific project but the value is getting displayed multiple times. Also when I chose the value. I get error "Failed to find ID:163"

Best regards

Jeyanth


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Jeyanth Kumar](/t5/user/viewprofilepage/user-id/5542211)

Please clarify: Are the values you are querying in the SQL Query unique, or do they appear in multiple rows in the table?

If it's the latter, then the code works just fine. However, you must add additional parameters to make the result displayed in the SQL Query unique.

The values in the example I have provided are unique, as shown in the Database Table. Hence, I am not facing any issue.

It would be helpful if you share a full screenshot of your configuration and all the updated codes you are currently using.

I am looking forward to your feedback and clarification.

Thank you and Kind regards,

Ram


author: Jeyanth Kumar
body: Hello [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

Good day. I see three entries for all the three projects For example: For CNA project we have only one value but I get the same option thrice and all three are selectable. From table perspective we don't have any duplicate entries.

Please see the screenshot for my entire configuration.

![Screenshot 2024-08-05 110944.png](https://community.atlassian.com/t5/image/serverpage/image-id/339927iB6BAD0B0D0983E0E/image-size/large?v=v2&px=999 "Screenshot 2024-08-05 110944.png")![Screenshot 2024-08-05 111025.png](https://community.atlassian.com/t5/image/serverpage/image-id/339928i8100650114B51AEA/image-size/large?v=v2&px=999 "Screenshot 2024-08-05 111025.png")

Best regards

Jeyanth


author: Jeyanth Kumar
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

Adding more screenshot for your understanding.![CNA.png](https://community.atlassian.com/t5/image/serverpage/image-id/339967i30D0D7062A96161A/image-size/large?v=v2&px=999 "CNA.png")![SNOW.png](https://community.atlassian.com/t5/image/serverpage/image-id/339969i089BAE72225E2899/image-size/large?v=v2&px=999 "SNOW.png")![Jira.png](https://community.atlassian.com/t5/image/serverpage/image-id/339968i293036EDB1EFD3F1/image-size/large?v=v2&px=999 "Jira.png")

Best regards,

Jeyanth


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Jeyanth Kumar](/t5/user/viewprofilepage/user-id/5542211)

This clearly indicates that the columns you are invoking are used in multiple rows, hence the duplicates.

Please modify your SQL Query and add the DISTINCT keyword, as shown below and see if the duplicate values are removed.

```
import com.atlassian.jira.issue.Issue
import com.onresolve.scriptrunner.canned.jira.fields.editable.database.SqlWithParameters

def columnName = 'VALUE'

getSearchSql = { String inputValue, Issue issue, originalValue ->

    if (issue.projectObject.name == 'ServiceNow') {
        new SqlWithParameters("""
        select DISTINCT ID, VALUE from AO_00B950_AOPROJECT_VALUE
        where ${columnName} in (?,?,?)
        """, ['1000 Core Transport Network Sweden', '1015 Fixed Switch', '4459 Service Now'])

    } else if (issue.projectObject.name == 'Confluence Administration') {
        new SqlWithParameters("""
        select DISTINCT ID, VALUE from AO_00B950_AOPROJECT_VALUE
        where ${columnName} in (?)""", ['4461 IT Dev Tools - Recharge'])
    } else if (issue.projectObject.name == 'Jira Administration') {
        new SqlWithParameters("""
        select DISTINCT ID, VALUE from AO_00B950_AOPROJECT_VALUE
        where ${columnName} in (?)""", ['4461 IT Dev Tools - Recharge'])
    }
}

renderViewHtml = { displayValue, row ->
    displayValue
}
```

I am looking forward to your feedback.

Thank you and Kind regards,

Ram


author: Jeyanth Kumar
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

Many thanks again for your reply. I was able to successfully fix the problem with our earlier script. The reason why there are 3 entries in the drop down value is due to 3 custom fields with same values in Jira. I have added additionally the customfield id. Please find it below

import com.atlassian.jira.issue.Issue  
import com.onresolve.scriptrunner.canned.jira.fields.editable.database.SqlWithParameters

def columnName = 'CUSTOMVALUE'

getSearchSql = { String inputValue, Issue issue, originalValue -\>

if (issue.projectObject.key == 'SNOW') {  
new SqlWithParameters("""  
select ID, CUSTOMVALUE from CUSTOMFIELDOPTION  
where ${columnName} in (?,?,?) and CUSTOMFIELD = '12406'  
""", \['1000 Core Transport Network Sweden', '1015 Fixed Switch', '4459 Service Now'\])

} else if (issue.projectObject.key == 'CNA') {  
new SqlWithParameters("""  
select ID, CUSTOMVALUE from CUSTOMFIELDOPTION  
where ${columnName} in (?) and CUSTOMFIELD = '12406'  
""", \['4461 IT Dev Tools - Recharge'\])  

} else if (issue.projectObject.key == 'JIRA') {  
new SqlWithParameters("""  
select ID, CUSTOMVALUE from CUSTOMFIELDOPTION  
where ${columnName} in (?) and CUSTOMFIELD = '12406'  
""", \['4461 IT Dev Tools - Recharge'\])  
}  
}

renderViewHtml = { displayValue, row -\>  
displayValue  
}

Once again, I appreciate your sincere replies for getting this fixed. Cheers !

Best regards

Jeyanth


author: Laurie Sciutti
body: Hello [@Jeyanth Kumar](/t5/user/viewprofilepage/user-id/5542211) and welcome to the Community! I have not used the Data Picker field but it looks like you can use a "Retrieval/validation SQL" script to achieve what you want. There are some sample scripts (provided you are using a recent version of SR).

![Capture.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/335330i2A9C32C2D96D53B8/image-size/large?v=v2&px=999 "Capture.PNG")

comments: author: Jeyanth Kumar
body: Hi [@Laurie Sciutti](/t5/user/viewprofilepage/user-id/1137433) ,

Thanks for replying. I have used the examples but my case is. I have to use an SQL like this.

select a.col5

from a.tab1, b.tab2

where a.col3=b.col5 and a.col2 = 12406 and b.col3 = {project}

order by a.col5

Here in this SQL col3 from table b is projectkey which is based dynamically.

Hope this helps.

Best regards

Jeyanth


Tags: custom-field

data-center

jira-data-center

scriptrunner