get Structure View Id?

DPE TEAM December 9, 2020

I am trying to switch the owner of a Structure View. I have received an example ScriptRunner script from ALMWorks to do this. The script requires I know the ID of the View. How/where do I find the Structure View's ID?

1 answer

1 accepted

0 votes
Answer accepted
David Niro
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 10, 2020

Hello DPE Team,

David from ALM Works here!

I see that we were able to provide an answer to your question through the ticket you reference.  Sharing it here as well, so others who are looking for a similar solution will be able to find it easily!

The script below has 3 parameters:

Starting with 'oldUserName,' which is the current owner of the view.

'newUserName,' which is the user that will be the owner of the view after the script execution.

And flag 'dryRunMode,' which states that if the value is 'true' - proceed by running the script without changing any states.

We'd first suggest running the script in your staging instance (if there is one) and observe if you get a similar result to this one:


View #42 "asdvfb": admin(admin) -> test
View #8 "my view": admin(admin) -> test
View #9 "progress_view": admin(admin) -> test
View #41 "sorter_test_view": admin(admin) -> test


If you don't get any errors, then run the script again by changing the 'True' value of '*dryRunMode*' to '*false*.'

// change values
def oldUserName = 'test'
def newUserName = 'admin'

def dryRunMode = true

//**************!!!!!!!***************
// do not change code below
//**************!!!!!!!***************

import com.atlassian.jira.component.ComponentAccessor

def plugin = ComponentAccessor.pluginAccessor.getPlugin('com.almworks.jira.structure')
def JiraUsers = plugin.classLoader.loadClass('com.almworks.jira.structure.api.util.JiraUsers')
def structureComponents = plugin.getModuleDescriptor('structure-components').module
def viewManager = structureComponents.getViewManager()

def result = []

def oldUserKey = ComponentAccessor.getUserManager().getUserByName(oldUserName).getKey()
def newUser = ComponentAccessor.getUserManager().getUserByName(newUserName)

if (oldUserKey == null) {
result << """'Old User' key:'${oldUserKey}' doesn't exist, cannot change the owner of the views"""
}

if (newUser == null) {
result << """'New User' name:'${newUserName}' doesn't exist, cannot change the owner of the views"""
}

if (newUser != null && oldUserKey != null) {
viewManager.getViews(null).each { view ->
def user = view.getOwner()
if (user && oldUserKey == user.getKey()) {
result << "View #${view.id} \"${view.name}\": ${view.owner} -> ${newUser.name}"
if (!dryRunMode) {
view.setOwner(newUser).saveChanges()
}
}
}
}

result.join('<br>')

Best,
David

DPE TEAM December 14, 2020

Hi,

Yes the script I received did reassign the owner. The thing I did not like about this solution was that it reassigned the owner for All views the old owner had. I guess it was not a precise as I would have liked. Overall it did what I needed, thanks.

Suggest an answer

Log in or Sign up to answer