Discussions

Expand all | Collapse all

Save javascript generated value to a field

  • 1.  Save javascript generated value to a field

    Posted 08-22-2020 14:30
    Hi

    Long story short but my team uses decimals to track a length, but we need to send reports to an external vendor that has the length formatted as a fraction.  Example:

    Field (Numeric) "Internal Length in Inches": 6.625
    Field (Text) "Vendor Length in Inches - Helper": 6-5/8

    I haven't been able to find a GCD function in Quick Base that I can use for a formula but I did come up with a javascript in a rich text formula field that converts the decimal into the formatted fraction (below).

    Now I need to be able to save the returned JS value as text in a "Vendor Length in Inches" (text) field. 

    I've tried:
    • Injecting the returned value into the form field via javascript but it doesn't save the value, just blank.
    • Using a field formula to grab the helper field, but that only saves the script code.
    Has anyone had any experience with something similar to this?

    Thank you
    "<img src onerror=' this.outerHTML=( function(x) { var decimal = x; var decimal_string = decimal.toString(); var decimal_array = decimal_string.split(&quot;.&quot;); var whole_number = parseInt(decimal_array[0]); var numerator = parseInt(decimal_array[1]); var fraction_power = numerator.toString().length; var denominator = Math.pow(10, fraction_power); var gcd; var a = numerator; var b = denominator; while (a!=b){ if (a>b){ a = a -b; } else { b = b - a; } } gcd = a; var vendor_length = whole_number+&quot;-&quot;+numerator/gcd+&quot;/&quot;+denominator/gcd; document.getElementById(&quot;_fid_9&quot;).value=vendor_length; return vendor_length; } ) (" & [Internal Length] & "); '>"​​

    ------------------------------
    Lorelei B
    ------------------------------


  • 2.  RE: Save javascript generated value to a field

    Posted 08-24-2020 15:32
    Would you want a script to run each time you opened a record and to then place the derived value into a blank field you have, if it is blank(or if the derived value has changed from what is already saved maybe?) Or how did you envision this working?

    You cannot just insert a value into an element like that, it is only a visual representation at that point. You need to make an API call to Quick Base for that specific record you are on. So what you would want to do is run something like the Image On Load thing, as you are, but have it load your script from a variable like it expects. Then in the script you will want it to do the stuff you have set but then also make a call to the Quick Base API and do an EditRecord call on the record id you are on and have it set field 9(from your code, I think) to be the value in vendor_length. Then you will probably want to refresh the page and the record should be updated.

    There may be other or better ways of doing this too. It might be possible to transform the data in Excel or similar programs too, if it was not a thing you needed done on every record.