Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Need Help with Assets Field Data Copied over to Sub-Tasks

Sriharsha
Contributor
September 25, 2024

Hi,

I'm using the below script to create sub-tasks using Automation and working fine. The issue I'm facing here is that I'm unable to copy the value of Software Sub-Product relevant values based on the Software Products like like Adobe, Microsoft. Adobe has Software Sub-Products like Animate and Microsoft has Copilot. Each Sub-task is getting created for each Adobe and Microsoft. I want to copy Animate under Software Sub-Product field in the Adobe sub-task and Copilot under Software Sub-Product field for Microsoft Sub-task.

Both Software Product and Software Sub-Product Fields are Asset object field (multi-select).

Here is the Script I'm using - 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

// Define the IDs for the custom fields
Long softwareVendorFieldId = 31701L // Multi-select asset object field for "Software - Vendor"
Long installForFieldId = 32006L // Custom field ID for "Install For"
Long softwareSubProductFieldId = 31702L
// Retrieve the current issue from the context
def issueManager = ComponentAccessor.getIssueManager()
MutableIssue parentIssue = issueManager.getIssueObject(issue.id) // Ensure issue is available

// Fetch the custom field for "Software - Vendor" and "Install For"
CustomField softwareVendorField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(softwareVendorFieldId)
CustomField installForField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(installForFieldId)
CustomField softwareSubProductField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(softwareSubProductFieldId)

def selectedOptions = parentIssue.getCustomFieldValue(softwareVendorField) as List
def installForValues = parentIssue.getCustomFieldValue(installForField) as List
def softwareSubProductValues = parentIssue.getCustomFieldValue(softwareSubProductField) as List

if (selectedOptions) {
    // Log each option to inspect its type and create a subtask for each
    selectedOptions.each { option ->
        log.info("Selected option: " + option.toString())
        createSubTaskForOption(parentIssue, option, softwareVendorField, installForField, softwareSubProductField, installForValues, softwareSubProductValues)
    }
}

void createSubTaskForOption(MutableIssue parentIssue, def option, CustomField softwareVendorField, CustomField installForField, CustomField softwareSubProductField, def installForValues, def softwareSubProductValues) {
    def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
    IssueFactory issueFactory = ComponentAccessor.getIssueFactory()
    IssueManager issueManager = ComponentAccessor.getIssueManager()

    // Create a new subtask instance
    MutableIssue subTask = issueFactory.getIssue()

    // Assuming option is a simple string (adjust as necessary based on logging)
    subTask.setSummary("Software Request: " + option)  // Subtask summary based on the option value
    subTask.setIssueTypeId("22500") // Replace "5" with your subtask issue type ID
    subTask.setParentObject(parentIssue)
    subTask.setProjectObject(parentIssue.getProjectObject())
    subTask.setReporter(parentIssue.getReporter())

    // Copy the "Software - Vendor" field value (current option) to the subtask
    subTask.setCustomFieldValue(softwareVendorField, [option]) // Only set the current option

    // Copy the "Install For" field value to the subtask (copying all values from the parent)
    if (installForValues) {
        subTask.setCustomFieldValue(installForField, installForValues)
    }
    if (softwareSubProductValues) {
        subTask.setCustomFieldValue(softwareSubProductField, softwareSubProductValues)
    }
    // Store the new subtask issue
    issueManager.createIssueObject(user, subTask)

    // Use SubTaskManager to link the subtask to the parent issue
    def subTaskManager = ComponentAccessor.getSubTaskManager()
    subTaskManager.createSubTaskIssueLink(parentIssue, subTask, user)

    log.info("Subtask '${subTask.summary}' created and linked successfully.")
}

def filterRelevantSubProducts(def softwareSubProductValues, def option) {
    return softwareSubProductValues.findAll { subProduct ->
        subProduct.toString().contains(option.toString())
    }
}
Thanks, in advance! Can anyone help?
@Ram Kumar Aravindakshan _Adaptavist_  - can you please help me

1 answer

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
September 26, 2024

Hi @Sriharsha

Based on what condition do you want the Sub-Tasks to be created?

