Javascript to find and replace and then update fields

  • 0
  • 1
  • Question
  • Updated 3 months ago
  • In Progress
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?


Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb

Posted 3 months ago

  • 0
  • 1
Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb
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.
Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb
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.
Photo of Harrison Hersch (QB)

Harrison Hersch (QB), Quick Base Sr. Product Manager (Platform)

  • 1,478 Points 1k badge 2x thumb
Hi Wendy. What is the goal you are trying to achieve? What is the use case?
Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb
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. 
(Edited)
Photo of Harrison Hersch (QB)

Harrison Hersch (QB), Quick Base Sr. Product Manager (Platform)

  • 1,478 Points 1k badge 2x thumb
Thanks. Have you tried a Quick Base Automation to do this natively?
Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb
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_.
Photo of Harrison Hersch (QB)

Harrison Hersch (QB), Quick Base Sr. Product Manager (Platform)

  • 1,478 Points 1k badge 2x thumb
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.
Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb
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?
Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb
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])
...
(Edited)
Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb
What is wrong with this formula?

var text thisText = [Body];
$thisText = SearchAndReplace( $thisText, "[PC]", [Case - PC Full Name]);
$thisText
Photo of QuickBaseCoach App Dev./Training

QuickBaseCoach App Dev./Training, Champion

  • 67,278 Points 50k badge 2x thumb
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.
Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb
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. 
Photo of QuickBaseCoach App Dev./Training

QuickBaseCoach App Dev./Training, Champion

  • 67,278 Points 50k badge 2x thumb
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
Photo of QuickBaseCoach App Dev./Training

QuickBaseCoach App Dev./Training, Champion

  • 67,278 Points 50k badge 2x thumb
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




Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb
So you can't write a new value to the variable using a function?  You need to declare a new variable on each line? 
Photo of QuickBaseCoach App Dev./Training

QuickBaseCoach App Dev./Training, Champion

  • 67,278 Points 50k badge 2x thumb
yes, exactly.  you can use short names if you like, such as 
A
B
C
D etc
Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb
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. 
Photo of QuickBaseCoach App Dev./Training

QuickBaseCoach App Dev./Training, Champion

  • 67,278 Points 50k badge 2x thumb
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. :)
Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb
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. 
Photo of QuickBaseCoach App Dev./Training

QuickBaseCoach App Dev./Training, Champion

  • 67,278 Points 50k badge 2x thumb
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
Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb
Ya, I read through that but it doesn't indicate that you can't change the value of a variable once it's set.