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)"
7 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!!
-
My question is why are you creating a JSON at all? From the code it doesn't seem like you are creating data that doesn't already exist in your tables.
Just to verify, a bill record can have many related payment records?
If so then can't you do something like for a message formula field:
switch count(Payments) do case 0: "No payments found for this bill" case 1:( if Payments.'Pmt Type' = "FULL" then if Arrears > 0 then "Current Due PAID IN FULL. Arrears (if any) will be paid in their respective bills" else "" end else if Payments.'Pmt Type' = "PART" then "This bill has been partially paid only" end end ) end;
There are some changes still need for lines 7 and 12 as Payments is an array that needs to be figured out.
You can see in line 1, we use the reference link to Payments so we don't have to do any selects.
-
said:
So if I understand correctly, you mean to say that 'cuz we are in the context of "Bill" table and writing this script inside a formula field on "Bill" table, we don't need to do a select on Payments and match them against that particular Bill in context -- count(Payment) will automatically return the Payment records corresponding to this current Bill ??Yes, we want to use the power of relationships. According to your pictures, there is a reference field called Payments(it has the create and add text at the bottom). Which means Ninox already knows which records in Payments are linked to this specific Bill record, so use it.
When you find yourself reaching for the select, think if you have a link you can use instead.
Of course this does not apply to global functions, which makes sense since you are not in an particular record to start.
I don't use functions so I can't help you on your other post.
Content aside
- 4 hrs agoLast active
- 7Replies
- 34Views
-
3
Following