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

groovy script from scheduler

Robi Anton Apr 13, 2016

Hi, 

 

I have a script working perfectly when I call it from my workflow.
But, I also want to use the same script from a scheduler (Systems->Services).

Could someone correct the script? please

 

Thanks

 

import org.apache.log4j.Category
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.*;
import java.sql.Timestamp




def jqlSearch = "project = 'AISC - Suivi des demandes' and ('Date de livraison' >= startOfMonth() and 'Date de livraison' <=now()) and status = Validation"
//If you are going to use a JIRA version => 7 then you should get the ApplicationUser instead of User, to do that remove the .directoryUser
def user = ComponentAccessor.getJiraAuthenticationContext().getUser().directoryUser
def searchService = ComponentAccessor.getComponentOfType(SearchService.class)
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
def value = 0
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def myCustomField = customFieldManager.getCustomFieldObjectByName("Temps d'intervention")
if (parseResult.isValid()) {
try {
def results = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
def issues = results.getIssues()
issues.each {
if (it.getCustomFieldValue(myCustomField))
value += it.getCustomFieldValue(myCustomField).toInteger()
}
} catch (SearchException e) {
e.printStackTrace()
}
} else {
log.warn("Invalid query")
return null
}



IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();


//ComoponentManager = gestionnaire de composant JIRA
ComponentManager componentManager = ComponentManager.getInstance()
//CustomFieldManager = gestionnaire de customfield JIRA
CustomFieldManager cfManager = componentManager.getCustomFieldManager()

// Le champ "Total intervention M0"
CustomField cfTotalInterMCourant = cfManager.getCustomFieldObject("customfield_11405")
// Update the value
cfTotalInterMCourant.updateValue(null, issue, new ModifiedValue("", value.toString() ), changeHolder);

6 answers

0 votes
JamieA Apr 13, 2016

duplicate of https://answers.atlassian.com/questions/38385348 which already has answers

0 votes
Robi Anton Apr 14, 2016

@Jamie Echlin [Adaptavist]

It's not exactly the same.

We can't execute the same groovy script called from a wf in a scheduler. 

0 votes
Robi Anton Apr 17, 2016

@Jamie Echlin [Adaptavist]

 

import org.apache.log4j.Category
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.*;
import java.sql.Timestamp

 

Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());
int currentDay = localCalendar.get(Calendar.DATE);
println currentDay


if ( currentDay == 18 ){
println "WE ARE IN CURRENT DAY _script Somme Precedent"

def jqlSearch = "project = 'AISC - Suivi des demandes' and status was in (Validation) and ('Date de livraison' >= startOfMonth(-1) and 'Date de livraison' <=endOfMonth(-1))"
def userManager = ComponentAccessor.getUserManager()
def issueManager = ComponentAccessor.getIssueManager()
def searchService = ComponentAccessor.getComponentOfType(SearchService)

def user = userManager.getUserByName("yk3520").directoryUser
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)


def value = 0
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def myCustomField = customFieldManager.getCustomFieldObjectByName("Temps d'intervention")


if (parseResult.isValid()) {
try {
def results = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
def issues = results.getIssues()
issues.each {
if (it.getCustomFieldValue(myCustomField))
value += it.getCustomFieldValue(myCustomField).toInteger()

}
} catch (SearchException e) {
e.printStackTrace()
}
} else {
log.warn("Invalid query")
return null
}


// Save the value at custom field with name A number custom field of issue with key TEST-1
def targetIssue = issueManager.getIssueByCurrentKey("SL-8")
def customField = customFieldManager.getCustomFieldObjects(targetIssue).find {it.name == "Total intervention M-1"}
if (customField) {
def changeHolder = new DefaultIssueChangeHolder()
customField.updateValue(null, targetIssue,
new ModifiedValue(targetIssue.getCustomFieldValue(customField), value.toString()),changeHolder)
log.debug "Custom field $customField.name} updated with value ${value}"
return
}

log.warn "Custom field ${customField?.name} was not updated"

 


}
else
{
println "WE ARE NOT IN CURRENT DAY"
}

0 votes
JamieA Apr 17, 2016

the question? Or would you prefer me just to go through all that code, work out what you want it to do, then work out why it's not doing that, rewrite it, and post it back here?

0 votes
Robi Anton Apr 18, 2016

@Jamie Echlin [Adaptavist]

 

Nothing to do. I wanted to inform you that my script is working. 
 You have nothing to do. 

 

0 votes
JamieA Apr 18, 2016

ok, that's what I like to hear. Thanks for letting us know then.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in Off-topic

Disney Live Action Reboots: Are you for or against?

Happy Friday Everyone! Today marks the international release of Disney's live action version of the animated classic Aladdin. I know that this movie was met with some controversy of over cast...

139 views 19 8
Join discussion

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