Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Purpose

Hosting

Compatible Tools

Change the Internal/External setting of a form

Server or Data Center

Any scripting tool, such as JMWE, Power Scripts or Scriptrunner, that uses Groovy.

Script

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.bc.issue.properties.IssuePropertyService
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.entity.property.EntityPropertyService

Issue issue = ComponentAccessor.getIssueManager().getIssueObject("ISSUE-1")
String formName = "Form Name"
ApplicationUser loggedInUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
IssuePropertyService issuePropertyService = ComponentAccessor.getComponentOfType(IssuePropertyService.class)
IssueManager issueManager = ComponentAccessor.getIssueManager()

Closure<Long> getFormId = { def property, String name ->
		def slurper = new groovy.json.JsonSlurper()
  	def parsedJson = slurper.parseText(property.value)
  	String id
  	parsedJson.forms.each(){ form ->
  			if(form.name.equals(name)){
    				id = form.id.toString()
      			return
      	}
  	}
  	if(id){
      	return Long.valueOf(id)
  	}
}

def allProperties = issuePropertyService.getProperties(loggedInUser, issue.id)
def formId
for(def property : allProperties){
    if(property.key.equals("proforma.forms")){
   			formId = getFormId(property, formName)
    }
}

boolean changeSecondForm = false

for(def property : allProperties){
    if(property.key.equals("proforma.forms")){
        String toReplace
        for(def section : property.value.split("\"id\":")){
            if(section.startsWith(formId.toString())){
                toReplace = section
            }
        }
        if(toReplace){
            String replacement
            if(toReplace.contains("\"internal\":true")){
                replacement = toReplace.replace("\"internal\":true", "\"internal\":false")
            } else if (toReplace.contains("\"internal\":false")){
                replacement = toReplace.replace("\"internal\":false", "\"internal\":true")
            }
            

            EntityPropertyService.SetPropertyValidationResult validationResult =
                issuePropertyService.validateSetProperty(
                    loggedInUser,
                    issue.id,
                    new EntityPropertyService.PropertyInput(property.value.replace(toReplace, replacement), property.key))
            if(validationResult.isValid()){
                issuePropertyService.setProperty(loggedInUser, validationResult)
                changeSecondForm = true
            }
        }
    }
}

if(changeSecondForm){
    for(def property : allProperties){
        if(property.key.equals("proforma.forms" + ".i" + formId)){
            if(property.value.contains("\"visibility\":\"i\"")){
                EntityPropertyService.SetPropertyValidationResult validationResult =
                    issuePropertyService.validateSetProperty(
                    loggedInUser,
                    issue.id,
                    new EntityPropertyService.PropertyInput(property.value.replace("\"visibility\":\"i\"", "\"visibility\":\"e\""), property.key))
                if(validationResult.isValid()){
                    issuePropertyService.setProperty(loggedInUser, validationResult)
                }
            } else if(property.value.contains("\"visibility\":\"e\"")){
                EntityPropertyService.SetPropertyValidationResult validationResult =
                    issuePropertyService.validateSetProperty(
                    loggedInUser,
                    issue.id,
                	new EntityPropertyService.PropertyInput(property.value.replace("\"visibility\":\"e\"", "\"visibility\":\"i\""), property.key))
                if(validationResult.isValid()){
                    issuePropertyService.setProperty(loggedInUser, validationResult)
                }
            }
        }
    }
}

Installation instructions

  • If used in a transition:

    • Comment line 9

    • Change formName in line 10 to the name of the form that you want to move between internal/external statuses.

  • If used in the script console:

    • Change issue key in line 9 to the issue that has the form you want to move between internal/external statuses.

    • Change formName in line 10 to the name of the form that you want to move between internal/external statuses.

Possible use cases

Use this script to change a form from internal to external or vise versa. For example, you can have a form which was submitted on the portal become Internal (not visible on the portal) when it reaches a given status.

Limitations

  • If multiple forms with the same name exist on the issue, the script will only change the first setting on the first form.

  • This script may not work if the form is over 32 KB. You can limit the size of your forms by using multiple smaller forms, and by including character/word limits on your text fields.

  • No labels