# On\_change is taking the old field value

**URL:** https://discuss.tryton.org/t/on-change-is-taking-the-old-field-value/3069
**Category:** Developer
**Tags:** accounting
**Created:** [July 25, 2020, 3:33am UTC](https://discuss.tryton.org/t/on-change-is-taking-the-old-field-value/3069 "2020-07-25T03:33:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![German](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/german/32/143_2.png) [@German](https://discuss.tryton.org/u/German)
#### Post date: [July 25, 2020, 3:33am UTC](https://discuss.tryton.org/t/on-change-is-taking-the-old-field-value/3069/1 "2020-07-25T03:33:50Z")

</div>

Dear Tryton team.  
I’m working on a discount functionally for invoice:

1. User has to input the % of discount
2. Tryton will calculate the amount of this particular discount ( from payable amount and I need this field on db , so I can’t use function field)
3. I’ll show a total-discount (using a function field)

Everything seems good, but I’m doing something wrong with the on\_change method, when I put 10% and then let’s say I change for 20% the invoice\_discount\_amount field is calculated with 10%, like calculated with previous value of % in all cases.

Any help will be appreciated:

```
class Invoice(metaclass=PoolMeta):
    __name__ = 'account.invoice'

    invoice_discount = fields.Numeric('Descuento %')
    invoice_discount_amount= fields.Numeric('Monto del descuento')
    invoice_with_discount = fields.Function(fields.Numeric('Total con descuento',depends=['invoice_discount_amount'
        ,'total_amount']),'amount_with_discount')

    def amount_with_discount(self, name):
        try:
            amount_with_discount = self.total_amount - self.invoice_discount_amount
        except:
            amount_with_discount = self.total_amount
        return amount_with_discount

    @fields.depends('untaxed_amount','tax_amount','invoice_discount')
    def on_change_invoice_discount(self, name=None):
        if self.untaxed_amount:
            amount = self.untaxed_amount + self.tax_amount
            discount_amount = Decimal(self.invoice_discount/100) * amount
            self.invoice_discount_amount = discount_amount

```

---

<div class="post-metadata">

### Author: ![German](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/german/32/143_2.png) [@German](https://discuss.tryton.org/u/German)
#### Post date: [July 25, 2020, 3:24pm UTC](https://discuss.tryton.org/t/on-change-is-taking-the-old-field-value/3069/2 "2020-07-25T15:24:42Z")

</div>

Well, It’s working using the **on\_change\_with** on destination field, here’s the working method in case someone has a similar issue 😉 :

```
@fields.depends('untaxed_amount','tax_amount','invoice_discount')
def on_change_with_invoice_discount_amount(self, name=None):
    if self.untaxed_amount:
        amount = self.untaxed_amount + self.tax_amount
        discount_amount = Decimal(self.invoice_discount/100) * amount
        return discount_amount
    else:
        return 0

```

---

<div class="post-metadata">

### Author: ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)
#### Post date: [August 24, 2020, 3:24pm UTC](https://discuss.tryton.org/t/on-change-is-taking-the-old-field-value/3069/3 "2020-08-24T15:24:45Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
