Record creator, by device, or username.

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Record creator, by device, or username.

Viewing 5 reply threads
  • Author
    Posts
  • January 9, 2021 at 10:15 AM #43134

    Stephen
    Participant

    Hi,

    I’m a real newbie at related databases period, but with help I’ve had some success & I’m understanding more, but I have another issue that after much time searching these forums, reading the 101 & 102 etc. and many attempts is still not coming right, so a steer would be really appreciated.

    I’ve created a new form for notes as a child of the main form using a 1:many.
    I’d like to track who created the note record & who last made a modification to it.

    The later is solved easily with the Modified by device field type, but I’m struggling to set the original creator with a fixed value. My original thinking was a calc or script that tested if a text field for “Created by:” was empty and referencing the Modified by device value if empty & ‘undefined’? if not?

    It doesn’t seem possible to reference that field type within a calculation or script, at least not in a way that appears obvious to me?

    Do I need to think about this in a different way?

    Thanks, Stephen.

    January 9, 2021 at 12:18 PM #43137

    Sam Moffatt
    Participant

    I don’t see the modified by field in the script editor link, however you can grab the field ID from just under the description field in the field editor panel. You can then use that with record.getFieldValue and that seemed to work properly for me to retrieve the value that I expected in the quick test I did. There might be a way of convincing a calculation field to get at it, but for myself I use a script field for this particular purpose.

    You can also cheat with a script field and use it to set the value automatically:

    var self = record.getFieldValue('fld-991082c160224a12a87151cf87aaec93');
    var modified = record.getFieldValue('fld-b7b336c4acc7406786eecc8a7b9f75b9');
    
    self ? self : modified;
    

    Where self is the ID of the script field you just created and modified is the ID of the modified by field you created. Essentially it says if self is set, use the value of self otherwise use the value of modified. Tap Forms takes the return value of this to update the value of the script field so that the next time it’s run, it uses the value of the script field. This makes the field read only in a way that can’t be edited without having to use another script field or form script to change it. An example form script would look like this:

    function Reset_Created_By() {
    	record.setFieldValue('fld-991082c160224a12a87151cf87aaec93', undefined);
    	document.saveAllChanges();
    }
    
    Reset_Created_By();
    

    Which resets the field value to an empty value which then triggers the form script to reset the value to the current value of the last modified device. We need the document.saveAllChanges() here to tell Tap Forms to save the change made by setFieldValue. We didn’t need it on the script field because Tap Forms is implicitly saving the result of the script field.

    Hopefully this helps!

    January 9, 2021 at 1:06 PM #43138

    Stephen
    Participant

    Hi Sam,

    thanks for the reply.

    Everything you said makes sense, but it’s not working in practice in my world :(

    User_Script();
    
    function User_Script() {
    
    	var self = record.getFieldValue('fld-36b6432d2ec640c29f48daf7c45b74ae');
    	var modified = record.getFieldValue('fld-ac2a6fe2490e464bb4ff32ef723e6a8b');
    
    	self ? self : modified;
    console.log (self);
    console.log (modified);
    
    }
    
    User_Script();

    I don’t understand the syntax of the “self ? self : modified;” line.
    I understand your description of how it should work, but have I missed something, as I get no value in the field. I added the console log lines & the console shows the expected values.(Undefined & my MacBook name).

    Thanks, Stephen.

    January 9, 2021 at 8:15 PM #43142

    Sam Moffatt
    Participant

    The script I wrote doesn’t use the default template with the function, it’s just that text in the script editor. I’ve attached a screenshot of what it looks like to make it clear. When you’re dealing with something simple it’s sometimes easier to just directly put the code into the editor without the extra wrapping. In this case where we’re essentially doing a single line calculation, we can simplify a little :)

    To get your code to work put a return in front of the self ? self : modified; line. Taking what you’ve got though, something like this should make it work:

    function User_Script() {
    
    	var self = record.getFieldValue('fld-36b6432d2ec640c29f48daf7c45b74ae');
    	var modified = record.getFieldValue('fld-ac2a6fe2490e464bb4ff32ef723e6a8b');
    
    	console.log (self);
    	console.log (modified);
    
    	return self ? self : modified;
    }
    
    User_Script();

    Minor reordering to put the console.log line ahead of the return and then an explicit return from the result of the ternary operator. The operator is a short hand for doing the following instead:

    if (self) {
       return self;
    } else {
       return modified;
    }

    What it’s doing is essentially simplifying that down to a single line instead of the five lines (and the word return written a couple of times).

    So no boilerplate?

    Fundamentally you don’t need the function wrapper, it’s there because without it you can’t use the return statement. When we don’t use the function boilerplate that Tap Forms provides and write directly in the script (as depicted in the screenshot), the return statement doesn’t work because there is no function to return from. This is another reason to use the ternary operator as it helps out making things a little compact. The alternative looks a little weird to most Javascripters:

    if (self) {
        self;
    } else {
        modified;
    }

    This works but looks a little awkward because well you’d expect a return statement however because this is capturing the last value from the script directly you don’t need the return. In fact the runtime will give you an error if you put a return statement in saying “Return statements are only valid inside functions.”

    Attachments:
    You must be logged in to view attached files.
    January 9, 2021 at 8:26 PM #43146

    Stephen
    Participant

    Hi Sam,

    thanks for taking the time to explain. I actually have something working, I chipped & chipped away looking through the forums and other Java sites and managed to stumble across this… (which looks very much like the long hand you were guiding me to ignore! lol

    function Created_By() {
    
    var self = record.getFieldValue('fld-b55aeb5016cb4a9ea62fc8aae6cc36e7');
    var modified = record.getFieldValue('fld-ac2a6fe2490e464bb4ff32ef723e6a8b');
    
    if (self === undefined) {
    		return modified;
    		} else {
    		return self;	
    		}
    
    }
    
    Created_By();

    So, I understand how I can invoke your original solution a bit more now & tidy up my code!

    Thanks again for taking the time, I clearly need a copy of Java for dummies!! ;))

    Regards, Stephen.

    January 9, 2021 at 9:03 PM #43147

    Sam Moffatt
    Participant

    Glad to hear you got it to work, if it solves the problem then there’s no issues using it.

    Ternary operators is something I’m personally quite fond of and in a language like Javascript where it’s more flexible with typing it also covers a variety of situations. Since Javascript will convert anything into a boolean based on if it’s truthy or falsy, it means a bunch of values that we aren’t interested in (empty string for example) are covered by the statement too.

    One note, don’t confuse “Java” with “Javascript” because whilst they seem like they should be related, it was really a marketing ploy. There are plenty of web focused Javascript resources (Mozilla Developer Network, W3Schools) and a few resources leveraging node.js (also W3Schools as an example), there aren’t that many resources that focus on pure Javascript that I’ve found. A lot of the resources include platform specific stuff that really only relates to the use in web browsers or to node.js rather than the ECMAScript standard.

Viewing 5 reply threads

You must be logged in to reply to this topic.