# Adding Macro Functionality

**URL:** https://community.zammad.org/t/adding-macro-functionality/3632
**Category:** Development
**Created:** [December 12, 2019, 11:16am UTC](https://community.zammad.org/t/adding-macro-functionality/3632 "2019-12-12T11:16:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![fleverest](https://community.zammad.org/user_avatar/community.zammad.org/fleverest/32/2204_2.png) [@fleverest](https://community.zammad.org/u/fleverest)
#### Post date: [December 12, 2019, 11:16am UTC](https://community.zammad.org/t/adding-macro-functionality/3632/1 "2019-12-12T11:16:48Z")

</div>

Disclaimer: This is the first open source project that I’ve contributed to, and I have never developed on ruby on rails before this.

I am working on this feature request:

> [@Relative time and date for "Pending till" in Macros](https://community.zammad.org/t/relative-time-and-date-for-pending-till-in-macros/1748):
>
> Macros are extremely useful, however the function “Pending till” only allows the entry of absolute dates and times, which makes it useless in almost all cases. It would be great to have the possibility to set “Pending till” to relative times and dates like “in 24 hours”, “in 2 months” ecc.

I have added a new macro via the rails console as follows:

```
Macro.create_if_not_exists(
name: 'Rails rel pend',
perform: {
    'ticket.pending_time' => {
        operator: 'rel',
        value: 7,
        range: 'day',
    }
},
updated_by_id: 1,
created_by_id: 1,
ux_flow_next_up: 'none',
note: 'MACRO FROM RAILS CONSOLE',
active: true,
)

```

The intended functionality of this macro is to set the ticket “pending\_time” to a relative date, rather than a statically selected one. Unfortunately, the assets will need to be changed to handle such a macro.

In a test environment (docker) I have implemented a change in the following file:

> <https://github.com/zammad/zammad/blob/develop/app/assets/javascripts/app/models/ticket.coffee#L118>

This is the code I have added:

```
# apply pending_date
if attributes[1] is 'pending_time'
    pendtil = new Date
    pendtil.setDate(pendtil.getDate()+content.value)
    console.log params.ticket[attributes[1]]
    params.ticket[attributes[1]] = pendtil.toISOString()
    console.log params.ticket[attributes[1]]

```

This was intended to handle only my specific macro.

In testing my new macro, I noticed that the console will correctly log the old ‘pending\_time’ for the ticket and the new time, 7 days from now. However, I’ve encountered some strange results:

I created a new ticket (id: 2). I set it to pending reminder.

```
# App.Ticket.find(2)['pending_time']
"2019-12-13T21:00:00.000Z"

```

That’s good - it was saved.

```
# App.Ticket.macro({macro: App.Macro.find(4).perform, ticket: App.Ticket.find(2)})
Array [7]
2019-12-13T21:00:00.000Z
2019-12-19T11:11:32.865Z

```

Seems to have worked? But then afterwards I get this:

```
# App.Ticket.find(2)['pending_time']
"2019-12-13T21:00:00.000Z"

```

It didn’t change??? How can I make these changes persist? I have a gut feeling that it has to do with the Ticket.save method in the controllers, but I honestly can’t think of what I need to change.

Could someone please point me in the right direction?

---

<div class="post-metadata">

### Author: ![MrGeneration](https://community.zammad.org/user_avatar/community.zammad.org/mrgeneration/32/9260_2.png) [@MrGeneration](https://community.zammad.org/u/MrGeneration)
#### Post date: [December 13, 2019, 3:25pm UTC](https://community.zammad.org/t/adding-macro-functionality/3632/2 "2019-12-13T15:25:57Z")

</div>

Maybe @anon29869905 can help further if he finds some spare time.

---

<div class="post-metadata">

### Author: ![fleverest](https://community.zammad.org/user_avatar/community.zammad.org/fleverest/32/2204_2.png) [@fleverest](https://community.zammad.org/u/fleverest)
#### Post date: [December 16, 2019, 3:57am UTC](https://community.zammad.org/t/adding-macro-functionality/3632/3 "2019-12-16T03:57:15Z")

</div>

Incredibly silly mistake - misplacing the `else` before the `if` statement broke the block and the `pending_time` attribute was simply overwritten at the end of the macro.perform method… My apologies! I’ll create a pull request with a function which exhausts all ideal macro selections - hopefully I’ll get to this by tomorrow.
