Search Results for 'script'
-
Search Results
-
Topic: For Help
hello
I wish everyone a happy day
How to use a tool larger than or smaller than with a replacement
Example
if X>Y print “No balance”
Can I do it through the calculator
Or should be placed in a scriptIf you put it in a script, add it in the form
In Linking Images in a Database an ask was made to pick images and whilst that’d be cool to see somehow with form links, I decided to implement this as a quick field script as an example though a Link from Form field with the ability to embed a custom layout I think would be a cool way to solve the problem..
This script borrows some publicly available images to solve the problem and uses a picklist to limit choices. I noticed I needed to hit refresh because the field script wasn’t being triggered. It will also not do anything if an image is already there, you have to manually delete the image to change it. Very simple sample form attached as well.
var disc_type = record.getFieldValue('fld-d829341555114d4fb0a1f6d05a80930a'); var disc_type_logo = record.getFieldValue('fld-9cd36df80c1141d4a87f59496222ebb0'); console.log(disc_type); console.log(JSON.stringify(disc_type_logo)); if (!disc_type_logo.length) { var url = ''; switch (disc_type) { case 'CD': url = 'http://pluspng.com/img-png/file-cd-logo-png-1024.png'; break; case 'DVD': url = 'http://pluspng.com/img-png/dvd-movie-png-dvd-logo-600.png'; break; case 'Bluray': url = 'http://pluspng.com/img-png/ama-flat-track-logo-vector-png-blu-ray-disc-logo-400.png'; break; case 'LP': url = 'http://pluspng.com/img-png/turntable-hd-png-turntables-png-cliparts-2640178-1000.png'; break; } if (url.length) { record.addPhotoFromUrlToField(url, 'fld-9cd36df80c1141d4a87f59496222ebb0'); } } else { console.log('Skipping setting logo'); } console.log('All done');Topic: VLOOKUP Script
Originally noted in the general forum, this is a method equivalent to Excel’s VLOOKUP. It’s actually mostly templated from the snippets, the
child records loopsnippet looks mostly like thevlookupfunction and the prompter snippet was in the other form script POC.This will work, it requires using script field instead of calc field but it’s not that bad. You need to tell it the value you want to lookup with, the “join” field (table or link to form), the search field to match the lookup value on and then the field to return.
Create a new form script called “vlookup” with the following contents:
function vlookup(lookup, join_field, search_field, return_field) { var entries = record.getFieldValue(join_field); for (var index = 0, count = entries.length; index < count; index++){ var target = entries[index].getFieldValue(search_field); if (target && target == lookup) { return entries[index].getFieldValue(return_field); } } return ""; }Then create a script field to map the values across:
form.runScriptNamed('vlookup'); var addresses_id = 'fld-34e22de8a7cf438fb4a83146108f0511'; var address_name_id = 'fld-f05929829d674141aaed98efe11e29f1'; var street_id = 'fld-04ec2a23e3554770b3e1f1d771157dd6'; var primary_address = record.getFieldValue('fld-9b2865aa57b74b70bd4421b27081d65b'); vlookup(primary_address, addresses_id, address_name_id, street_id);In the script editor, select the fields from the linked form and use the “ID” button instead of double clicking them to get the var syntax. You’ll want to change the last field to match your fields across.
I’ve attached a sample archive which should demonstrate what I’m talking about. It also has another form script using a prompter to handle the address change but for some reason the script fields aren’t updating afterwards, you have to manually press refresh on the record.
Topic: addToTableIfMissing script
This is another one of my convenience functions that adds rows to a table if a given key/value pair are missing. This does mean that you can have duplicate ‘keys’ with different values and this is intentional. The use case is for picking up attributes from external sites like eBay or AliExpress that have this sort of attribute or variant data available to hand. This is another one in my ‘Script Manager’ form which I’m going to attach for reference as well. It includes the four modules I’ve posted for the logger, the currency conversion, setIfEmpty and this addToTableIfMissing function.
// ========== addToTableIfMissing Start ========== // // NAME: addToTableIfMissing // VERSION: 1.0.1 // CHANGELOG: // 1.0.1: Add support for specifying a custom record to use. document.getFormNamed('Script Manager').runScriptNamed('Logger'); /** * Add's a key/value pair to a table if and only if it's missing. * * If you have a key that exists already but with a different value, * then this will add another table row for * * fieldId: The field ID of the table field. * keyField: The field ID of the key field in the table field. * key: The value of the key field to check against. * valueField: The field ID of the value field in the table field. * value: The value of the value field to check against. * * return: Empty return value in all cases. */ function addToTableIfMissing(fieldId, keyField, key, valueField, value, currentRecord) { if (!currentRecord) { currentRecord = record; } logger.logMessage(<code>Adding to ${fieldId} with ${key} and ${value}</code>); var table = currentRecord.getFieldValue(fieldId); for (var index = 0, count = table.length; index < count; index++) { var targetKey = table[index].getFieldValue(keyField); var targetValue = table[index].getFieldValue(valueField); if (targetKey == key && targetValue == value) { logger.logMessage(<code>Found existing value for ${fieldId} with ${key} and ${value}</code>); return; } } var newRecord = currentRecord.addNewRecordToField(fieldId); newRecord.setFieldValue(keyField, key); newRecord.setFieldValue(valueField, value); return; } // ========== addToTableIfMissing End ========== //Here’s a sample of one of the note parsers that I use to take key/value summary data and turn it into something mildly useful. Most of the heavy lifting is done by a PHP script that I run locally to process and extract the data, this script is just handling the response data:
document.getFormNamed('Script Manager').runScriptNamed('Logger'); document.getFormNamed('Script Manager').runScriptNamed('setIfEmpty'); document.getFormNamed('Script Manager').runScriptNamed('addToTableIfMissing'); function noteParser() { var note_id = 'fld-bf19d52c18cb4f5198df191ef7902e1b'; var notes = record.getFieldValue(note_id); if (!notes) { return "Notes field empty"; } var result = Utils.postContentToUrlWithContentType(notes, "http://localhost/~pasamio/research/scrape/extract_note.php", "text/plain"); if (!result) { return "no result returned from web service"; } if ("title" in result.fields) { setIfEmpty(record, 'fld-0d0edd2552ea461e929f806a4e5552b5', result.fields.title, null); } if ("price" in result.fields) { setIfEmpty(record, 'fld-08129d71ab0f4fa4a2749456281fca07', result.fields.price, null); } if ("brand" in result.fields) { setIfEmpty(record, 'fld-1e250019d7b249f282cc572814d3e71d', result.fields.brand, null); } if ("itemkey" in result.fields) { setIfEmpty(record, 'fld-ae7379d699e9473aa2ab16a2a2f002d4', result.fields.itemkey, null); } if ("colour" in result.fields) { setIfEmpty(record, 'fld-a8626656cc90455ea9336dd2488d4aef', result.fields.colour, null); } if ("category" in result.fields) { setIfEmpty(record, 'fld-6fdd09891a8c4d73be1b24aa07d077be', result.fields.category, null); } var variant_data_id = 'fld-eb212e705eb34e9ea5cc4386ea7a9b1f'; var key_id = 'fld-ecc1b1ede8414912a63ec144012fa9e9'; var value_id = 'fld-e4ce093c1c22416192eb80554272d6cd'; if (result.data) { for(datum in result.data) { addToTableIfMissing(variant_data_id, key_id, datum, value_id, result.data[datum]) } } document.saveAllChanges(); return result; } logger.consoleHeader('Note Parser', 'Purchases'); var result = noteParser(); logger.consoleFooter('Note Parser', 'Purchases'); JSON.stringify(result);I quite often use
JSON.stringifyto take an object and turn it into JSON that is stored in the field. This helps with debugging and seeing the contents of the execution after it has run. This used to be more necessary before the console was added but is still useful to have a durable copy of the last execution.Topic: setIfEmpty Script
This is a script that will only set a field value if it is currently set to an empty value or is set to a specified default value. This is useful for blindly setting a value on a field without having to do the check work yourself.
If you use my ‘Script Manager’ form, I call this one ‘setIfEmpty’ and you load it as:
document.getFormNamed('Script Manager').runScriptNamed('setIfEmpty');// ========== setIfEmpty Start ========== // // NAME: setIfEmpty // VERSION: 1.0 /** * Set a field if it is currently empty or matches the default value. * * target: The record to use (TFFormEntry object) * fieldId: The field ID (e.g. <code>fld-hash</code>) to set. * value: The value to set in the field. * defaultValue: The default value of the field. * * return: boolean true if set or boolean false if unset. */ function setIfEmpty(target, fieldId, value, defaultValue) { var current = target.getFieldValue(fieldId); if ((!current || current == defaultValue) && current != value) { console.log('setIfEmpty passed for ' + fieldId + ', setting to: ' + value); target.setFieldValue(fieldId, value); return true; } else { console.log('setIfEmpty failed for ' + fieldId + ', skipping.'); return false; } } // ========== setIfEmpty End ========== //Simple example of how to use it:
setIfEmpty(record, 'fld-39ca9564ef2347ac93f933bc9a2316ac', result.fields.title, null); setIfEmpty(record, 'fld-39379cdff743496f9a1ccbdc1ae56297', result.fields.quantity, 1);The first has a default of null (TapForms’ default default value) and the second has a default of 1 which obviously is a configured value for a number field.

