JIRA SM Behaviour to check Description field
Asked by Joel Batac:

Hi,

I'm trying to create a behaviour that checks the content of Description. If the field contains the string "We can't log you in right now" it should set an error but I can't get it working. Any idea how can i go about this?  
Here's the sample code.  
def descriptionField = getFieldById("description")  
def descriptionValue = descriptionField.getValue().toString()

<br />

if (descriptionValue.contains("We can't log you in right now"))  
{  
descriptionField.setError('Error')  
}


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

Could you provide some more information, i.e. when do you want this behaviour to trigger? Is it on the Create, Edit or some other screen?

I am looking forward to your clarification.

Thank you and Kind regards,

Ram

comments: author: Joel Batac
body: Hi, during creation in the Service desk form.


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Joel Batac](/t5/user/viewprofilepage/user-id/4581452)

For your requirement, you must create a Server-Side Behaviour configuration for the Description field.

Below is a sample working Behaviour code:-  

```
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def description = getFieldById(fieldChanged)
def descriptionValue = description.value.toString()

description.clearError()

def wordArray = ['We', "can't", 'log', 'you', 'in', 'right', 'now'] as String[]

if (descriptionValue && containsWords(descriptionValue, wordArray)) {
    description.setError('Error')
}

static boolean containsWords(String input, String[] words) {
    def lastInput = input.split(' ').last()

    if (lastInput.startsWith('n') && lastInput.endsWith('w') && input.split(' ') == words ) {
        Arrays.stream(words).allMatch(input::contains)
    }
}
```

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

Below is a screenshot of the Server-Side Behaviour configuration.

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

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

1. If the Description field contains all the text the error message is displayed as shown in the screenshot below:-

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

2. If the Description field doesn't contain all the text specified, the error message will not be displayed as shown in the screenshot below:-

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

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

Thank you and Kind regards,  
Ram


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Joel Batac](/t5/user/viewprofilepage/user-id/4581452)

Has your question been answered?

If yes, please accept the answer provided.

Thank you and Kind regards,

Ram


author: Joel Batac
body: [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) - I tried the solution but not working for me. It's not setting the Error


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Joel Batac](/t5/user/viewprofilepage/user-id/4581452)

Could you please share the text that you have tested with?

Also, please the me know the current version of Jira and ScriptRunner you are using.

Thank you and Kind regards,

Ram


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Joel Batac](/t5/user/viewprofilepage/user-id/4581452)

As requested in my previous comment, could you please share the text you have tested with?

Also, please the me know the current version of Jira and ScriptRunner you are using.

Thank you and Kind regards,  
Ram


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

Jira Service Management 5.4.10

Scriptrunner Version: 8.8.0


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Joel Batac](/t5/user/viewprofilepage/user-id/4581452)

As requested in my previous comment:-
> As requested in my previous comment, could you please share the text you have tested with?

Please share the sample text that you have tested with so I can test it here in my environment.

Thank you and Kind regards,  
Ram


Tags: behaviours

data-center

jira-service-management

jira-service-management-data-center

scriptrunner