Field script is not updating value

Viewing 3 reply threads
  • Author
    Posts
  • January 12, 2024 at 3:38 PM #50302

    Cornelius Fischer
    Participant

    Hi guys – I started playing with the field scripts. My first goal, try to use TapForms to generate offers and invoices. Therefore I need to calculate taxes. In my first trial I have a form to generate an offer with 2 positions.

    Basic options like:

    • Position name
    • amount
    • hourly rate
    • sum (amount*hourly rate)

    Now I calculate with a calculation field the net-total from sum-1 and sum-2. Now my taxes script comes into play. Im using a script field with the following script to calculate the tax from the net-total. Thx to ChatGPT by the way for the script (i’m not a programmer)..

    // Aufruf der Funktion
    berechneMehrwertsteuer();
    
    // Funktion zur Rundung auf 0.05
    function roundToNearestFiveCents(value) {
    return Math.ceil(value / 0.05) * 0.05;
    }
    
    // Funktion zur Berechnung der Mehrwertsteuer mit Rundung auf 0.05
    function berechneMehrwertsteuer() {
    // Hole die Nettosumme aus dem Formularfeld
    var nettoSumme = record.getFieldValue('fld-8b1dde982f4849b7b6980b0a95b49a45');
    
    // Prüfe, ob die Nettosumme gültig ist
    if (isNaN(nettoSumme)) {
    console.error('Ungültige Nettosumme');
    return;
    }
    
    // Setze den Mehrwertsteuersatz
    var mehrwertsteuersatz = 8.1;
    
    // Berechne den Mehrwertsteueranteil
    var mehrwertsteueranteil = nettoSumme * (mehrwertsteuersatz / 100);
    
    // Runde auf 0.05 genau
    mehrwertsteueranteil = roundToNearestFiveCents(mehrwertsteueranteil);
    
    // Gib das Ergebnis in der Konsole aus
    console.log('Mehrwertsteueranteil: ' + mehrwertsteueranteil);
    
    // Rückgabe des Ergebnisses (falls notwendig)
    return mehrwertsteueranteil;
    }

     

    Now my problem, whenever I change something like the amout from pos 1 oder pos 2, the net-total is recalculated but NOT the tax. When I understand the FAQ for script field right, the script should automatically run, when the reference field (record.getFieldValue) changes.

    So why is my script not updating my tax field? 🤷‍♂️

    • This topic was modified 3 months, 2 weeks ago by Brendan. Reason: formatted the code
    January 14, 2024 at 1:40 AM #50306

    Brendan
    Keymaster

    Hi Cornelius,

    Yes, Tap Forms should run the script whenever the value for the field fld-8b1dde982f4849b7b6980b0a95b49a45 changes. Do you see any of your console log output at all when you change the value of that field?

    Is this a Field script? Do you have it set to return the proper Number type from the Script?

    Thanks,

    Brendan

    January 14, 2024 at 3:51 AM #50310

    Torsten Reim
    Participant

    Nur so eine Idee:

    Wird “nettoSumme” als String oder als Float Datentyp übergeben?

    Gruß,

    Torsten

    January 15, 2024 at 12:59 AM #50326

    Cornelius Fischer
    Participant

    Hi Brendan

    I just checked what the console is showing, when I change values. Nothing.. 🤷‍♂️ When I hit the “refresh entry list” Button on the bottom left, the script field gehts calculated.

    I attached a screenshot of my test form. I’m using number and calculation fields for calculating the net-total. This works fine. For the tax, I created a script field.

     

    @Torsten

    To be honest, I’m a photographer and not a programmer. Don’t have clue what kind of data type the script output is.. 🫣 I even don’t know how the calculated value is pasted into the form field. I only can say, if I manually activate the script, the script-form-field is updated with the correct tax value. 🤷‍♂️ So the script itself (from ChatGPT) seems to work.

    Attachments:
    You must be logged in to view attached files.
Viewing 3 reply threads

You must be logged in to reply to this topic.