Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to Sync parent Custom Field with Child Issue's Custom Field(same Field)

Hi I have a field called "Impacted Applications" in Both Parent Issue and Child Issue( issue Type == Epic).

My requirement is , when i will delete or remove value from child custom field same field will be removed from Parent Custom field too.

Fr Ex:- Parent issue == Parent1

   Child issues == Epic - 1, Epic - 2.

Again i need to check all Child or All Epic issues which are under same Parent Issues that the value i have removed from Epic -1 that should not present in any other child issues, if present i will not remove that value from Parent issue.

But i am unable to find the solution, that how to check which value is removed from Epic -1 and same value only should remove Parent too.

Below i have a code for to eliminate the last value added in Parent issues will be deleted.

But its not working.

Can anyone help me on this?

Regards

Manas

 

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.customfields.option.Option

import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.index.IssueIndexingService

import org.apache.log4j.Logger
import org.apache.log4j.Level ;

log.setLevel(Level.DEBUG) ;
Issue epicIssue = event.issue
String issuetype = epicIssue.getIssueType().getName()

if(issuetype == "Epic"){
if (event?.getChangeLog()?.getRelated("ChildChangeItem")?.find { it.field == "Impacted Application/s" }) {

def changeItem = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find { it.field == "Impacted Application/s" }
String[] newValue = changeItem.get("newstring").toString().split(';')
String[] oldValue = changeItem.get("oldstring").toString().split(';')

log.debug("New Value: "+newValue)
log.debug("Old Value: "+oldValue)

//Split and get array of entries per CSI

if( (oldValue[0] == 'null') || newValue.size() <= oldValue.size()){

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def ImpactedAppField = customFieldManager.getCustomFieldObjectByName("Impacted Application/s")
String ImpactedAppValue = epicIssue.getCustomFieldValue(ImpactedAppField)
log.debug("Value in Impacted App field is: "+ImpactedAppValue)
log.debug("Value in Event is: "+ event?.getChangeLog()?.getRelated("ChildChangeItem")?.find { it.field == "Impacted Application/s" })

def cf = customFieldManager.getCustomFieldObjectByName("Parent Link")
def parentValue = epicIssue.getCustomFieldValue(cf)


if(parentValue != null){
log.debug("parent Value is: "+parentValue)

def issueManager = ComponentAccessor.getIssueManager() ;
Issue PlanviewIssue = issueManager.getIssueObject("" + parentValue);
log.debug("Planview Issue is: "+PlanviewIssue)
String planviewImpactedApp = PlanviewIssue.getCustomFieldValue(ImpactedAppField)
log.debug("Planview Impacted Apps are: "+planviewImpactedApp)
if(planviewImpactedApp != null){
def userManager = ComponentAccessor.getUserManager()
ApplicationUser admin = userManager.getUserByName("INT_JIRA")
String[] csis= separateCSI(ImpactedAppValue)


// def newField = newAppList.size() - 1;
if(planviewImpactedApp != ImpactedAppValue){
log.debug("Both Parent and Child fields are not same>>>>" + planviewImpactedApp)
def newField = planviewImpactedApp as String[]
planviewImpactedApp.size() - 1

log.debug("Both Parent and Child fields are not same>>>>" + newField)
//Build my unique string
/* String newAppList = ""
csis.each { String csi ->
newAppList += csi +"; "
}
modifyValue(admin,PlanviewIssue,ImpactedAppField, newAppList.substring(0,newAppList.length()-2)) */
}
}
else{

}

}
}

}
}

boolean modifyValue(ApplicationUser user, Issue issue, CustomField customField, String value){

IssueService issueService = ComponentAccessor.getComponent(IssueService);
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.addCustomFieldValue(customField.id, value)
def update = issueService.validateUpdate(user, issue.getId(), issueInputParameters)

/* validate update info and execute update */

if (update.isValid()) {

IssueService.IssueResult status = issueService.update(user, update)
if (!status.isValid())
{
for (def error : status) {

}
} else {

}
} else {


IssueService.IssueResult status = issueService.update(user, update)

for (def error : status) {

}
}

return true
}

def String[] separateCSI(String apps){

def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")

//need to take out the [ ] that show up under my CSI's
apps = apps.trim()
log.warn(apps)
//if(apps.contains("\\[")) apps = apps.split("\\[")
def counter = 0
String[] result = apps.split(';')
result.each { String csi ->
if(csi.contains('[')){
String[] s = csi.split('\\[')
result[counter] = s[0].trim()
} else if(csi.contains('(')){
String[] s = csi.split('\\(')
result[counter] = s[0].trim()
} else if(csi.contains('-')){
String[] s = csi.split('\\-')
result[counter] = s[0].trim()
} else result[counter] = csi.trim()

counter++

}

//Change this line 10/15 from result to result.toUnique() to remove duplicates
return result.toUnique();

}

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

5 mobile apps for Jira Cloud to boost productivity

  It’s very important to have access to the workflow process from anywhere. Especially if you manage the work of others. There is no difference whether you’re out of office, or drive a ca...

225 views 2 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you