0

Can't calculate the GST and Balance Pmt formula fields

Hello Friends  ,

I can't wrap my head around this problem. I have four formula fields on the form

1. OrderCharges, 2. OrderTotal, 3. GST, 4. PmtBalance

Initially is was all good with OrderCharges, OrderTotal and GST

OrderCharges = sum(OrderItems.Subtotal)

OrderTotal = OrderCharges + DeliveryCharges

GST = OrderTotal / 11

Now I applied the styled() function on OrderCharges and OrderTotal. It returns a styled object, so I converted it into a number in the end. If I don't convert OrderCharges into a number, it given error while calculating OrderTotal (since OrderCharges is a styled object and I am trying to add DeliveryCharges to it to calculate OrderTotal).

After this the calculated values of OrderCharges and OrderTotal are showing fine in the formula fields, however it refuses to calculate the value of GST formula field and displays it as zero. Similarly it is not calculating the value of PmtBalance formula field as well. Some people make partial payments.

PmtBalance = number(OrderTotal) - PmtReceived

** Looks like internally it is taking the value of OrderTotal as null (I used If condition to check if OrderTotal is null, show an alert and the alert gets displayed every time even if the OrderTotal is showing a valid calculated value).

Code for OrderTotal formula field below:

let orderTotalAmount := 'Order Charges' + 'Delivery Charges' + Adjustment;
styled(text(orderTotalAmount), "#F7F8FC")

 

Code for PmtBalance formula field below:

if number('Order Total') = null then
    0
else
    number('Order Total') - 'Pmt Received'
end

 

Code for GST formula field below:

let GST := number('Order Total') / 11;
"(Includes GST of: " + GST + ")"

17 replies

