Tap Forms app icon half
Tap Forms Forum text image
Blue gradient background

Exchange tips and ideas with the Tap Forms community

Search Results for 'script'

Viewing 15 results - 1,501 through 1,515 (of 2,952 total)
  • Author
    Search Results
  • #43176
    Doug Bank
    Participant

    I am building a form to document a collection. I would like to have a field that captures an ID number that will be used to physically mark the item in the collection so that I can easily identify it and look it up in the future. Brendan has told me how to use an auto-incrementing number field for this purpose, but I foresee some issues.

    I would like the field to include a prefix and possibly a custom suffix. I figure that I can have a hidden auto incrementing number, and then use a script to concatenate the prefix and suffix to the ID number and then only display the result. However, I don’t know how to create such a script, and even when I do it, I want it to show 5 digits, which the number field doesn’t like.

    For example, one possible ID NUmber could be DJB00345-F

    Does anything like this exist, or do I need to figure out how to script?
    Is there any way to ensure that the numbers will never change and never be duplicated?
    What happens if I create a new record by accident and then delete it. Will that ID number be gone and unused forever?

    Thanks,
    Doug

    #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.

    #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.
    #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.

    #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!

    #43136
    Sam Moffatt
    Participant

    I don’t think you can make the field mandatory (though Brendan will likely correct if I’m wrong there) however I’d use a script field to check to see if the field is empty. Create a new script field in your child form, jump into the script editor, keep the function template but wipe out the contents of the method, double click on the section heading matching the name of the field and it’ll put in a record.getFieldValue line then you can check if it’s undefined or not. Something like this worked for me with a test form I was using:

    function Missing_Parent() {
    	var days = record.getFieldValue('fld-429746c8ec6d433d9bb1247a5ddf31bf');
    	if (days === undefined) {
    		return 1;
    	} else {
    		return 0;
    	}
    }
    
    Missing_Parent();
    

    I set the field return on the left of the script editor to be the number type and then you can do a saved search on that field to look for one or zero. You can hide the script field once this is all working to clean up your form display.

    I can reproduce the bug with creating a new parent from the child doesn’t unlink the old parent from the child.

    #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.

    #43084
    Stephen
    Participant

    Ok, my bad…

    RTFM!

    The answer is here all along… Tap Forms Scripting 101.

    So there are two types of scripts… I had created mine in the wrong place. It is now made as a field script. So answered half of my own questions. D’oh!!

    Now, if I can just find out how to return the record colour to default appearance…
    I’ll keep looking until someone can chip in with a clue, maybe I’ll be back to solve my own issue again! lol

    Doing some serious learning today! ?

    Stephen.;)

    #43077
    Stephen
    Participant

    Hi, I’m looking to achieve something similar to this thread, using Bredan’s example above I have entered the following code, but I’m getting unpredictable results, plus my no colouring state is clearly not right!

    Outcome should be, checkbox field true, record red. Checkbox field false, record not coloured.

    So, for the time being I’ve set my no colour state to green, just so I can see a change. The script appears to give the correct result when triggering manually with the play button at the bottom of the list of scripts, but not running on change of value of the check box field.

    Any guidance appreciated!

    Stephen

    function Record_Colour_Script() {
    
    	var number = record.getFieldValue('fld-5b558c4b243d4d428355eec24dadfb66');
    	var colourHex = '#FF0000';
    	var clear = '#FFFFFF00';
    	if (number > 0) { record.setRecordColor(colourHex); record.setRecordColor(clear);
    	}	
    	form.saveAllChanges();
    	
    }
    
    	Record_Colour_Script();
    #43072
    Brendan
    Keymaster

    The AppleScript feature is still there, but it was primarily meant for firing off a Form Script in Tap Forms from an AppleScript. Although you can also query for a list of forms and such. The AppleScript dictionary will tell you what’s available.

    #43055
    Mike Kilmer
    Participant

    Thanks, man. And is the Tapforms AppleScript Library either a thing of the past, figment of someones imagination?

    #43053
    Brendan
    Keymaster

    Hi Mike,

    The scripting in Tap Forms is designed more to facilitate working with your data within the database. But there are some Utils functions which might help you with something like this by using the Utils.openUrl() function. You could build a mailto: link in code and use a script to call it.

    Thanks!

    Brendan

    #43049
    Mike Kilmer
    Participant

    Hi, Brandan and TapForms community.

    I have an AppleScript I developed a few years ago for sending emails to selected entries in Mac Contacts that I was looking to migrate to TapForms.

    This script looks like this:

    set myMessage to "Hi, here's a newsletter...
    
    Thanks and I hope you are doing magically and wonderfully.
    
    Blessings, Mike.
    https://example.com
    "
    
    set mySubject to "Engaging Subject"
    
    display dialog "Please select the recipients in Address Book/Contacts"
    
    tell application "Contacts"
        set theContacts to selection
        repeat with contact in theContacts
            set contact_name to name of contact
            if first name of contact is missing value then
                if name of contact is not missing value then
                    set contact_addressed_as to name of contact
                else
                    set contact_addressed_as to "stranger"
                end if
            else
                set contact_addressed_as to first name of contact
            end if
            my send_message(mySubject, "Hey " & contact_addressed_as & ",
    " & return & myMessage, value of first email of contact)
            delay 10
        end repeat
    end tell
    
    on send_message(theSubject, theBody, theAddress)
        tell application "Mail"
            set theNewMessage to make new outgoing message with properties {sender:"Mike iLL <mike@madhappy.com>", subject:theSubject, content:theBody & return & return, visible:true}
            tell theNewMessage
                set visibile to true
                make new to recipient at end of to recipients with properties {address:theAddress}
                delay 3
                send
            end tell
        end tell
    end send_message
    display dialog "Sent batch ending with " & contact_name & "." buttons {"OK"}

    I changed the App name to Tap Forms 5, but before I get into the current error (okay it’s Expected class name but found identifier), I had seen a reference to a “Tapforms AppleScript Library” somewhere in the forums, but can’t find that.

    Additionally, I’m more adept at JavaScript and see that there’s an API for using that. Are there resources I should know about beyond the API reference in the docs?

    Thanks and I’m excited to, after three years, finally be getting back into using the app (which is up to date).

    #43044
    Brendan
    Keymaster

    Hi Wilma,

    You can do this, but not directly. You just need to add a Script field to your form and add in the following code to the script editor:

    function photoCount() {
    	var case_cover = record.getFieldValue('fld-your-field-id-goes-here');
    	return case_cover.length;
    }
    photoCount();

    This will give you the number of photos in the Photo field. You can then create a Saved Search which looks at this and checks to see if the value is greater than 0.

    You’ll need to replace the fld-your-field-id-goes-here part with the Field ID for your own Photo field. You can get that from below the Fields list when viewing the list of fields in your form.

    Thanks!

    Brendan

    #43027
    Kris Tucknott
    Participant

    I’m having a weird issue with a couple of the forms in one of my projects.

    After ‘some period of time’ (maybe a day or two?) my records seem to lock in such a way that you can change their data, but the moment you hit the ‘Refresh Records List’ button it loses all your changes and reverts them back to their previous state.

    If I add new fields, such as scripts, it does the same. I can add a script field to fill out a text entry, hit the ‘Recaluate Scripts button’ for the record and it works – it even shows the new data on the records list. But next time I hit the ‘Refresh Records List’ button it will wipe the script output field to blank again.

    If I make a new record it works fine at first, but then after a while that record will stop accepting new changes.

    I have no ‘global scripts’ at all.

    Not sure what might be causing the issue, any ideas anyone? If what I’m describing isn’t clear I can maybe take a screenshot or two to illustrate.

Viewing 15 results - 1,501 through 1,515 (of 2,952 total)