Everything is possible with a script, well almost ;-)
Here is the version that doesn the replacement of strings in a field:
var field_id = 'fld-64b0b17a635f49b8b40867e45f8d24db';
function findAndReplace(find_text, replace_with) {
for (let rec of form.getRecords()){
let value = rec.getFieldValue(field_id);
if (value) {
rec.setFieldValue(field_id, value.replace(find_text, replace_with));
}
}
form.saveAllChanges();
return;
}
findAndReplace("Ford", 'Tesla');
And this version replaces an empty field with the replacement text:
var field_id = 'fld-34fb289f2b124f26982df66430b31fbd';
function findAndReplace(find_text, replace_with) {
for (let rec of form.getRecords()){
let value = rec.getFieldValue(field_id);
if (find_text === ""){ // special case with empty string
if (value == undefined){
rec.setFieldValue(field_id, replace_with);
}
} else {
if (value){
rec.setFieldValue(field_id, value.replace(find_text, replace_with));
}
}
}
form.saveAllChanges();
return;
}
findAndReplace("Ford", 'Tesla');
Enjoy!
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
I found the find and replace script posted by Brendan at https://www.tapforms.com/forums/topic/find-and-replace-script/ very helpful, but would like some help in adapting it:
var field_id = 'fld-64b0b17a635f49b8b40867e45f8d24db';
function findAndReplace(find_text, replace_with) {
var records = form.getRecords();
for (var index = 0, count = records.length; index < count; index++){
var rec = records[index];
var value = rec.getFieldValue(field_id);
if (value == find_text) {
value = replace_with;
rec.setFieldValue(field_id, value);
}
}
document.saveAllChanges();
return;
}
findAndReplace('Ford', 'Tesla');
specifically:
1. at present it searches for a specific item in a specific field. How is it possible to search for anything in that field and replace with specific text (so rather than ‘Ford’ in Brendan’s example, there might be Ford, Mercedes or BMW etc). I have tried adding a wildcard indicator (*), but it does not work.
2. A variation on 1. is where there is nothing in the specific field. Changing ‘Ford’ in Brendan’s example, to ” also does not work.
Is this possible with the script?
Any help would be gratefully received.
Hi Chris,
Thanks for the feature request.
You can probably do this now with the new Calendar and Reminders API I added to the Scripting engine. You can add an event to a calendar from a Script and store the event and reminder’s identifier in a field somewhere in Tap Forms and then at a later date, recall that identifier and modify the event or reminder.
See the updated docs for more details:
https://www.tapforms.com/help-mac/5.3/en/topic/javascript-api
The new functions are at the bottom of the page.
So you could have a script that monitors a date field in Tap Forms and adds or updates an event or reminder when that date field changes.
If you’re actually going to offer subscriptions, count me in as a first customer. I’d be more than willing to pay for it, especially since I’ve been using your software for years and only paid $50+- !
You can count on me promoting this stuff in the large computer repair communities if you ever decide to take the jump (or rather, climb Mt. Everest:-) )
You can count on me right away! … ;-)
I don’t think the Javascript API currently has the depth in the request methods to make this happen. If you can specify the authentication credentials as a part of the POST request body or as a part of the query string then it should work. If you need to use basic auth, a custom header or some sort of cookie based auth, I don’t think the current API readily supports that.
You might need to build your own bridge to handle the authentication into WordPress and then use either a query string or as a part of a POST body.
Hi all,
I have a WP site were I would like to grep some informations via api to Tapforms. I have a json path which is already connected and working with Zapier.
I’m trying to find a Javascript code WITH basic API Key authorization included. With the code in the script editor in TapForms I got an authorization error. Who can help me to include the authorization code in the script:
function fetchFromURL() {
var xxxxx = Utils.getJsonFromUrl(‘https://myjsonpath’);
return xxxxx[0];
}
fetchFromURL();
Thank you in advance!!!
Tom
This is the error:
{
code = “rest_forbidden_context”;
data = {
status = 403;
};
message = “Sorry, you are not allowed to view entries”;
}
Thanks a lot for thinking with me!
This is the result of the script, hope I did the execution right (see attachment).
30-03-20 09:52:59 / Trades / Tijdberekening
“1970-01-01T14:51:02.000Z”
Thu Jan 01 1970 15:51:02 GMT+0100 (CET)
undefined
undefined
Attachments:
You must be
logged in to view attached files.
It works well for me being in PDT. I even tried to set my computer to CEST and it reports the correct numbers.
Just curious, could you create a script like follows and report what you get in the console? I hope I got the field names right.
var tijdCET = record.getFieldValue(form.getFieldNamed('Tijd CET').getId());
var tijdUS = record.getFieldValue(form.getFieldNamed('Tijd US').getId());
console.log(JSON.stringify(tijdCET));
console.log(tijdCET);
console.log(JSON.stringify(tijdUS));
console.log(tijdUS);
Attachments:
You must be
logged in to view attached files.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
1 thing more , printing the record in ipad doesnt seem to populate the script fields (even if they are already filled by refreshing the record )
Attachments:
You must be
logged in to view attached files.
closing and opening the document didn’t fix the issue
I created the script on the Mac
refreshing the record populated the missing fields , but this way I have to do it for every record I create using the iPad
If you check out my GitHub there is a tftools GIT repo with a number of PHP scripts that should run on any recent Mac (tested on High Sierra, semi frequently on Mojave and also on Ubuntu 18.04 with PHP 7.2). There are two backup tools, the first is a simple polling operation that grabs records as they change at the polling interval and the other one of those is a backup tool that uses CouchDB’s revisioning properties to try to retrieve all changes. You can use eithre to get a copy of almost all changes (assuming sufficient polling interval) made to a Tap Forms document. I generally use the simple one with an hourly snapshot interval. Caveat is you need to enable sync to CouchDB for it to work properly but beyond that it is stable (in theory you might be able to skip the CouchDB requirement but you’d have to track down the sync port from Tap Forms each time you open a document). I personally use it to keep an extra backup copy that let’s me also look back into time to pull back stuff that I break. Downside of this is that it’s all inaccessible from Tap Forms, you’d need some sort of bridge web service to get at that data from a script inside of Tap Forms.
If you wanted the data to be accessible inside of Tap Forms, you could create a second form within Tap Forms to store the changes. You would need to store a shadow copy of the record with the old values and you would need another form for your log with just the differences. The shadow copy could be an exact replica of your base form but that would be a pain to keep in sync and would result in a duplicate form for each form tracked. That’d be a little expensive but does have the advantage that you could debug it reasonably easily because Tap Forms would render the shadow copy. That leaves you with a form that would store a serialised copy of the records in a simpler form which would have the advantage that you could store what ever you need (curiously at that point you’re recreating a database like CouchbaseLite on top of Tap Forms which is running on CouchbaseLite). You need the shadow copy because Tap Forms doesn’t maintain the earlier copy so you need to do so to get the old version of the record. From there you could use field scripts to monitor all fields, deserialise the shadow copy, compare all of the fields, update the shadow copy with the new values that have changed and then persist the change to a logging form. The logging form I could see as having fields for Form ID and Name, the Field ID and Name, a field for a serialised copy of the old value and a field for a serialised copy of the new value.
You could leverage an external set of tooling to also write back into the CouchDB the change log and maintain the shadow copy outside of the Tap Forms database. That might make sense to be able to aggregate a number of related edits together into a single logical change because without the edit/save button enabled, every edit is automatically persisted and replicated to CouchDB (this is, in general, a good thing). This would remove the need to have a field scripts inside of Tap Forms to duplicate the data but would mean you’d be reliant on that infrastructure working.
Try closing the document and opening it up again.
Where did you create the script? On the iPad or on the Mac?
Did you try refreshing the record on the iPad? If you drag down from the top of the record it should let you refresh the record and force all script and calc fields to be recalculated/executed.
I noticed that the script of a field only run in the mac app
when I create a record in ipad the script is not running (the script get the name of parent record)
I attached screenshot of record created in MAc and another in ipad app
note patient name and mrn are empty in ios
I also attached screenshot of the script
any solution ?
Attachments:
You must be
logged in to view attached files.
Thank you Sam for your contribution. I tried your scripts (Form Logger, Logger, etc.) and came up with the following idea:
Instead of historical values (by triggering new entries), the old and new values could be written to a log file (ideally on a log server), e.g.: modification date / form name / field / deviceName / old value / new value …
In the case that nothing is changed, log entry should not be made.
Do you think that would be possible?
Thanks
Chris
(I’m unfortunately an absolute beginner in scripting, so your help would be really great …)
A thing I thought about a while back for detecting the change is to create a script that watches just one field each and referred to itself to detect changes to the field. Then you can use runScriptName with some variables set already to have a common form script to handle the specific logic you need. That was only for fields in the same form, I’m not sure how well it’d work with table/child records propagating to the parent records but I think it should work for detecting changes within the same form. You could use something similar to call into the parent from the child for a link to form case, I’ve not spent as much time with table fields to know if that’d work.
An example of a script field that refers to itself to retain historic state is in my checkbox flip flop script.