Script to bulk-add favourite dashboards
Asked by Erik Ekengren:

Hi!

I am on the hunt for a way to add some dashboards to my users based on which group they are connected to. The reason for this is:

* My users are not that well trained in Jira yet. Therefore it is hard to ask them to search for specific dashboards
* We have different dashboards for different type of users. HR, IT, Security etc. should look at different dashboards

And that is why I need to find a way to add these dashboards to the users and since we have a lot of users I would prefer not to do this manually one by one...

I would also like to only ADD the dashboards, if they already have other favourite dashboards they should not be deleted as favourites. I found this script in the following link [JRASERVER-15841](https://jira.atlassian.com/browse/JRASERVER-15841):

```
import com.atlassian.jira.ComponentManagerimport com.atlassian.jira.portal.PortalPageimport com.atlassian.jira.portal.PortalPageManagerimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.user.ApplicationUserimport com.atlassian.jira.favourites.FavouritesManagerimport org.apache.log4j.Loggerimport org.apache.log4j.Leveldef log = Logger.getLogger("")log.setLevel(Level.DEBUG)def portalPageIds = [10407,10404]def groupNames = ['teq_jirasd_administrators']def groupManager = ComponentAccessor.getGroupManager()ComponentManager componentManager = ComponentManager.getInstance()PortalPageManager portalPageManager = (PortalPageManager) componentManager.getComponentInstanceOfType(PortalPageManager.class)FavouritesManager favouritesManager = (FavouritesManager) componentManager.getComponentInstanceOfType(FavouritesManager.class)for (String groupName: groupNames) {def grpUserList = groupManager.getUsersInGroup(groupName)for (ApplicationUser user:grpUserList){for (Long portalPageId: portalPageIds) {PortalPage portalPage = portalPageManager.getPortalPageById(portalPageId)try{favouritesManager.addFavourite(user, portalPage)log.info("Adding portal " + portalPageId.toString() + " to user: " + user)} catch (Exception ex){log.warn("Failed Adding portal " + portalPageId.toString() + " to user: " + user + "\n" + ex)} //catch} //for portalID} //for group users} //for groups
```

However I get the same error as the user in the comment:
> The script could not be compiled:
>
> ```
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script162.groovy: 25: expecting '{', found 'Unknown' @ line 25, column 1. Unknown macro:{ favouritesManager.addFavourite(user, portalPage) log.info("Adding portal " + portalPageId.toString() + " to user}
> ```

Perhaps anyone has already done something similar and are able to help me out on this? I would be very grateful!


Answers:
author: Radek Dost?l
body: Ooof, that is an old code.

Here's updated one that seems to work:  

```
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.portal.PortalPage
import com.atlassian.jira.portal.PortalPageManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.favourites.FavouritesManager

Long[] portalPageIds = new Long[]{23508L, 81598L}
String[] groupNames = new String[]{"test-group"}

GroupManager groupManager = ComponentAccessor.getComponent(GroupManager)
PortalPageManager portalPageManager = ComponentAccessor.getComponent(PortalPageManager)
FavouritesManager favouritesManager = ComponentAccessor.getComponent(FavouritesManager)

StringBuilder stringBuilder = new StringBuilder()
String LF = "<br>"

for (String groupName : groupNames) {
    for (ApplicationUser user : groupManager.getUsersInGroup(groupName)) {
        for (Long portalPageId : portalPageIds) {
            PortalPage portalPage = portalPageManager.getPortalPageById(portalPageId)

            if (portalPage != null) {
                try {
                    favouritesManager.addFavourite(user, portalPage)
                    stringBuilder.append("Adding " + portalPage.getName() + " (" + portalPageId + ") to user " + user.getUsername()).append(LF)
                } catch (Exception e) {
                    stringBuilder.append("<b>ERROR:</b> Could not add " + portalPage.getName() + " (" + portalPageId + ") to user " + user.getUsername() + ": " + e.toString()).append(LF)
                }
            }
        }
    }
}

return stringBuilder.toString()
```

comments: author: Erik Ekengren
body: Thank you Radek!

This turned out great and will help me a lot!


Tags: data-center

jira-data-center

jira-server

scriptrunner

server