null
    • Fred
    • 4 mths ago
    • Reported - view

    This post explains why you can't use styled() data directly.

    It seems kinda convoluted but I was able to get this to work:

    Formula 2 is a field that sums then applies a style:

    let x := sum('MTL ACTIVITY'.Quantity);
    styled(text(x), "green")
    

    Then in another field I put this in the code.

    let f := formatJSON('Formula 2');
    let p := parseJSON(f);
    number(item(p, "text"))

    Then it returns the value that was used in Formula 2. JSON always returns the data as any so I ended turning it back into a number.

      • Database App Developer
      • vermau81
      • 4 mths ago
      • Reported - view

       Hello mate,

      Thanks for your solution. You caught it right. This is exactly what I was looking for.

      I implemented your solution exactly given by you in my app, but I don't get the number value back. All I get is either "null" or zero.

      let f := formatJSON('Order Charges');
      let p := parseJSON(f);
      let allLineItemCharges := number(item(p, "text"));
      let orderTotalAmount := allLineItemCharges + 'Delivery Charges' + Adjustment;
      orderTotalAmount
      

       In the end the value returned for orderTotalAmount is either "null" or zero 'cuz the textbox shows $0.00 instead of $90.00 and the balance due from the customer is -$90.00 although it should be $0.00 as the customer has already made full payment of $90.00 already.

      There is another problem -- I tried to run a sample code in the console also, but nothing happens in the console. There is no output in the output window. I keep on clicking the "Run" button, but nothing happens.

      I have attached the screenshot as well.

      • Fred
      • 4 mths ago
      • Reported - view

       When I troubleshoot a line that has many components to it. I strip it back and see where it problem is.

      So I would start with:

      let orderTotalAmount := allLineItemCharges
      

      If that shows what you want then:

      let orderTotalAmount := allLineItemCharges + 'Delivery Charges'
      

      If that is OK:

      let orderTotalAmount := allLineItemCharges + 'Delivery Charges' + Adjustment;
      
      • Database App Developer
      • vermau81
      • 4 mths ago
      • Reported - view

       I have done all this and banged my head against the wall many times. The moment I hard code a value in the first line ( let f := formatJSON(styled(text(90), "#F7F8FC")); ) everything is fine and I get the desired result in OrderTotal field.

      Looks like for some reason it is not picking up the value from "OrderCharges" formula field in the first line. It is picking up null ('cuz that was what was showing in the alert)...

      What's your idea what would it not capture the value from "OrderCharges" formula field?

      • szormpas
      • 4 mths ago
      • Reported - view

       Hi, what is the code you use inside the "OrderCharges" formula field?

      • Database App Developer
      • vermau81
      • 4 mths ago
      • Reported - view

       Hello Sotirios,

      below is the snapshot of my code. Just two lines:..

      This is just to give gray background color to the formula field for UI/UX to show that this field is not manually editable..

      Also attached is the copy of my database app..

      • Fred
      • 4 mths ago
      • Reported - view

      Well then we need to go back further into the code. When we look at the variable 'f' we get: (the values will not match your posts as I choose a random record).

      {"text":"$30.00","color":"#262F4D","background":"#F7F8FC"}
      

      As you can see the value of text has a character in it, the dollar sign, so that means it not be converted into a number.

      I think the easiest way around this is to just add the first line from Order Charges to the Order Total formula and then you have the data you need.

      • szormpas
      • 4 mths ago
      • Reported - view

        Hi, you are right.   uses the prefix ($) in the 'Rate' field back to 'Order Items' table. This prefix seems to be retained both in the 'Subtotal' fx field and then into the sum() function.

      The interesting thing is that although we know that the sum() function returns a number it seems that the styled() function gets both the number and the prefix. I just wonder how is this possible.

      • Fred
      • 4 mths ago
      • Reported - view

      It goes all the way back to Order Items.Subtotal as that is formatted with a dollar sign.

      • szormpas
      • 4 mths ago
      • Reported - view

        the Subtotal fx field is not directly formatted with a dollar sign, it is the 'Rate field' that is directly formatted with a dollar sign.

      If you remove the dollar sign from the 'Rate' field of the 'Order Items' table all works fine!!

      • Database App Developer
      • vermau81
      • 4 mths ago
      • Reported - view

       But the $ sign denotes that this is a currency field. It is customary to have $ sign in any currency field to differentiate it from any other numeric field that is not currency. Every software supports that.

      What have I done wrong here by formatting a currency field (rate) to show a $ sign in it?

      • Fred
      • 4 mths ago
      • Reported - view

      I don't think it is a question of right/wrong. It is just having to deal with how the styled() function works.

      That is why I suggested you just do the sum of Order Items.Subtotal again. Since you are referencing linked records it will be quick.

      • szormpas
      • 4 mths ago
      • Reported - view

        an alternative option to what   suggests is to replace the text() function inside your script with string() function. The latter does not retain any formatting, like below:

      let allLineItemCharges := sum(OrderItems.Subtotal);
      styled(string(allLineItemCharges), "#F7F8FC")
      
      • Database App Developer
      • vermau81
      • 4 mths ago
      • Reported - view

        Thanks to both of you for helping me resolve this problem. After Fred's advise I wrote the below code to resolve the problem..

      "-----------------------------------------------------------------------------------";
      "Extract the value of charges from styled JSON object";
      "-----------------------------------------------------------------------------------";
      let f := formatJSON('Order Charges');
      let p := parseJSON(f);
      let charges := string(item(p, "text"));
      "-----------------------------------------------------------------------------------";
      "Exclude the $ sign in charges and pickup the numeric value till the end";
      "-----------------------------------------------------------------------------------";
      let sumTotalOfLineItems := number(substr(charges, 1));
      let orderTotalAmount := sumTotalOfLineItems + 'Delivery Charges' + Adjustment;
      "-----------------------------------------------------------------------------------";
      "Format the charges back to currency format before showing the value on the form";
      "-----------------------------------------------------------------------------------";
      styled(format(orderTotalAmount, "$#,##0.00"), "#F78F8FC")
      

      However I will go with the shorter solution that Sotirios has given ( using string() ).

      So from my understanding the text() function makes a snapshot of what it gets as an input parameter however the string function ignores the special characters and picks up only the ascii characters.

      I can't thank you both enough to have helped me resolve this. This problem was creating problem in my account ( balance due from the customers ) and I was not able to wrap my head around it.

      Fred, can you please enlighten me how did you find those values in the JSON set.

      {"text":"$30.00","color":"#262F4D","background":"#F7F8FC"} (empty)

      How and where can I see the values this way in the future..

      • Fred
      • 4 mths ago
      • Reported - view

      I didn't use anything special. I just changed the last line to f so I can see what is being stored in the variable.

      You should be made aware of the debugValueInfo() command. It will show you the type of data and the format that Ninox sees it in.

      So for f, you just use:

      debugValueInfo(f)

      and that will show you:

      string("{\"text\":\"$36.00\",\"color\":\"#262F4D\",\"background\":\"#F7F8FC\"}")

      If you do:

      debugValueInfo(p)

      you will see:

      any({"text":"$36.00","color":"#262F4D","background":"#F7F8FC"})

      Handy when you need to see how Ninox sees grouping and such.

      However I will go with the shorter solution that Sotirios has given ( using string() ).

      Or even shorter is just do the sum again:

      let allLineItemCharges := sum(OrderItems.Subtotal);
      let orderTotalAmount := allLineItemCharges + 'Delivery Charges' + Adjustment;
      orderTotalAmount
      

      :)

      • Database App Developer
      • vermau81
      • 4 mths ago
      • Reported - view

       I actually implemented the above solution in the end.

      My only worry is that we have to calculate the allLineItemCharges and orderTotal at least 4 times on the same form. Is it a good design to ping the same table 4 times for calculating the same values.

      1. Calculate OrderCharges

      2. Calculate OrderTotal

      3. Calculate GST

      4. Calculate BalanceDue

      Is it possible to have global variables instead so that we calculate them once and just use them 4 times, or have a global function return these two values everytime we call that function.

      • Fred
      • 4 mths ago
      • Reported - view

       

      wrote in the other post:

      Another solution is to use a non-visible intermediate formula containing only :sum(OrderItems.Subtotal)
      Then use this value to calculate ‘Order Charges’ and ‘Order Total’.

Content aside

  • 4 mths agoLast active
  • 17Replies
  • 76Views
  • 3 Following