Forums

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

Need scriptrunner script to execute only at login/or once per user

AisM
Contributor
September 18, 2023

Hello,

I have a script fragment, which shows a prompt (dialog box) to the users. Currently, the script works fine, but it shows the prompt everytime the page loads. How do I limit this to once per user,/ or only at the time of login. Below is a snippet from the code,

 

def searchService = ComponentAccessor.getComponent(SearchService)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
def issueManager = ComponentAccessor.getIssueManager()
def searchRequestManager = ComponentAccessor.getComponent(SearchRequestManager)
def filter = searchRequestManager.getSearchRequestById(3737)
def query = filter.query

def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())

 

1 answer

1 vote
Hauke Bruno Wollentin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 18, 2023

How do I limit this to once per user

You can create a user property to store kind of a boolean value in it to know in your script if the dialog has been shown to that user or not (and then reset it every night or so by a job if needed).

AisM
Contributor
September 18, 2023

@Hauke Bruno Wollentin Thank you for the comment. 

Actually, the time for when the script must run is triggered through  custom field values from a service desk. The script gets the announcement start & end date from the ticket & shows the pop up box during that specific time. It is working fine. Only issue is, the pop up shows every time the page refreshed. 

Could you please help with any links or examples on how to create the user property. I do not have an extensive scripting knowledge to fully understand how to do that on my own. 

Like Hauke Bruno Wollentin likes this
Hauke Bruno Wollentin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 19, 2023

There is an example in the Adaptavist library: https://library.adaptavist.com/entity/jira-user-properties

Reece Lander [ScriptRunner - The Adaptavist Group]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 19, 2023

If you're running ScriptRunner 7.11.0 or above, our HAPI feature provides simplified scripting syntax for many things, including reading/setting user properties: https://docs.adaptavist.com/sr4js/latest/hapi/work-with-issue-and-entity-properties#reading-and-writing-user-properties

Like Hauke Bruno Wollentin likes this
AisM
Contributor
September 19, 2023

Thank you for the links. But sadly, I'm not able to figure out how to bring this concept into my scripting. 

Hauke Bruno Wollentin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 19, 2023

The idea is to run your script as described in your first post but to add a key/value pair like "my.cool.dialog.displayed: true" to every user as an user property.

In your script you than can just check if the current user has such property set or not, if not -> show the dialog; if yes -> do nothing.

AisM
Contributor
September 19, 2023

Okay, makes sense. I added the below code snippet, but nothing happened. Do, you think I'm missing something, 

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

String newInfoTag = "Update"
def userPropertyManager = ComponentAccessor.getOSGiComponentInstanceOfType(UserPropertyManager.class);
def userProperty = userPropertyManager.getPropertySet(user)?.getString("Display.Outage");

//BELOW IS THE END OF SCRIPT

if ((userProperty != newInfoTag) && (today>=start && today<=end)) {
log1.info("Success:")
return true
}
}
return false

 

Tried this as well, did not work

def propertyname = "OutageBox"
def propertyvalue = "positive"
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

//below is end of Script

if ((today>=start && today<=end)) {
log1.info("Success:")
return true
}
if (user) {
def userPropertyManager = ComponentAccessor.userPropertyManager
def propertySet = userPropertyManager.getPropertySet(user).setString(propertyname,propertyvalue) as String

def isPropertySet = propertySet.contains(propertyname)

return isPropertySet
}
else {

}
}
return false

 
Hauke Bruno Wollentin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 20, 2023

Without having more context, this should do

 

def user = Users.loggedInUser
def propertySet = user.propertySet

String key = "OutageBox"

def foo = propertySet.getString(key)

if (!foo) {

// Display the stuff
propertySet.setString(key, "positive")

}
Since the idea behind the OutageBox key/value pair is kind of a boolean behaviour, just the condition is needed to check if the value isn't there (for now), assuming that is will be false or negative of just not exists.

Suggest an answer

Log in or Sign up to answer