How to 'de-stringify' JavaScript code that has been stringified? JSON.parse does not seem to work. We want to store JavaScript code in the database and then retrieve it and then eval it. Here's the code in the database:
//# sourceURL=journal.js
function onBlur(e) {
var drAmount = script.getGridRowFieldValue('debitamount', e.rowuid)
var crAmount = script.getGridRowFieldValue('creditamount', e.rowuid);
// Prevent both debit and credit from having values.
if (drAmount != undefined && crAmount != undefined) {
if (e.fieldname == 'debitamount') {
script.setGridRowFieldValue('creditamount', e, undefined)
} else if (e.fieldname == 'creditamount') {
script.setGridRowFieldValue('debitamount', e, undefined)
}
}
}
Here's what's returned:
"//# sourceURL=journal.js\r\n\r\nexport function onBlur(e) {\r\n\r\n var drAmount = script.getGridRowFieldValue('debitamount', e.rowuid)\r\n var crAmount = script.getGridRowFieldValue('creditamount', e.rowuid);\r\n\r\n // Prevent both debit and credit from having values. \r\n if (drAmount != undefined && crAmount != undefined) {\r\n\r\n if (e.fieldname == 'debitamount') {\r\n script.setGridRowFieldValue('creditamount', e, undefined)\r\n } else if (e.fieldname == 'creditamount') {\r\n script.setGridRowFieldValue('debitamount', e, undefined)\r\n }\r\n\r\n }\r\n}\r\n\r\n\r\n\r\n\r\n"
JSON.parse()helped you here. Theeval()function is what you want, but be very careful not toeval()any "outside" code. There's a reason (more than one) thateval()is not a generally-recommended practice.export function onBlur. That's not pure JavaScript - it might be part of some intermediary language like TypeScript or some ES6-inclusive compiler.