I am asking this because instead of using Automation to create the Subtasks, you can do it directly via ScriptRunner. When the Subtask is created, it can automatically pass the Asset value to the field.

I am looking forward to your feedback and clarification.

Thank you and Kind regards,
Ram

 

Sriharsha
Contributor
September 26, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ , thank you for your response! 

I want the sub-tasks to be created based on the field called Software Sub-Product.

I have placed this script in the Automation Action : 

Execute a ScriptRunner script

 

I'm able to create the sub-tasks but unable to copy the relevant sub-products like Animate for Adobe Sub-task and copilot for Microsoft Sub-Task. I'm getting both the values (Animate & Copilot) in both the Sub-tasks. 

Sriharsha
Contributor
September 26, 2024

You mean if I use the above same script in the Workflow post function, will it work?

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
September 26, 2024

Hi Sriharsha,

In your last comment, you asked:-

You mean if I use the above same script in the Workflow post function, will it work?

It depends. As I asked in my previous comment:-

Based on what condition do you want the Sub-Tasks to be created? I need to know this first.

Thank you and Kind regards,
Ram

 

Sriharsha
Contributor
September 26, 2024

I have an Asset Field which fetches Users from an Object Schema and based on number of users, I need to create Sub-tasks based on the Software Sub-Products selected for each user. Let's say I have selected two users and 3 Software Sub-Products. For one user, 3 Sub-tasks (one for each Software Sub-Product selected). 

In total 6 ( 3 for each user) in this case.

Best Regards,

Sriharsha

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
September 27, 2024

Hi @Sriharsha

I will work on an example and get back to you once I have an update.

Thank you and Kind regards,
Ram

Sriharsha
Contributor
September 27, 2024
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
September 30, 2024

Hi @Sriharsha

Below is a sample working code for your reference:-

import com.adaptavist.hapi.jira.assets.Assets
import com.adaptavist.hapi.jira.users.Users
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean

def selectedUsers = issue.getCustomFieldValue('Selected Users') as List<ObjectBean>

if (issue.issueType.name == 'Task') {

selectedUsers.each { selectedUser ->

issue.createSubTask('Sub-task') {
setSummary('Test Sub-Task')
setDescription('Testing Sub-Task 123')

def username = Assets.getById(selectedUser.id).getAttributeValues('Username').toString().replace("[StringValue(value=",'').replace(')]','').trim()

setAssignee(Users.getByName(username))

def id = Integer.parseInt(Assets.getById(selectedUser.id).getAttributeValues('Software').toString().substring(25, 27).trim())

Assets.getById(id).each {product ->
setCustomFieldValue('Assigned Product', product)
}
}
}
}

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

In the code above, I have used ScriptRunner's HAPI Isseu Sub-Task Creation feature along with the Assets feature to simplify the code.

I am able to get the Sub-task to be created with the correct product in the Asset field that is set to the User without any need for Automation.

Below is a screenshot of the Post-Function configuration:-

post_function_config.png

Also, in my environment, I have configured an example Asset configuration, which most likely will not match yours. So, you must play around with the code accordingly.

Below are screenshots of my Asset configuration:-

1. The User Asset config

asset1.png

2. The Adobe Asset config

asset2.png

3. The Microsoft Asset Config

asset3.png

Below are some test screenshots for your reference:-

1. On the create screen two users are selected from the Asset field both who are Adobe product users.

test1.png

2. Once the issue is created 2 sub-tasks are also created.test2.png

3. In the first sub-task, the Asset is set to Photoshop, the Assignee is set to the User selected in the Asset

test3.png

4. Similar to number 3, the Assignee is set to the User selected in the Asset. However, this time, the Asset is set to Acrobat.

test4.png

 

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

Thank you and Kind regards,
Ram

 

Sriharsha
Contributor
September 30, 2024

Thank you so much, @Ram Kumar Aravindakshan _Adaptavist_ ! I will check and update you ASAP.

 

Best regards,

Sriharsha

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
November 29, 2024

Hi @Sriharsha

Has your question been answered?

If yes, please accept the answer provided.

Thank you and Kind regards,
Ram

Sriharsha
Contributor
December 9, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

 

