Sharing JSON object between two formula fields in the same table
Hello
I have been able to make some progress in my project (Expense Manager) in the last few days, however I am stuck in a very weird problem at the moment.. I am writing to seek advise on this..
Requirement: Share JSON object between multiple formula fields in the same table/form
Scenario: I have two tables (Bill and Payment). I first create a new Bill and then make Payment (single or multiple) for that bill. An expense can have multiple bills and one bill can have multiple payments.
I have a button "Make Pmt" and three formula fields "Total Paid", "Bal Left" and "Message" on the Bill form. After entering the details of the new bill or opening the existing bill, I click on "Make Pmt" to create a new Payment against that bill. Then based on the amt paid (Full pmt, part pmt), I update the "Total Paid" and "Bal Left" formula fields on the Bill form.
I have created the below script on "Bal Left" formula field that validates the payments made and updates and displays the balance left to be paid on the bill. All this data (balDue, PmtCompleted, Message, PmtType and Message) is stored in a JSON object (pmtData) that I create inside the script on the "Bal Left" formula field.
let pmtData := {balDue: 0, pmtCompleted: "NO", pmtType: "NOT PAID", message: ""};
Problem: I want to display a custom message in the "Message" formula field on the Bill form after the pmt is made for that bill. The customised message is created inside the script depending upon various conditions.
The fact that "Bal Left" and "Message" are two separate formula fields in the same table, how can I access from the "Message" formula field, the "message" string of the pmtData JSON on the "Bal Left" formula field.
Is there a way to share a local JSON object between two fields on the on the same form rather than creating a global JSON object for the whole database ??
Full code below..
let currBill := this;
"-- create JSON data type for pmt details";
let pmtData := { balDue: 0, pmtCompleted: "NO", pmtType: "NOT PAID", message: "" };
"-- function to validate pmts to claculate balance left for the bill";
function validateBillPayments(billID : text,pmtData : any) do
let pmts := (select Payment where trim(upper(Bill.'Bill ID')) = trim(upper(billID)));
switch count(pmts) do
case 0:(
setItem(pmtData, "balDue", currBill.'Current Due');
setItem(pmtData, "message", "No payments found for this bill")
)
case 1:(
setItem(pmtData, "pmtType", text(pmts.'Pmt Type'));
if item(pmtData, "pmtType") = trim(upper("full")) then
setItem(pmtData, "balDue", 0);
if Arrears > 0 then
setItem(pmtData, "message", "Current Due PAID IN FULL. Arrears (if any) will be paid in their respective bills")
else
setItem(pmtData, "message", "")
end
else
if pmtData.pmtType = trim(upper("part")) then
setItem(pmtData, "balDue", number('Total Due') - number('Total Paid'));
setItem(pmtData, "message", "This bill has been part paid only")
end
end
)
end;
void
end;
validateBillPayments(currBill.'Bill ID', pmtData);
"record('Global Data',1) := formatJSON(pmtData)"
4 replies
-
You have access to the values of a JSON object for example via dot notation. Try
pmtData.message
Don't use the JSON as formatJSON(). That's just text.
-
Thanks mate for the reply, I appreciate your help
So far what I have understood is that I should either have an extra hidden formula field on the form that should ideally return the whole pmtData JSON object (as a good design) instead of one single value from inside the object.
Once I have the JSON object returned, I can then capture that object inside another script in a different formula field and extract the required value to be displayed in the new formula field !!
But I will have to repeat the script on every form. Or I can have a global function and move this script inside that function and return the JSON object from there. This way I can access that JSON object on any form in the entire app.
The only thing I am trying to find out is whether Ninox supports optional parameters or not, to make the function more flexible.
what are your thoughts on this!!
Content aside
- 6 hrs agoLast active
- 4Replies
- 23Views
-
2
Following