Forum Discussion
15 Replies
Sort By
- _anomDiebolt_Qrew Elite1) "I looked at the new code and could not evaluate the piece of code that would query for the specific parent record or set of records in a report?"
My code queried all child records using {qid: 6} for simplicity of the demo. Again, you will have to modify the code to provide the correct context of those child records you want to concatenate to the parent.
2) "I added a new Actor (to the Movie record Dr.No) in your example database and the concatenated text did not get updated."
It shows up when I run the script anew:
3) "Would it be possible for you to add the brief comments ...?"
Sure I was just wallowing away my time watching CNN coverage of IRMA consisting of a bunch of man-in-the-street reporters ignoring their own advice to take cover.
More Notes:
(1) this piece of code simply protects all code within from creating conflicts with QuickBase's global variables:(function(){
(2) this piece of code defines a bunch of parameters that would need to be modified if you moved the code to another application
// rest of script
})();var dbid = "bm3wfa883";
(3) this piece of code defines an async function named process() and then calls that function:
var dbidMovies = "bm3wfa894";
var dbidActors = "bm3wfa9a2";
var apptoken = "cwk6g85n9e79c5kask3dyd9wi2";
$.ajaxSetup({data: {apptoken: apptoken}});async function process() {
// definition of async function
}
process();
(4) this piece of code wraps a jQuery promise in a native JavaScript promise so it can be awaited:
var xml1 = await Promise.resolve(
$.get(dbidActors, {
act: "API_DoQuery",
qid: "6"
})
);
The await keyword can only be used within an async function. The await keyword causes the function to suspend executing temporarily until the promise returns its result (xml1).
(5) this piece of code (with some console.log{}'s added for debugging) converts the XML response into an array of objects containing the same information:var data = $("record", xml1).map(function(index) {
(6) this piece of code randomly generates a new delimiter for the concatenated child records to provide a visual indication that the operation was successful:
var charactor = $("character", this).text();
var actor = $("actor", this).text();
var relatedMovie = $("related_movie", this).text();
return {charactor, actor, relatedMovie};
}).get();
console.log("\ndata:");
console.log(JSON.stringify(data2, null, " "));var delims = ",./|&#!~";
var delim = " " + delims[Math.floor(Math.random() * delims.length)] + " ";
(7) this piece of code uses the underscorejs library to reformat that data by (a) grouping child records together, (b) mapping the child records into a concatenated string, and (c) returning an array of objects:var data2 = _.chain(data)
The code above is modified with tap() methods to provide console debugging of each step.
.groupBy("relatedMovie")
.tap(function(x) {
console.log("\nafter grouping:");
console.log(JSON.stringify(x, null, " "));
})
.map(function(data, key) {
var cast = _.pluck(data, "actor").join(delim);
return {rid:key, cast: cast};
})
.tap(function(x) {
console.log("\nafter mapping:");
console.log(JSON.stringify(x, null, " "));
})
.value();
(8) this piece of code logs the final processed data which will be iterated over in the next step:console.log("\ndata2:");
(9) this piece of code iterates over each element in the data array and performs a API_EditRecord on each element of the array:
console.log(JSON.stringify(data2, null, " "));data2.forEach(async function(item) {
We could easily modify the script to concatenate all child records to the parent with one call to API_ImportFromCSV but the whole point of this demo (from my perspective) is to introduce you to Async /Await and to reinforce the idea that this technique allows you to make an arbitrary amount of asynchronous network requests written as if they were synchronous operations.
var xml2 = await Promise.resolve(
$.post(dbidMovies, {
act: "API_EditRecord",
rid: item.rid,
_fid_12: item.cast
})
);
(10) this piece of code does a final reload of the page when all the async operations are completeddocument.location.reload(true);
- SaudaFurkhanaQrew CadetHi Dan,
Thank you for the inputs. I got the new code working too and have few questions and need your guidance.
1) "My code queried all child records using {qid: 6} for simplicity of the demo."
Oh, my mistake! It was my oversight to read "qid" as "query".
My report in child table has "ask user" field to specify the parent record id. I hard coded the parent record id to test the code.
$.get(dbidActors, {
act: "API_DoQuery",
qid:"1000084",
nv:"1",
v0:"7524"
})
>>>> The concatenated text is appearing in the parent record. However, the code is looping and the execution does not break!
Meaning, in the every iteration, the output is calculated twice for each delimiter. And
This looping behavior is seen in both the "view mode" of the record#7524 and on the parent table report that contains #7524. Why is the execution not stopping when the concatenated text is displayed for #7524 the first time?
>>>> I stopped the execution after 12 iterations, please see the log file. For 12 iterations, we have output text calculated 24 times?
Please see the txt file the log output
Here is the screenshot of the output displayed in parent record and parent table report.
2) My parent and child tables have 10,000+ records each so I think I will have to execute the code for specific parent record ids ONLY and I'm not sure how to pass the parent record id or ids dynamically in the JS doquery code that uses qid (that is, a report). Can you give me some input on this?
Should I use the following code to extract the list of parent record identifiers (rids) and loop (using "for" or "._chain") through it using v0:rids in DoQuery?
var rids = $("div.QBU_Childs").map(function() {
return this.dataset.rid;
}).get();
3) "It shows up when I run the script anew"
- Can you please clarify what you mean by "run the script anew"? Movies table home page only has the concatenated text field (_fid_12). I don't see the formula text field that invokes the script. How did you run the script for all the records in parent table for the home page to reflect the new concatenated text?
Thanks
Sauda- _anomDiebolt_Qrew Elite1) "My code queried all child records using {qid: 6} for simplicity of the demo."
... My report in child table has "ask user" ... the code is looping and the execution does not break!
I can't debug this without seeing your exact code. Also, please don't run your "development code" against my demo as I don't want to have to come back and fix it or restore it to the original state. Some of my demos allow a user to add, edit or even delete records to show off the intended functionality and I don't mind small edits such as creating the "Test" actors. But you should create your own application mimicking mine and run your development code and modifications in your own application. It is a challenge creating public demos that use script when there are modifications to an application or deep workarounds.
2) I'm not sure how to pass the parent record id or ids dynamically in the JS doquery code that uses qid (that is, a report). Can you give me some input on this?
You can pass any number of parameters to process() to generalize what the async function does:async function process(qid) {
3) "It shows up when I run the script anew" Can you please clarify what you mean by "run the script anew"? Movies table home page only has the concatenated text field (_fid_12). I don't see the formula text field that invokes the script.
// definition of async function
}
var qid=7;
process(qid);
QuickBase does not have a "concatenate to parent" summary field so this thread (and the prior one) offered workarounds using script. The script has to be run immediately prior to viewing the report as child records may have changed since last viewing the report. So you have to get the scrip to run before viewing the report. This means you probably have to bind the script to a button but I have no idea where you want to place that button as that is a workflow issue unique to how you want to perform your task. Without any mention of your intended workflow I simply started the process from the console by pasting the script.
Undoubtedly you have to place the button (1) on the dashboard, {2) on some fake parent record (3) on a Tasks table record, (4) on some other parent record or perhaps even (5) on a code page.
How to configure a button to run script is a separate consideration - ask about it in a new question. The forum is a better resource for everyone if you ask simple questions that stand on their own, eliminate unnecessary details, and avoid long wandering threads.
- SaudaFurkhanaQrew CadetHi Dan,
Good Morning. Thank you for all the responses!
1) Please be assured that I'm not executing my test code against your demo tables. I just added two child records to test the functionality. All the changes/r&d is in my copy app. Should I send my code to your email address?
2) I think I'm not asking the question correctly. Let me investigate more on this.
(I need the v0 parameter value to change dynamically to single parent record id or list of parent record ids in the parent table report. I currently hard-coded it to one of the parent table record numbers.)
3) Very clear explanation, thank you! Let me investigate this on my own and I will come back to you if I have questions.
Thanks,
Sauda - _anomDiebolt_Qrew EliteMy script uses API methods and some advanced Async/Await JavaScript capabilities built into modern version of browsers - there is no GUI or page loads other than the final document.location.reload(true).
Using the <ask the user> feature and the associated URL parameters &nv and &v0 is not using script or API methods but rather loading new pages and using redirection features built into early versions of QuickBase during the Jurassic Epoch (ie dinosaurs). Continuing to use these archaic methods will hold you and everyone else back. The same goes for using &rdr in formulas URL.
The reason is simple: Using redirection methods to (1) chain together formula calculations or (2) step through workflow disturbs the entire application because the page reloads.
If you need to solicit user input prior to running a script use a dialog. as demonstrated here:
https://haversineconsulting.quickbase.com/db/bkw2ff3e3?a=q&qid=1
What is the Hylo Technique?
https://community.quickbase.com/quickbase/topics/what-is-the-hylo-technique
THIS IS NOT A TRIVIAL OR A PEDANTIC MATTER ON MY PART. THE USE OF REDIRECTION METHODS TO CHAIN TOGETHER URL FORMULAS OR TO FORCE WORKFLOW STEP EVALUATIONS WILL CONTINUE TO HOLD BACK THE EVOLUTION OF QUICKBASE AND IS PROBABLY ONE OF THE TECHNICAL REASONS QUICKBASE DID A REBOOT OF THEIR PRODUCT DEVELOPMENT STRATEGY.
STOP USING &rdr, &nexturl, &RedirectURL, &rl PARAMETERS AND 302 REDIRECTS