What's erpnext equivalent to formula or calculated fields?

140 views
Skip to first unread message

Jev Björsell

unread,
Dec 30, 2013, 12:01:33 PM12/30/13
to erpnext-u...@googlegroups.com
Hi,

I want to add a custom field to the Item doctype that is a calculated field. It is the product of the multiplication of two other fields on the same object.

Here's an example where I have two custom fields on Item:
width_mm
height_mm

I want to add a new field called area which is the product of width_mm * height_mm

What's the erpnext way of achieving this?
Thanks,
-Jev

Maxwell Morais

unread,
Dec 30, 2013, 12:06:30 PM12/30/13
to erpnext-u...@googlegroups.com

​​
you can add a custom script inside the your calculation fields
​ base​
:

function compute(doc, cdt, cdn){
    if (doc.width_mm && doc.height_mm){ //it's check if the fields contains a value
        doc.result_field = doc.width_mm * doc.height_mm;
    }
}

cur_frm.cscript.custom_width_mm = compute;
cur_frm.cscript.custom_height_mm = compute;


2013/12/30 Jev Björsell <ebjo...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "ERPNext User's Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-user-fo...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

Maxwell Morais
Tecnologia da Informação

Jev Björsell

unread,
Dec 30, 2013, 1:27:40 PM12/30/13
to erpnext-u...@googlegroups.com
Thank you Maxwell.

It is worth noting that this approach will allow the area to be calculated when a user is using the web-interface, but if we a record is created by the API then this calculation will be skipped.

-Jev



On Monday, December 30, 2013 9:06:30 AM UTC-8, Maxwell wrote:

​​
you can add a custom script inside the your calculation fields
​ base​
:

function compute(doc, cdt, cdn){
    if (doc.width_mm && doc.height_mm){ //it's check if the fields contains a value
        doc.result_field = doc.width_mm * doc.height_mm;
    }
}

cur_frm.cscript.custom_width_mm = compute;
cur_frm.cscript.custom_height_mm = compute;


2013/12/30 Jev Björsell <ebjo...@gmail.com>
Hi,

I want to add a custom field to the Item doctype that is a calculated field. It is the product of the multiplication of two other fields on the same object.

Here's an example where I have two custom fields on Item:
width_mm
height_mm

I want to add a new field called area which is the product of width_mm * height_mm

What's the erpnext way of achieving this?
Thanks,
-Jev

--
You received this message because you are subscribed to the Google Groups "ERPNext User's Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-user-forum+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Maxwell Morais

unread,
Dec 30, 2013, 1:38:09 PM12/30/13
to erpnext-u...@googlegroups.com

Hi, Jev! You are right!

You must edit the code controler, in “mydoctype.py” i.e “accounts/doctype/account/account.py”

You must a custom code, inside the Doctype class

It is interesting that at the time of your validation function is called, see the code below.

https://github.com/webnotes/erpnext/blob/develop/accounts/doctype/account/account.py#L28

I refer to the validation because it does not matter if your doctype is being created or updated.

Your code would look something like below


from webnotes import cflt

class DocType:
    def validate(self):
        ....
        self.compute()

    def compute(self):
        if not self.doc.height_mm or not self.doc.width_mm:
            msgprint(_('Please, input the width and the height!'), raise_exception=1) 
           # raise_exception=1 stops the code, and raise the error
        self.doc.computed_field = cflt(self.doc.width_mm) * cflt(self.doc.height_mm)


2013/12/30 Jev Björsell <ebjo...@gmail.com>
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-user-fo...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Jev Björsell

unread,
Dec 30, 2013, 1:49:32 PM12/30/13
to erpnext-u...@googlegroups.com
Thank you Maxwell, this is very useful, you saved me some time!

I may try and get by without the server side modifications until 4.0 lands, and there I'll try to do it with a custom module. I think this will be easier to maintain as more updates come down the line.

-Jev


2013/12/30 Jev Björsell <ebjo...@gmail.com>


2013/12/30 Jev Björsell <ebjo...@gmail.com>

To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-user-forum+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--

Maxwell Morais
Tecnologia da Informação

--
You received this message because you are subscribed to the Google Groups "ERPNext User's Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-user-forum+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Rushabh Mehta

unread,
Dec 31, 2013, 8:11:06 PM12/31/13
to erpnext-u...@googlegroups.com
Yes in 4.0.0, all your customizations will be a part of your own "app" - this will also make them easily deployable.

