How to execute the code in the code field from Server Script

I have define a field of type code in a doctype.
In the document i have defined a formula.

I want to evaluate the formula to obtain the result using server script.
Any Idea.

1 Like

Hi @gsarunk Have you reached a soultion for it , I’m having the same issue

Hi,

Should’nt it be something like

doc.IV * 3.6

https://frappe.io/frappejs/docs/models/formula

1 Like

You would need to use eval, but it is disabled for server scripts for security concerns

Instead, you could use eval on a client script, which is allowed

Also try virtual docfields: Virtual DocField

1 Like

Currently managing with client script. We are building a dynamic conditions that can be applied to any doctype field. We are thinking of making it as API so that this can be used by any doctype. Unfortunately via client script, it restricts the formula to that particular doctype

1 Like

Can you further explain your use case? Just curious.

Building a conditions like SAP. I need a field to be calculated by a sequence of formulas and conditions. something similar to payroll calculation in Frappe HRMS app.

Once the dynamic calculation procedure is ready you can attach it to a doctype field. this way we need not modify the code every time for field to be computed.

Seems like something you should do on a custom app, although I haven’t tried building one yet.

For calculating complex data in a doc, I’m currently just firing webhooks to another NodeJS server, making the calculations there and then updating the doc with the REST API

1 Like