Forums

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

Change due dates of template - scriptrunner?

Reto Eggenberger
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.
October 10, 2016

Hi all

I have a template with tasks. Kind of a timetable. 

On the template only the difference between the dates matter. Task 2 is ten days later den Task 1 ...

Now I want a possibility to change all the due dates. Say I want to start the timetable on 01.11.2016. The result should be:

  • 01.11.2016 ...
  • 10.11.2016 ...
  • 14.11.2016 ...

The same difference between 01.11.2016 and 01.01.2000 gets applied to all the due dates. Or according to the offset in brackets.

  • Ideal would be, if the date gets asked on creation of the page.
  • Or a button to change the date.
  • Or an event listener which changes all the due dates automatically.

I think with the Scriptrunner Add-On this should be possible, but I don't know where to start ...

Thanks for any hints on this.

Reto

1 answer

0 votes
Reto Eggenberger
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.
October 12, 2016

I found a way to change the values according to a offset in brackets with scriptrunner. Works perfect. 

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Calendar;
Date date = new SimpleDateFormat("dd.MM.yyyy").parse("${parameters.releasedate}");
SimpleDateFormat isord = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat rd = new SimpleDateFormat("dd.MM.yyyy");
Calendar cal = Calendar.getInstance();
String[] lines = body.split("\\</li\\>", -1);
body = "";
for (int i = 0; i < lines.length; i++) {
    Pattern p = Pattern.compile("\\[(.*?)\\]");
    Matcher m = p.matcher(lines[i]);
    while(m.find()) {
        cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, Integer.parseInt(m.group(1)));
        lines[i] = lines[i].replaceAll("01.01.2000",rd.format(cal.getTime())).replaceAll("2000-01-01",isord.format(cal.getTime()));
    }
    body = body + lines[i];
}
body;

But I made a huge mistake...

The change is only to the visual layer. The dates are stored as they are without the offset. Doesn't work with reminders and so on ...

Jonny Carter
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.
October 13, 2016

I'm a little unclear on how your template takes information about existing tasks and creates the table of dates and such. That's the critical point, as that's where you might capture the task object's ID. From your script, it looks like you're assuming that the task table is already present in the macro body, which I assume your template takes care of. Can you elaborate on the template a bit?

Reto Eggenberger
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.
October 13, 2016

Yes. You're right. The tasks are already predefined in the template (approx. 20 tasks for different users). After the tasks, there are different headings and tables and so on. But the tasks are on top framed by a macro (as you see it in the screenshot). This might help to define which tasks shall get modified with the macro.

Jonny Carter
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.
October 13, 2016

How does the template know which tasks to look at? Are they always new tasks?

Reto Eggenberger
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.
October 13, 2016

Since it has to change the tasks (not only the 'visual layer' as my script above does), it would have to trigger only once. Maybe on creation of the new page from template (if you can input the release date). Or manual triggering with a button (or webitem) on the page (+ input dialog for the release date).

Jonny Carter
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.
October 13, 2016

What if your macro parameter just took a task ID (or list of task IDs) as a parameter? You could also provide the number of days to add/subtract as a parametr. Then it would lay out the table for you based on the task dates.

Since the WorkBox is a plugin, you'll need the @WithPlugin and @PluginModule annotations.

import com.atlassian.mywork.service.TaskService
import com.atlassian.mywork.model.TaskBuilder

@WithPlugin('com.atlassian.mywork.mywork-common-plugin')

@PluginModule
TaskService taskService

TaskBuilder taskBuilder = new TaskBuilder()
def task = taskBuilder.user("whoever").createTaks() //use more of the methods on the TaskBuilder class to set your tasks' details
 
taskService.createOrUpdate("whoever", task)

See the API documentation for TaskBuilder, Tasks, and so on at 

https://docs.atlassian.com/mywork-api/1.0.3/com/atlassian/mywork/model/TaskBuilder.html
https://docs.atlassian.com/mywork-api/1.0.3/com/atlassian/mywork/model/Task.html 
https://docs.atlassian.com/mywork-api/1.0.3/com/atlassian/mywork/service/TaskService.html  

That would create or update a task, which you could then render your list from.

Not quite a full solution, but hopefully enough to get you started.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events