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 - 2,371 through 2,385 (of 2,985 total)
  • Author
    Search Results
  • #36892

    In reply to: Showing web images

    Sam Moffatt
    Participant

    If you’re looking to do a simple field, something like this should work as the contents of a script field. The first few lines set up some variables and get the current values. One thing to note that I use a variable to store the photo field ID because we’re going to use that again later. There is a basic sanity check that if the photo field is empty and the URL is set then go add to the field. If you don’t have this then it’ll keep trying to add to the field (photo fields support multiple images). This should let you delete the image and then if you change the URL again, it’ll download a new image to the field.

    var photo_field_id = 'fld-1234';
    var photo_field = record.getFieldValue(photo_field_id);
    var url_field = record.getFieldValue('fld-4321');
    
    if (photo_field.length == 0 && url_field.length > 0)
    {
        record.addPhotoFromUrlToField(url_field, photo_field);
    }
    

    I’ve just written this here on the forum, I haven’t tested it so there might be a bug. You’ll have to replace the field ID’s with your own fields but it should work.

    #36891

    In reply to: Showing web images

    Brendan
    Keymaster

    Have you tried it just using the record.addPhotoFromUrlToField(url, fieldID) function and a hard-coded URL? Where fieldID will be the ID of the Photo field.

    To get the URL value from another field, just double-click on the Website address field in the Script Editor and Tap Forms will generate code to get the value.

    e.g.

    var url_field = record.getFieldValue('some-field-id');

    Don’t forget to call form.saveAllChanges(); after.

    Thanks,

    Brendan

    #36888
    Tom Dropes
    Participant

    Hi all,

    I’m trying to fetch a web image into a TapForms image field with “record.addPhotoFromUrlToField(url, fieldID);”, but I couldn’t find an example anywhere. Is there some script around for a start?

    At the end I would like to use a TapForms URL field to enter the path, and get the image which is on the web-server loaded into the image field.

    Thx a lot for your help

    Tom

    #36885
    Sin Cohen
    Participant

    Ok maybe that whole process/program thing was a bridge too far. All I really need is for the information to be organized and trackable in the way I described. I suspect learning the in-and-outs of tapforms will be project enough without having to delve into javascript. Whatever I can do to automate at least the accounting side I am hoping can be done in tapforms without any scripting but the main things would seem to be having things organized well by drive type, then model, then inventory (serials) and then assign serials to sales and remove from inventory (maybe just by transferring that record to the sales sheet/form). A lot of the math could be done with basic spreadsheet type functions but putting that together with serial number tracking and in a nice-to-work-with form(at) is what I am hoping tapforms can do. Not to mention barcode scanning/printing.
    Thanks,
    s

    #36881
    Sam Moffatt
    Participant

    Does tapforms have this sort of “process” ability? where I can start a process like “receiving” for entering new inventory and “sale” for entering sales record info and removing inventory?

    Maybe.

    For your use case with the hard drives, my guess is this is a basic process that might work:

    – you order a bunch of drives from an upstream manufacturer (could be it’s own form)
    – you receive those drives and they all have individual serials, you can load these into a drive form
    – you make a sale to a customer and create an invoice and link various drives to them

    First one you could put in Tap Forms or not, it does capture the start but also superfluous. The second one seems like your entry point for Tap Forms, you create new drive records when you receive and catalog them. Then the next step is an invoice table linked to your drive. Now Tap Forms will let you link all of this together with two or three forms no worries, what it won’t inherently enforce are your business rules.

    You could set up an invoice form with a link to the drive form so that you can select candidate drives. This will work but runs the risk that Tap Forms will display everything in that form, including ones you’ve already sold. A potential solution is in fact two drive forms, one with “available” drives and another with “sold” drives where you move records from one form to the other. That’s possible to do but would interact awkwardly with the Tap Forms UI because the same data will be split across two forms.

    If you’re fine eschewing the UI a little and happy to spend some quality time with Javascript, then you can use form scripts to automate some of the process. You could set up a saved search for “unsold” drives (e.g. ones not linked to a sale/invoice) and then implement a pre-check to make sure that the drive you just scanned isn’t already sold before linking it to a new invoice. Of course if you link it to an invoice that gets returned or similar then you’d have to unlink the drive from the invoice.

    Whilst Tap Forms has a barcode scanner feature built in on iOS, a Bluetooth barcode scanner for your desktop will likely help you speed through this as well. It could also help you on the phone but I’d personally suggest automating via the desktop this functionality. If you can find a programmable Bluetooth barcode scanner that will let you append “Command+N” to the end then you could do really quick data input.

    Without some work, Tap Forms can do some of what you want in terms of the data model but that wouldn’t enable the full process management. With some script fields and form scripts, you could automate most of the process to work for what you need with the checks you want. I’m not going to say yes because it’s going to require some scripting work to fully realise but it’s definitely not no: a solid maybe.

    #36834
    Sam Moffatt
    Participant

    Thanks @daniel_leu! It’s picking up on what you did and extending it! I only learned about the string interpolation/string template feature recently when I picked Javascript up again with Tap Forms. I was looking for a better way of building larger templated strings because I figured there had to be something by now in Javascript and turns out there was! ES6 brought the language a long way.

    #36831
    Daniel Leu
    Participant

    I really should learn Javascript… Thank you, Sam, for showing and explaining how to make the code more elegant and robust!

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #36830
    Sam Moffatt
    Participant

    You might want to use a form script though depending on what you are doing. The way I think of things is:

    • script fields update when the fields they refer to update and change their value based on that. They are useful for setting their own static value or even setting other fields. I use script fields to do stuff like take a notes field, parse it and then fill in other fields in a form. Script fields for me are the sort of thing that you should be able to run multiple times and they should have roughly the same output. Script fields are a little magical which is good and bad.*
    • form scripts I use for instances where I need a manual interaction OR the side effect of the script is in a sense variable. I use these for doing quick data entry, creating new records based on existing data or to automate mistake fixing.* The other use case is for building function libraries which allows re-usable code.

    In your case I think I’d actually model this as form scripts, create a form for logging and then when you generate your emails you put them in the log. In the log form you have the weblink to create the email and use that to send the email based on the log. That way you have a record of what you generated and when you sent it from within Tap Forms and you don’t have to go to your email to see what you sent.

    Then in terms of automating this I’d look to use form scripts with the prompter to ask for input. You could actually build a full state machine in the scripting engine and then use the log form mentioned above to be your audit trail as you go through everything. You could do some of this with script fields that watch and update a state field somewhere (and a previous state field as well) but in this case it sounds like you a form script with explicit intent is better than implicit state management.

    * When I say script fields are magical and I use form scripts for mistake fixing…I have a set of script fields that automatically propagate changes for like shipping dates into or from other records. Doing that means that I can’t simply change those fields because it detects those changes, runs it’s validation and then tries to undo the changes I just made because it makes the state inconsistent. To work around this, I have a few form scripts that reset these fields in a way that disables the script field updates so that I can change everything without my own automation undoing the changes.

    #36829
    john cesta
    Participant

    It looks like I could make a script as a field type and the result would be the email body. What’s ever in the fields within the script would be available for me to simply copy into an email.

    I think that’s possible.

    #36828
    john cesta
    Participant

    Thanks Sam. I’ve started with just getting and setting one field. What I have is a form called written biz and want to send the client an email for each phase of the policy. In the email I put the policy number, their name, and a few other fields in the record. So what’s ever In the record will go when I send the email.

    It works the same for every one and every policy. So while the calculation may be too weak
    Th script would certainly do it.

    I understand the complexity of a partial feature set. I didn’t think of the iPhone aspect though.

    John

    #36825
    Sam Moffatt
    Participant

    The challenge with porting some of that stuff over to iOS is whilst it makes sense for iPad it isn’t as useful on iPhone. That means it’s something useful for only half of the iOS market now that iOS apps are on both form factors. Perhaps an extra unlock for that as a feature so that those who only use an iPhone don’t have to pay for a feature they can’t use. That then leads to complications in the UI around enabling and disabling functionality based on the purchases which has their own challenges (and the inevitable restore purchases button). There’s also the other challenge that there is only one Brendan.

    I think you can use a calculation field to create the text you want and if you’re not doing a whole bunch of them at once, you can use the process Daniel Leu suggested as well. If you look at the bottom of the Calculations documentation, there is an example of formatting an address using a calculation field. You could use that as the basis of building out a fully formatted body. Alternatively you could use a script to build the template as well for creating the email link. Using a script, you’d put your template inside of the script and you can use variables inside of it.

    You can use a script to set multiple field values, setFieldValue or setFieldValues from the JS API can help you out there. Once you set a value, you also need to save those changes with document.saveAllChanges() otherwise they might not persist.

    On my iPad I did a quick test to build a form with a standard address layout and then made a script field to populate a “Web Site” field for me. The script looks like this:

    var name = record.getFieldValue('fld-f3d753238cf4457cb2ccd5c0eaa96b0f');
    var email = record.getFieldValue('fld-b1b7229002144634ae174c861b78ae72');
    var address_1 = record.getFieldValue('fld-ba8218dbc15741428af68f3e80c29fea');
    var address_2 = record.getFieldValue('fld-d11947b80b7a4592aa1815588275ef8e');
    var city = record.getFieldValue('fld-c22af0fe9d1a4fd3a587d96a83b352e5');
    var postcode = record.getFieldValue('fld-10d06c4cd8564b5a87ed365c5a307740');
    var state = record.getFieldValue('fld-35f71f806348488e8f755be48b8ce2f2');
    var country = record.getFieldValue('fld-0b6fe598592c428bab1010755d4bfa89');
    var text = record.getFieldValue('fld-8b5d1c927eca4daa89c7750848bd02c2');
    
    var addressLines = [];
    name ? addressLines.push(name) : null;
    address_1 ? addressLines.push(address_1) : null;
    address_2 ? addressLines.push(address_2) : null;
    addressLines.push(`${city} ${state} ${postcode}`);
    country ? addressLines.push(country) : null;
    var address = addressLines.join('\n');
    
    var subject = encodeURIComponent(`Hello ${name}! We missed you!`);
    var body = encodeURIComponent(`Hello ${name},
    
    We haven't seen you for a while and we wanted to 
    reach out to you. We don't  have a phone number 
    on file for you but we do have your address:
    ${address}
    
    If this isn't correct, please let us know so we 
    can keep in touch. 
    
    Thanks in advance!
    
    Your Friends`);
    
    var linkText = `mailto:${email}?subject=${subject}&body=${body}`;
    var send_email_template_id = 'fld-f03ed31415304eab814d9f137db2f598';
    record.setFieldValue(send_email_template_id, linkText);
    document.saveAllChanges();
    
    • The first chunk of the script gets all of the fields I care about via getFieldValue, that’s relatively straight forward.
    • The next section does a simple address composition as an example of how empty/unset fields could be handled (it’s not perfect, I took a shortcut on the City/State/Postcode line). It uses the Javascript ternary operators to determine if it should add to an array and then does a join on what was added to the array.
    • The next piece uses the Javascript backtick string interpolation feature to template the values out. It’s similar to the bracket format above except it uses a dollar sign and curly braces to wrap the variable. These values map to variables defined within the script itself. The first line is easy, it’s just the subject line. The second one shows how you can turn that into a multiline string to splice in values. Of note here I use encodeURIComponent to escape the text so that if you put in a special character it doesn’t munge the email.
    • The last few lines compose the link text that makes the email. This is essentially the process Daniel Leu suggested though not encapsulated as well as he has done.

    As you change the referenced fields, the web site field will update with the new values and then you can just tap on the globe icon and Tap Forms (after it asks “are you sure?”) will spin up Mail on the iPad prefilled with your email.

    Edit: Forgot that the backticks are special on Brendan’s forum too. I used an online HTML entity encoder/decoder to fix it up.

    Edit: I wrapped some of the lines in the message to better fit in the forum. Where you put line breaks in the script is where they will show up in the email. I’d generally want to manually wrap lines when doing plain text emails at less than 70 columns but that’s just me.

    #36823
    john cesta
    Participant

    I looked Daniel and responded thanks.

    Brendan is determined to make me buy a Mac. I just bought my daughter a Mac and myself an iPad Pro. So I have to wait.

    If it wasn’t too much trouble for tapforms I think some would be willing to pay more (I would) for an iPad versus in that contained even some of the Mac features like calendar and or the pdf fields feature.

    I know it could be a lot of work porting it but what do I know?

    Is there a way, perhaps with a script, to set values on multiple fields at once. After I mail to a few hundred people I’d like to set a field with the name of the mailing and date.

    I use a child form to store the mailing content now.

    #36820
    Sam Moffatt
    Participant

    The result should be stored in the script field and it won’t change the original field. The reason for this is if you change the original field via the script, it’ll break the UI selection because it will trigger as you select the options.

    My intent was that if you were using a layout to just use the script field in place of the editable field. You could flag the script field as hidden to remove the script field from the default layout but then use the script field on the layouts. This also means the UI will still work properly with the original field.

    ===

    The other thing to do would be to take the script and turn it into a form script that instead of returning the value at the end, sets the value of the field. If you bind it to a keyboard shortcut, you can then press that once you’re done with your edits to clean the field.

    To make it into a form script, change this line:

    return cleaned.join(' ');

    To:

    record.getFieldValue('fld-6d04efdd9e334ad29399bc3c4aef4f06', cleaned.join(' '));

    That’ll reset the field to the new value for you. You will also need to put document.saveAllChanges() somewhere in your script to make the change persist.

    If you have many of these fields that you want to mutate this way and you went for a form script, you could do an array of the field ID’s.

    First step of that is to change the Restructured method to accept a parameter:

    function Restructured() {
    	// Replace this with the field that stores your pick list.
    	var sentence = record.getFieldValue('fld-6d04efdd9e334ad29399bc3c4aef4f06');
    

    Is rewritten into

    function Restructured(field_id) {
    	// Replace this with the field that stores your pick list.
    	var sentence = record.getFieldValue(field_id);
    

    Update the setFieldValue at the bottom to use field_id as well.

    Then when you call into Restructured, you can just set the field ID’s into an array:

    Change:

    Restructured();

    To:

    ['fld-1', 'fld-2'].map(Restructured);
    document.saveAllChanges();

    Replacing in the list of fields you need to clean up. The beauty of this particular approach is that once the 1: placeholders are gone, assuming you don’t use colons elsewhere the field is stable so you can run it multiple times and it won’t break fields already updated. Though as I’m sure you’re aware, as soon as you rewrite that field the UI selection options will break.

    Full script:

    // This cleans out the Tap Forms comma and space delimiter.
    function cleanString(input)
    {
    	// I had an input length check because the first array
    	// element is going to actually be empty but then I put
    	// in a filter and this if statement ended up being redundant.
    	if (input.length)
    	{
    		// What we're looking for is a space OR comma multiple
    		// times at the end of the string (the dollar sign means
    		// end of string) and we want to replace that with an
    		// empty string.
    		return input.replace(/[\s,]*$/, "");
    	}
    }
    
    // Boilerplate :D
    function Restructured(field_id) {
    	// Replace this with the field that stores your pick list.
    	var sentence = record.getFieldValue(field_id);
    	
    	// Step 1: take the sentence and split it based on "Number: ", e.g. "1: ".
    	var cleaned = sentence.split(/[0-9]*: /)
    	// Step 2: Filter removes elements from an array that don't match.
    	//         In this case, if the string length is greater than zero, keep it.
    	//         The 'x' here is short hand for the array element and this could
    	//         have also been implemented as another function.
    		.filter(x => x.length > 0)
    	// Step 3: Map executes a function per element and creates a new array with
    	//         the new values. What we're doing is using "cleanString" to clean
    	//         the values and remove the Tap Forms delimeters. This could also
    	//         have been done inline too: .map(x => x.replace(/[\s,]*$/, ""))
    		.map(cleanString);
    
    	// Last but not least we return the value to our caller. In this case
    	// we're joining the array with a single space however you could also do
    	// this with a new line character (e.g. '\n') or any other string that
    	// makes sense for your use case.
    	record.setFieldValue(field_id, cleaned.join(' '));
    }
    
    ['fld-6d04efdd9e334ad29399bc3c4aef4f06'].map(Restructured);
    document.saveAllChanges();
    #36818
    Daniel Leu
    Participant

    I’m using TapForms as a simple CRM tool and once in a while, I need to send emails using content from a record. Using a field with the type ’email’ opens the mailtool, but doesn’t provide a way to add a subject or message content.

    But I found that I can trick Tapforms into launching the email tool using a “mailto:” web link! And with “mailto:” I can set the email subject and body as well.

    So in my form, I have a field called “Send Email” of the type ‘Web Site’. Using a script, I set this field with a mailto link and additional content derived from my customer record. The mailto link then looks like “mailto:email@company.com?subject=…@body=…”.

    Then I have a script field called ‘Create Email’ which contains following code:

    
    // Create Email URL String
    function createEmailURL( recipient, subject, message) {
    
    	var subject_field = subject ? "subject=" + subject : "set subject";
    	var message_field = message ? "&body=" + message: "";
    
    	var url = "mailto:" + recipient + "?" + subject_field + message_field;
    	
    	return url;
    }
    
    function Script() {
    	var send_email_id = 'fld-c3fbfda318c5412fb5be8ecf2e0e7e3a';
    	var name = record.getFieldValue('fld-00336938a7d340a58d47a7231fe497dd');
    	var email = record.getFieldValue('fld-09e0ebeccf814d4b920e4021cf824997');
    
    	var message = "Dear " + name;
    	message += "\n\nThis is my message for you!";
    	message += "\n\nRegards - Daniel";
    
    	var url = createEmailURL(email, "Member update", message);
    
    	record.setFieldValue(send_email_id, url);
    	document.saveAllChanges();
    	
    	return url;
    }
    
    Script();
    

    Whenever I update a field used in my script, the Send Email field is updated. Perfect! Unfortunately, I have to do a cmd-R (Refresh Selected Record) to have the GUI reflect the update as well. It would be so good to have a Record.Refresh() function to do this from a script (Brendan, are you listening? :)!

    Once done, clicking on the Web Site link, the mailtool is opened with the prepared email.

    This is not efficient if one needs to send hundreds of emails, but it is perfect for one or just a few.

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #36817
    Chris Ju
    Participant

    Thank you very much, Sam!

    The script works! But the result is not stored in the field! What am I doing wrong?

    It would be great, if you could give me an advice!

    Thanks!
    Chris

Viewing 15 results - 2,371 through 2,385 (of 2,985 total)