Want to validate radio button and smart attachment field value.
Asked by SysAdmin:

Hi,

We have a radio button field, if it is yes then it should check whether is there any attachment in the smart attachment field

Smart attachment has category in its attachment field, we will have to check if the attachment is present in that attachment category

Then if both mentioned above are yes then the transition should happen else it should throw error message 'Upload attachment'

Remember if radio button field has NO in it, then it should not restrict the transition, it is only if it is yes then it should check for attachment and throw error.

Current script is not working, and it is attached here, can you pls help with this?

Below is the script

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.AttachmentManager

//Get the issue and the custom field value

Issue issue = issue

def customFieldValue = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("LLD Configuration Requirement"))

// Check if the custom field is "yes"

if (customFieldValue == "Yes") {

// Get the attachment manager

AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager()

// Check if the issue has attachments

if (attachmentManager.getAttachments(issue).isEmpty()) {

// If there are no attachments, throw an error return false

}

}

//If the custom field is not "yes" or there are attachments, allow the transition return true

Regards.


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

Please clarify what approach you are trying to use to perform the validation.

Are you using Behaviour or the Workflow Validation? If it's the former you will need to use the Server-Side Behaviour and set it for the transition screen. If it's the latter, then it would be best if you use the Custom Script Validator.

Also, please clarify, are you trying to perform this validation using the standard Jira dialog or via the JSM Portal?

I am looking forward to your feedback and clarification.

Thank you and Kind regards,  
Ram

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

Thanks for the reply!

I used a custom script validator for this purpose.

As per the use case I had to script it as below and which is working.

```
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade
import com.stiltsoft.jira.attachcategory.facade.entity.issue.AttachmentCategories
import com.stiltsoft.jira.attachcategory.facade.entity.issue.AttachmentCategory

@WithPlugin("com.stiltsoft.jira.smart-attachments")
SmartAttachmentsFacade facade = ScriptRunnerImpl.getPluginComponent(SmartAttachmentsFacade)


CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField customfield_A = customFieldManager.getCustomFieldObject("customfield_Id")
Option customFieldValue = (Option) issue.getCustomFieldValue(customfield_A)
String customFieldValueRaw = customFieldValue.getValue()

CustomField customfield_B = customFieldManager.getCustomFieldObject("customfield_Id")
Option customFieldValue1 = (Option) issue.getCustomFieldValue(customfield_B)
String customFieldValueRaw1 = customFieldValue1.getValue()

CustomField customfield_C = customFieldManager.getCustomFieldObject("customfield_Id")
Option customFieldValue2 = (Option) issue.getCustomFieldValue(customfield_C)
String customFieldValueRaw2 = customFieldValue2.getValue()

CustomField customfield_D = customFieldManager.getCustomFieldObject("customfield_Id")
Option customFieldValue3 = (Option) issue.getCustomFieldValue(customfield_D)
String customFieldValueRaw3 = customFieldValue3.getValue()


log.warn"customFieldValueRaw= "+customFieldValueRaw+"customFieldValueRaw1"+customFieldValueRaw1+"customFieldValueRaw2"+customFieldValueRaw2+"customFieldValueRaw3"+customFieldValueRaw3
if (customFieldValueRaw.equals("Yes") || customFieldValueRaw1.equals("Yes") || customFieldValueRaw2.equals("Yes") || customFieldValueRaw3.equals("Yes")) {

AttachmentCategories attachmentCategories = facade.getAttachmentCategories(issue)
AttachmentCategory serverCategory = attachmentCategories.getCategories().find { category -> category.getName() == "smart attachmnt category name" }
log.warn"serverCategory= "+serverCategory

def tempAttachmentsCount = serverCategory.getTempAttachments().size()
log.warn"tempAttachmentsCount= "+tempAttachmentsCount
def actualAttachmentsCount = serverCategory.getAttachments(true).size()
log.warn"actualAttachmentsCount= "+actualAttachmentsCount

if (tempAttachmentsCount == 0 && actualAttachmentsCount == 0) {
throw new InvalidInputException("There is no attachment in the category")
}
}?
```

Regards,


Tags: scriptrunner

addon-com.onresolve.jira.groovy.groovyrunner

data-center

jira-data-center