Please find the script I have worked upon which creates  linked issues based on the values of  Software Vendor field (Asset Object multi-select) which has options. Example while creating the request,  if I select options  like Adobe, VMWare, it creates two linked tickets. There's another field called Software - Sub Product, which has the child values of Software Vendor field. Please find the screenshots attached.

 

If you look at the AC Software 3.PNG file, It should only show the Sub Products related to Software Vendor (Adobe) and not other vendor products like X-Ways.

 

X-ways has another linked ticket created and it should show X-ways related Sub- Products and not Adobe related options.

AC Software 5.PNG 

 

Attached is my script I have been executing using an Automation Action - Run a Script (AC Software 4.PNG) -

/////////////////////////////////////////////////////////////////////////////////////

import com.adaptavist.hapi.jira.assets.Assets
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.fields.CustomField
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean

Long softwareVendorFieldId = 32320L // Multi-select asset object field for "Software - Vendor"
Long softwareSubProductFieldId = 32321L

def issueManager = ComponentAccessor.getIssueManager()
MutableIssue parentIssue = issueManager.getIssueObject(issue.id) // Ensure issue is available

parentIssue.setAssignee(null)
// Fetch the custom fields for "Software - Vendor" and "Install For"
CustomField softwareVendorField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(softwareVendorFieldId)
CustomField softwareSubProductField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(softwareSubProductFieldId)


def selectedOptions = parentIssue.getCustomFieldValue(softwareVendorField) as List<ObjectBean>
def softwareSubProductValues = parentIssue.getCustomFieldValue(softwareSubProductField) as List<ObjectBean>

if (selectedOptions) {
selectedOptions.each { option ->
log.info("Selected option: " + option.toString())
createServiceRequestForOption(parentIssue, option, softwareVendorField, softwareSubProductField, softwareSubProductValues)
}
}

void createServiceRequestForOption(MutableIssue parentIssue, def option, CustomField softwareVendorField, CustomField softwareSubProductField, def softwareSubProductValues) {
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
IssueFactory issueFactory = ComponentAccessor.getIssueFactory()
IssueManager issueManager = ComponentAccessor.getIssueManager()
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
def attachmentManager = ComponentAccessor.getAttachmentManager()

// Create a new Service Request instance
MutableIssue serviceRequest = issueFactory.getIssue()

// Set the necessary fields for the service request
String reporterName = parentIssue.getReporter()?.getDisplayName() ?: "Unknown Reporter"
String cleanedOption = option.toString().replaceAll(/\s*\(.*?\)\s*/, "")
serviceRequest.setSummary("Software Request: " + cleanedOption + " for " + reporterName)
serviceRequest.setIssueTypeId("22601") // Replace with Software Sub Request issue type ID
serviceRequest.setProjectObject(parentIssue.getProjectObject())
serviceRequest.setReporter(parentIssue.getReporter())
serviceRequest.setDescription(parentIssue.getDescription())
serviceRequest.setPriority(parentIssue.getPriority())

// Unassign the Service Request
serviceRequest.setAssignee(null)
// Copy the "Software - Vendor" field value (current option) to the service request
serviceRequest.setCustomFieldValue(softwareVendorField, [option]) // Only set the current option
// Copy the "softwareSubProductField" value to the service request (copying all values from the parent)

if (softwareSubProductValues) {
serviceRequest.setCustomFieldValue(softwareSubProductField, softwareSubProductValues)
}

// Store the new linked issue
issueManager.createIssueObject(user, serviceRequest)
def parentAttachments = attachmentManager.getAttachments(parentIssue)
parentAttachments.each { attachment ->
attachmentManager.copyAttachment(attachment, user, serviceRequest.key)
}
issueLinkManager.createIssueLink(parentIssue.id, serviceRequest.id, 10003L, null, user)
log.info("Service Request '${serviceRequest.summary}' created, attachments copied, and linked successfully.")

/////////////////////////////////////////////////////////////////////////////////////

AC Software 2.PNGAC Software 1.PNGAC Software 3.PNGAC Software 4.PNG

 

Could you please help me fix my code.

 

Best regards,

Sriharsha

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events