Discussions

Expand all | Collapse all

Javascript to find and replace and then update fields

  • 1.  Javascript to find and replace and then update fields

    Posted 03-05-2019 03:36
    I have a 2 part issue:

    Using the [iol] method, I have a mod.js file and am calling it in my form.

    Issue #1:

    I am able to retrieve the data in some of the fields using 

    var intro = _fid_15.value;

    However, using var name = _fid_66.value; does not work.
    Upon inspection, there isn't a field with id _fid_66 even though the data is being displayed. There is a label for="_fid_66" with a subsequent div following with the info. The div has an id of "tdf_63" (which I am assuming is the 63rd field on the form as the field next field is tdf_64 and the one after that is tdf_65).

    Using getElementById().label does not work as there isn't an element with _fid_66.

    We are in constant development right now and there is absolutely no guarantee that the fields will remain in the order they are in (in fact I can 100% assume not).

    Question #1:
    How do I get the value in this div into a variable without knowing the exact number on the form? 


    Issue #2:

    Ignoring issue #1, I have changed "var intro" and now want to put the new value back into the field. 

    Using "var intro = _fid_15.value;" I then change the value. Then using "_fid_15.value = newValue" and saving the form (via saveAndKeepWorkingMenuOption.click();).

    Updated value does not get saved.


    Question #2:
    What step am I missing to change and save the data?




  • 2.  RE: Javascript to find and replace and then update fields

    Posted 03-05-2019 03:39
    Want to mention that _fid_15 is a field in the table associated with the form, _fid_66 is a related table field.
    Also fields that I will be changing will all be rich-text fields that will also need to be edited later by user.


  • 3.  RE: Javascript to find and replace and then update fields

    Posted 03-05-2019 04:18
    Field gets changed in form when I run the script straight from load, but it fails when I run the same script in a function.


  • 4.  RE: Javascript to find and replace and then update fields

    Posted 03-05-2019 14:49
    Hi Wendy. What is the goal you are trying to achieve? What is the use case?


  • 5.  RE: Javascript to find and replace and then update fields

    Posted 03-05-2019 16:20
    Overview:
    Template stored in a table with multiple languages and place holders for field values using rich text fields.
    Template copied with selected language.
    Run script to find and replace place holders with the values for the related case when user clicks a button.

    I have everything except pulling in the table field values for the elements without an html element ID of the table field id. 


  • 6.  RE: Javascript to find and replace and then update fields

    Posted 03-05-2019 16:57
    Thanks. Have you tried a Quick Base Automation to do this natively?


  • 7.  RE: Javascript to find and replace and then update fields

    Posted 03-05-2019 17:00
    I can't see how an automation would work to find placeholders in the rich text field and replace them.

    I use automations and webhooks for much of  my workflow including copying the templates among other things.

    I'm just needing help with what I stated above... reading data from html elements that don't have a corresponding #_fid_.


  • 8.  RE: Javascript to find and replace and then update fields

    Posted 03-05-2019 17:03
    Hi Wendy. I don't know the details of the use-case, but Automations definitely can find and edit records. We also released _SearchAndReplace()_ as a formula function. Wherever possible, we would encourage using supported methodologies vs. image onload.


  • 9.  RE: Javascript to find and replace and then update fields

    Posted 03-05-2019 17:13
    I think you might have helped me down a better path!
    I'd rather not use .js [iol] anyways. 

    • Create formula rtf fields for each of the rtf fields
    • Use _SearchAndReplace() for the placeholders
    • When user clicks button, run automation that saves the formula field values into the regular fields
    Question: do the formula rtf fields have to be displayed in order for them to work properly?


  • 10.  RE: Javascript to find and replace and then update fields

    Posted 03-05-2019 18:02
    So the serchAndReplace works but for only one variable 

    For example my text says:

    [Date]

     

    To: [PC]
    [Delivery Method]

    Case: [Case ID]

    Client: [Client]

    There are three separate rtf and approximately 25 different variables (may be more as we progress and need this to be an option to be able to implement on the fly as new templates are created).

    So I need to be able to iterate over the text such as 

    Text = [rff field]
    Text = SearchAndReplace( Text, "[PC]", [Case - PC Full Name])
    Text = SearchAndReplace( Text, "[Delivery Method]", [Delivery Method])
    Text = SearchAndReplace( Text, "[Case ID]", [Case - Case ID])
    ...


  • 11.  RE: Javascript to find and replace and then update fields

    Posted 03-07-2019 00:34
    What is wrong with this formula?

    var text thisText = [Body];
    $thisText = SearchAndReplace( $thisText, "[PC]", [Case - PC Full Name]);
    $thisText


  • 12.  RE: Javascript to find and replace and then update fields

    Posted 03-07-2019 02:28
    Wendy, are  you saying that the formula has invalid syntax and won't save or does not work.  I have used upper and lower case in the formula variable names and they work fine.

     and btw, you can chain these formula variables so each one refers to the previous one, so that you can successively replace the fields.


  • 13.  RE: Javascript to find and replace and then update fields

    Posted 03-07-2019 02:57
    I get an error.
    Inside the formula it says "Expecting text but found Boolean" 

    On save I get this error:

    There are extra characters beyond the end of the formula.


    var text thisText = [Body];
    $thisText = SearchAndReplace( $thisText, "[PC]", [Case - PC Full Name])

      $thisText

    I have tried it with and without a semi-colon after the ) on the line of $thisText. 

    FYI: I did just write a .js script file using the [iol] method to do all of the search and replace, format the dates, and then write the value back to the field. After a little bit of tinkering with your API, it all was pretty simple. I know you have answered a couple of my other questions (it's all related) and want to say thanks. 


  • 14.  RE: Javascript to find and replace and then update fields

    Posted 03-07-2019 03:01
    Sorry, I was being a bit stupid, but also you did not post your error message.

    var text thisText = [Body];

    var text PCReplace = SearchAndReplace( $thisText, "[PC]", [Case - PC Full Name]);

     $PCReplace


  • 15.  RE: Javascript to find and replace and then update fields

    Posted 03-07-2019 03:06
    So you can't write a new value to the variable using a function?  You need to declare a new variable on each line? 


  • 16.  RE: Javascript to find and replace and then update fields

    Posted 03-07-2019 03:06
    Then once that is working you grow the formula larger and feed the previous formula variable into the next.

    var text thisText = [Body];

    var text PCReplace = SearchAndReplace( $thisText, "[PC]", [Case - PC Full Name]);

    var text DeliveryReplace = SearchAndReplace($PCReplace, "[Delivery Method]",[Delivery Method]);

    var text CaseIDReplace = SearchAndReplace($DeliveryReplace, "[Case ID]", [Case - Case ID]);

    $CaseIDReplace






  • 17.  RE: Javascript to find and replace and then update fields

    Posted 03-07-2019 03:07
    yes, exactly.  you can use short names if you like, such as 
    A
    B
    C
    D etc


  • 18.  RE: Javascript to find and replace and then update fields

    Posted 03-07-2019 03:13
    Good to know for future. 

    The benefit to the .js file is that I created a function that I can pass in a table, RID, and field variables so I have started using the same file in multiple places. 


  • 19.  RE: Javascript to find and replace and then update fields

    Posted 03-07-2019 04:05
    When you only have a hammer, every problem looks like a nail.  I do  not come from a coding background so I'm a native kind of guy so I solve native.  I also rationalize that apps will be more easily supported in the future when your eventual successor inherits support for it,  if there is less non native code, but to each their own. :)


  • 20.  RE: Javascript to find and replace and then update fields

    Posted 03-07-2019 04:18
    I'm a web developer but in my current position I'm now the database girl! I always try to use native solutions because it is easier for support questions (and at worst, if I find a bug I can report it). Between the date formatting and having to use this same formula on multiple fields just once on adding a record across a few tables, it just seemed easier to only have to update one file instead of all the extra fields if we added a new variable.  That's my DRY coding  practices at play. 

    I couldn't find any documentation anywhere on the variable thing. I honestly tried looking and Google searching for quite a bit trying for a native solution.  I probably would have used this method and made it work, but in the end it was probably good that I dove into the API anyways. I have some future integrations to do, so at least I got some practice. 

    Coming from Access or PHP/MySQL, I'm not used to having limitations. Quick Base has a learning curve but luckily there's more than one way to accomplish anything. 


  • 21.  RE: Javascript to find and replace and then update fields

    Posted 03-07-2019 04:30
    We come from different directions and just meet in the middle somewhere.  Here is some help on formula variables.

    https://help.quickbase.com/user-assistance/formula_variables.html


  • 22.  RE: Javascript to find and replace and then update fields

    Posted 03-07-2019 04:33
    Ya, I read through that but it doesn't indicate that you can't change the value of a variable once it's set.