ScriptRunner: Creating a Worklog through a transition

I have seen other pages as reference and I have been trying to implement the WorklogImpl and/or WorklogImpl2 in scriptrunner. It keeps giveing me an error on this line of code:

worklog = new WorklogImpl2(issue, null, issue.reporter.name, issue.summary, new Date(), null, null, 27000, null)

or

worklog = new WorklogImpl(worklogManager, issue, null, issue.reporter.name, issue.summary, new Date(), null, null, 27000)

I am wondering if I need to import a plugin or if the inputs are not correct. 

2 answers

1 accepted

Hi everyone, this is how I created a worklog on transition. This doesn't log work on the weekends and only logs work on 7.5 hour days. Enjoy everyone and thanks for the help. 

import com.onresolve.scriptrunner.runner.ScriptRunnerImpl;
import com.onresolve.scriptrunner.runner.customisers.PluginModule;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import java.lang.Object
import com.atlassian.jira.issue.worklog.WorklogImpl2
import java.text.SimpleDateFormat
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.DefaultWorklogManager
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.issue.worklog.Worklog
import com.atlassian.jira.datetime.LocalDate


def componentManager = ComponentManager.getInstance()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfend = customFieldManager.getCustomFieldObjectByName("End Date")
def cfstart = customFieldManager.getCustomFieldObjectByName("Start Date")
def cfdays = customFieldManager.getCustomFieldObjectByName("Used vacation days")
def reporter = issue.reporter.name
def worklogManager = ComponentAccessor.getWorklogManager();


//Take customfield date object and change it to days
int days = (issue.getCustomFieldValue(cfdays) as int)//return the number of days
int timetracker =0;

ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager
def UsersRole = projectRoleManager.getProjectRole("Users")
long timespent = 27000
int counter = 0;
int check = 0;
 for(int i=0; check == 0 ;i++){
//Checks if it is sunday
  if(((issue.getCustomFieldValue(cfstart) as Date) + i).getDay() == 0){
  i++
          def worklog = new WorklogImpl2(issue, null, issue.reporter.name, issue.summary, (issue.getCustomFieldValue(cfstart) as Date)+ i, null, null,timespent, UsersRole)
    worklogManager.create(issue.reporter, worklog, 0L, true)  
 }
//Checks if it is Saturday
 else if(((issue.getCustomFieldValue(cfstart) as Date) + i).getDay() == 6){
  i = i + 2
  def worklog = new WorklogImpl2(issue, null, issue.reporter.name, issue.summary, (issue.getCustomFieldValue(cfstart) as Date) + i, null, null,timespent, UsersRole)
    worklogManager.create(issue.reporter, worklog, 0L, true)
 }  
//Any other day in the week  
 else{
  def worklog = new WorklogImpl2(issue, null, issue.reporter.name, issue.summary, (issue.getCustomFieldValue(cfstart) as Date) + i, null, null,timespent, UsersRole)
    worklogManager.create(issue.reporter, worklog, 0L, true)
 }

 counter++
 if(days == counter){
  check =1;
 }
            
}


return 0;
0 vote

What is the error you are seeing in the logs?

Have you seen our documentation here on doing exactly this? It would be useful to follow that example first and see if it works for you.

I got it to work actually. I didn't post the code because it hasn't been fully completed but I was able create worklogs by performing a transition.  When the code is fully complete, I will try and post it online.

Thank you for the help and suggestion

 

No problem. Great that would probably help people with a similar issue to yours.

I just faced an issue and I am wondering if you know how to fix it. JIRA seems to be running out of memory after I began scripting useing scriptrunner. I am using a JIRA server and it prevents me from logging on to the server. After further notice we realized its because the memory seems to max out. We restarted the server and it works fine, but after some time the memory maxs out again and then I can't log on again. My code is posted below: 

import com.onresolve.scriptrunner.runner.ScriptRunnerImpl;
import com.onresolve.scriptrunner.runner.customisers.PluginModule;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import java.lang.Object
import com.atlassian.jira.issue.worklog.WorklogImpl2
import java.text.SimpleDateFormat
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.worklog.WorklogImpl
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.DefaultWorklogManager
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.issue.worklog.Worklog
import com.tempoplugin.core.workattribute.api.WorkAttributeService
import com.tempoplugin.core.workattribute.api.WorkAttributeValueService


//Tempo Plugin
//@WithPlugin("is.origo.jira.tempo-plugin")

@PluginModule
//WorkAttributeService workAttributeService

def componentManager = ComponentManager.getInstance()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfend = customFieldManager.getCustomFieldObjectByName("End Date")
def cfstart = customFieldManager.getCustomFieldObjectByName("Start Date")
def cfdays = customFieldManager.getCustomFieldObjectByName("Used vacation days")
def reporter = issue.reporter.name
def worklogManager = ComponentAccessor.getWorklogManager();


//Take customfield object and change it to time

double stDate = (issue.getCustomFieldValue(cfstart) as Date).getTime()//time in milliseconds
double enDate = (issue.getCustomFieldValue(cfend) as Date).getTime()//time in milliseconds
double timediff = (enDate - stDate)/1000 // returns the time difference in seconds
int days = (issue.getCustomFieldValue(cfdays) as int)//return the number of days
timediff = Math.round(timediff)

 

ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager
def UsersRole = projectRoleManager.getProjectRole("Users")
long timespent = 27000
for(int i=1; i<=days;i++){
def worklog = new WorklogImpl2(issue, null, issue.reporter.name, issue.summary, (issue.getCustomFieldValue(cfstart) as Date) - 1 + i , null, null,timespent, UsersRole)
worklogManager.create(issue.reporter, worklog, 0L, true) 
}

return 0;

There might be another reason why this is occuring, but I just want to double check to make sure that it is not this line of code. We are using JIRA 7.3.1

 

the @PluginModule is also commented out (//@PluginModule)

I can't see anything wrong with your script.

Is it possible that there are event listeners setup that are being triggered in a loop? Is it only when you use this script you get out of memory?

If you have a paid license for ScriptRunner you can open up a support request through our portal here and we'll take a closer look for you. Please attach thread dumps and heap dumps when this problem occurs so we can analyse them.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

616 views 6 12
Read article

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