Want to validate radio button and smart attachment field value.

SysAdmin August 12, 2024

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.

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 23, 2024

Hi @SysAdmin

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

SysAdmin August 25, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

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,

Suggest an answer

Log in or Sign up to answer