groovy script from scheduler

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

@Jamie Echlin [Adaptavist]

It's not exactly the same.

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

@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"
}

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?

@Jamie Echlin [Adaptavist]

 

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

 

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted yesterday in United States

Topic Tuesday: What's your favorite topic?

Good morning All, Our goal is to get you into the habit of while enjoying your favorite morning drink you are checking the NOVA "space" for topics and comments. Your input is really needed and...

41 views 4 0
View post

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you