Then via hooks.txt you can listen on events.

Checkout how we stripped out the "demo" from erpnext

Jev Björsell

unread,
Jan 1, 2014, 9:54:03 PM1/1/14
to erpnext-u...@googlegroups.com
Very nice.

Can we create custom fields on existing objects?
How/where do we put unit tests and integration tests?

If 4.0 was available now, and stable, I would consider writing my data migration scripts as a module. I would have it set up all my custom fields, and import all my data (from salesforce export files). With unit tests, and integration tests, I would be able to set up and tear down a new instance of erpnext with one command, and use that for UAT. I would have a very tight loop for user feed back.

My current situation is that I have done a lot of setup manually, and I'm afraid of breaking something. Writing all my migration scripts would make starting from scratch very cheap for me. :)

Do you have a target/hopeful date for releasing 4.0?

Thanks,
-Jev






--
You received this message because you are subscribed to a topic in the Google Groups "ERPNext User's Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/erpnext-user-forum/TLhFXnBiWS0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to erpnext-user-fo...@googlegroups.com.

Rushabh Mehta

unread,
Jan 3, 2014, 2:00:43 AM1/3/14
to erpnext-u...@googlegroups.com
Jev,

We are almost there in terms of development. Since we have changed method routing (all methods are now "erpnext.selling.." instead of "selling.." we need to do a full round of manual testing. Let me check internally how fast we can put out a release candidate.

We are also taking the opportunity to break up the core accounting / selling code into functional elements (right it is not very easy to figure the class hierarchy and debug). We were hoping to put this in with the release.

Thanks for the reminder - We will have a discussion on this soon.

Also Max has been trying out 4 - will be happy to get his views on how quickly we can release.

Max - can you weigh in?

best,
Rushabh

Maxwell Morais

unread,
Jan 3, 2014, 6:56:26 AM1/3/14
to erpnext-u...@googlegroups.com
I've been watching and experiencing each new feature ERPNext assiduously.

Before even venturing out for 4.0.0, I was already excited with the customized server scripts that allow a breakdown of my code out of ERPNext.

Compared to 4.0.0, I'm crawling, but I see great facilities such as triggers in hooks.txt, and breaking modules in applications.

Jev, in my view, you can start your migration to 4.0.0 right now even with the custom scripts served.

This will create a folder called plugins in your repository, within it, you can migrate your customizations through inheritance, replacing or extending the DocTypes.

You can also add custom javascript code, following the same pattern Client Custom Code (cur_frm.cscript.custom_method).

Your tests, unit may be included in your own customization.

In my view, you just need to validate your changes, on the premise that the code ERPNext is guaranteed as tested.

Integration tests are, in my view a desired future for all. But it's still a bit early to talk about something.

I've had my experiences, and still realize basic integration testing through Salad (https://github.com/salad/salad), ie https://gist.github.com/MaxMorais/7464629

But in my opinion, is kind of hard to talk about integration testing, given that I needed to make some changes in my fork, to allow such integrations.

Jev, honestly, after 1 year and a half working with ERPNext, I can assure you one thing.

As much as the future of 4.0.0 is a grand promise, and that will bring major changes in the last fork 3.x, has all the necessary resources to prepare you safely to 4.0.0.

Looking a little content posted here by Pratik Vidias, if you have the opportunity, talk a few minutes with him on Plugins 3.x, and you'll notice that already has the necessary to start to change.




2014/1/3 Rushabh Mehta <rme...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "ERPNext User's Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-user-fo...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Rushabh Mehta

unread,
Jan 4, 2014, 12:33:02 AM1/4/14
to erpnext-u...@googlegroups.com
Thanks Max for the positive feedback :)

We are excited too about version 4. I think it will also release a lot of energy for us.... Jev here is a getting started doc:


Please post if you have any questions.



2014/1/3 Rushabh Mehta <rme...@gmail.com>
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-user-forum+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Jev Björsell

unread,
Jan 5, 2014, 6:15:44 PM1/5/14
to erpnext-u...@googlegroups.com
Thanks for all the great information guys. I'm looking forward to it.

I'm going to stick with the 3.X branch for now, as getting live on ERPNext is the priority, and I don't want to add risk from a pre-release version.
Reply all
Reply to author
Forward
0 new messages