Node custom formulas

From DreamFactory
Jump to: navigation, search
DreamFactoryTutorialsNode custom formulas
(Created page with "Make a field automatically be a mathematical result. Return 'total' as JSON key with value 'price' * 'tax_rate'. price and tax_rate are stored in a database, total is not. T...")
(No difference)

Revision as of 13:05, 7 September 2016

Make a field automatically be a mathematical result. Return 'total' as JSON key with value 'price' * 'tax_rate'. price and tax_rate are stored in a database, total is not.

This is a post process script that alters the response.

For this change to take effect, you have to enable modification of response in the Admin Console script editor. Check the box 'Allow script to modify request (pre-process) or response (post-process)'.

event.request.payload.resource.forEach(function(record) {
 
    if (!record.Tax) {
 
        throw "Tax is required!"
    }
 
    if (!record.Price) {
 
        throw "Price is required!"
    }
 
    record.Total = (parseFloat(record.Tax) * parseFloat(record.Price)/100) + parseFloat(record.Price);    
});