I need to create a copy of a record to a separate table before changing the Revision value and deleting the contents of several fields. I've got the update to the Revision value working, and how to ...
However, you can write your own recursive script that executes in the browser using other API methods and JavaScript. The fact that the script is recursive is not really a difficulty as using a feature in JavaScript called async generators you can easily iterate through the relationship tree and at each node (ie record) of the tree perform the appropriate copy or initialization of a field. In fact, it is possible to iterate over the tree in Depth First (Inorder, PreOrder, PostOrder) or Breadth First (Level Order) order:
Here is a quick script that recursively iterates over a table which is self-related:
async function* generator(dbid, rid, clist, relatedFid) { let clistArray = clist.split("."); let queue = [rid]; while (queue.length) { let recordID = queue.shift(); yield recordID; let xml = await Promise.resolve( $.get(dbid, { act: "API_DoQuery", query: '{${relatedFid}.EX.${recordID}', clist: clist, fmt: "structured", includeRids: "1" }) ); let childRids = $("record", xml).map(function() { let data = []; clistArray.forEach(fid) { data.push($('f[id=${fid}]').text()); } return data; }).get(); queue.push(...childRids); } }
This script does not do anything other than visit each node/record in the relationship tree and yields the [Record ID#] but it could easily be adapted to any specific task such as copying or initializing fields from the records that are visited.
Scripts like this are useful in a variety of other contexts other than recursively copying records. For example, you can easily calculate total weight or cost for a hierarchical assembly modeled using a table related to itself. Or you could perform various queries on an organization chart again modeled using a table related to itself.
If you need functionality similar to this feel free to contact me off-world using the information in my profile: