Blog  |  Support  |  Forums

Search Results for 'form.getRecords'

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Search Search Results for 'form.getRecords'

Viewing 15 results - 106 through 120 (of 146 total)
  • Author
    Search Results
  • #41842
    T.L. Ford
    Participant

    It can be done like this (for smaller datasets):
    Copy “target” records into their own table and link to that.
    A simple “rebuild records” script can keep the copied-form data in sync with the real form data.
    Hide things you don’t want to see.

    See attached Tap Forms database. The copy script (at the end of this post) uses a search to limit exercise_muscles to target.

    Random trivia: NoSQL tables don’t need IDs (if you are using traditional numbers for ids (tough to accept going from SQL-brain to NoSQL-brain). Links between objects are maintained for you.

    Question for experienced Tap Form scripters:
    What’s the correct way to copy links? This works, but feels cumbersome and incorrect.

    
    // only going to be one match, if any
    var possible_records = records[index].getLinkedRecordsForField(em_exercise_id);
    if (possible_records) {
    	var possible_record = records[index].getFieldValue(em_exercise_id);
    	if (possible_record) {
    		var possible_rec_id = possible_record.getId();
    		var possible_record = frm_exercise.getRecordWithId(possible_rec_id);
    		rec.addRecordToField(possible_record, exercise_id);
    	}
    }
    

    Notes for Brendan,
    TFFormEntry object needs documentation.
    Typo at:
    record.recordExistsInField(field_id’); // example is missing the record object parameter, and ‘
    record.getNoteFieldValue(field_id’); // I think I reported this one already.

    Chance of support for directly copying linked records? i.e.
    rec.setFieldValue(muscles_id, records[index].getLinkedRecordsForField(em_muscles_id));

    ——-
    Copy Code:

    function rebuild_data() {
    
    	// delete old records
    	var records = form.getRecords();
    	for (var index = 0, count = records.length; index < count; index++){
    		form.deleteRecord(records[index]);
    	}
    	document.saveAllChanges();
    
    	// this form's ids
    	var role_id = 'fld-e9c606f133124d6891c9a04645221833';
    	var exercise_id = 'fld-22527485052d4c0f98aa91bff884f75a';
    	var muscles_id = 'fld-5d856f1c8bfb4010843755a98f556f5a';
    
    	// exercise_muscle's ids
    	var em_role_id = 'fld-c2d53eebfed9446dbc57a11919c8b85d';
    	var em_exercise_id = 'fld-99daf5cc157f4f5d9b51ed3496f43e28';
    	var em_muscles_id = 'fld-a982502aca7649dcb1c9e47f812762d7';
    
    	var frm = document.getFormNamed('exercise_muscle');
    	var src = frm.getSearchNamed('target');
    	records = src.getRecords();
    
    	var frm_exercise = document.getFormNamed('exercise');
    	var frm_muscles = document.getFormNamed('muscles');
    
    	for (index = 0, count = records.length; index < count; index++){
    		var rec = form.addNewRecord();
    		rec.setFieldValue(role_id, records[index].getFieldValue(em_role_id));
    
    		// only going to be one match, if any
    		var possible_records = records[index].getLinkedRecordsForField(em_exercise_id);
    		if (possible_records) {
    			var possible_record = records[index].getFieldValue(em_exercise_id);
    			if (possible_record) {
    				var possible_rec_id = possible_record.getId();
    				var possible_record = frm_exercise.getRecordWithId(possible_rec_id);
    				rec.addRecordToField(possible_record, exercise_id);
    			}
    		}
    
    		var possible_records = records[index].getLinkedRecordsForField(em_muscles_id);
    		if (possible_records) {
    			var possible_record = records[index].getFieldValue(em_muscles_id);
    			if (possible_record) {
    				var possible_rec_id = possible_record.getId();
    				var possible_record = frm_muscles.getRecordWithId(possible_rec_id);
    				rec.addRecordToField(possible_record, muscles_id);
    			}
    		}
    
    	}
    	document.saveAllChanges();
    
    }
    
    rebuild_data();
    #41536
    Sam Moffatt
    Participant

    With the link you can absolutely fill it in from the Magazines form, that makes complete sense because when you add from the Magazines form, it autolinks the Magazine to the article for you similar to how the Table field operates.

    The Articles form is just a more accessible version of the table field you created, I think under the hood Brendan modelled them the same way, it’s just that you don’t see the form as the field itself is the form. You could hide the Articles form if you wanted so you’d not see it in the forms list but would still be able to interact with it. I personally like having them all available but the option is there.

    We can automate a lot of things using the calculation fields to pull data from the parent record. Tap Forms can’t give you a table view that has fields from either two different forms or fields from the parent form and rows in a table field. That’s in part where that Articles form comes in because we can reference that from other forms (Keywords in this case) and since it is linked to the Magazine form, we can use calculation fields in the Articles form to automatically replicate values from the parent Magazine form. The other advantage is that Tap Forms’ Link to Form JOIN field works on the form making it a little easier.

    If you only want to link to the magazine plus magazine month and don’t need the article name or page details (essentially anything in your articles table or the articles form) in your keyword form, then you could change the Articles form (and link) back into just a simple table. I might have misunderstood along the way but I thought it was useful to see the article and page details for the keyword as well. That’s what a lot of this is predicated on is pulling all of those fields together into the keyword to make a table.

    Looking at your second screenshot, if the Magazines link isn’t useful then you can safely remove it and for the articles link in your second screenshot if you click on the X button on the far right you can hide fields and reorder them (this also works in the multicolumn list view as well). Getting magazine month and number should be a calculation field that populates the value from the parent magazine form, this is just a matter of setting the fields up and making sure they have the right type (date for the month field and number for the number field [number should be the default]). If the calculation field is misbehaving, jump to the Articles form and click on the refresh icon on the bottom of the list view and it should recalculate the fields to update to the right value.

    The challenge with automatically creating a keyword if one doesn’t exist is that we need to scan the keywords form to find what keywords exist and do an insert if one doesn’t exist. If I was using something like MySQL, it has functionality that is optimised to make this mostly cheap through it’s indexing system but Tap Forms isn’t built with a columnar index structure but is a document store. That means we need to build the index ourselves. Doing this each time you edit the keywords field is probably a little intensive and would slow Tap Forms down as your database scaled in keywords. I’m also going to make an assumption that you’re going to hit a threshold where adding a new keyword is a rare event rather than a common one, as it will be today. Given both of those, I went to create a form script that handles it. You could also modify it to be a script field (might even work as is) but again that would be a little disruptive to editing that might not be appreciated.

    Here is a quick little form script that runs on the currently selected magazine record, scans the linked articles and checks the keyword table to see if it exists and creates a new keyword record if it doesn’t exist. It’s actually a slightly modified version of the earlier script, just without the link creation steps:

    function Update_Keywords() {
    	// This is the other form that has our keywords in it.
    	let keywordForm = document.getFormNamed('Keywords');
    	
    	// This is the field ID in the "keywords" form for the link to field to Magazines.
    	let magazines_id = 'fld-b4724a28d4094b3faa9910f18374588f';
    
    	// This is the field ID in the "keywords" form for the keyword field.
    	let keywords_keyword_id = 'fld-ebaf7ffa3d484834bdcc34d3ffb9c5f2';
    	
    	// This is the field ID in the "magazine" form for the keyword field in the articles table.
    	let keyword_id = 'fld-a75febca3ee54d2d9d77b8c176ac08db';
    
    	// Articles link field ID
    	let articles_link_id = 'fld-9af3b672710949d8a96591e23ba5466b';
    
    	// This is to store a map of keyword to keyword record.
    	let kwRecords = {};
    
    	// Build the map of keyword to keyword record (cache if you will).
    	console.log("Finding keyword records...");
    	for (let keywordRecord of keywordForm.getRecords())
    	{
    		console.log("Adding keyword: " + keywordRecord.getFieldValue(keywords_keyword_id));
    		kwRecords[keywordRecord.getFieldValue(keywords_keyword_id)] = keywordRecord;
    	}
    	console.log("Completed finding keyword records.");
    
    	// Iterate over every record in this form...
    	for (let sourceRecord of record.getFieldValue(articles_link_id))
    	{
    		// Get the value of the keyword field...
    		let keyword = sourceRecord.getFieldValue(keyword_id);
    			
    		// Skip it if it is empty...
    		if (!keyword)
    		{
    			continue;
    		}
    
    		// Check if a keyword record exists for it...
    		if (!kwRecords[keyword])
    		{
    			// Create keyword record if it doesn't exist...
    			console.log('Creating keyword record');
    			kwRecords[keyword] = keywordForm.addNewRecord();
    			kwRecords[keyword].setFieldValue(keywords_keyword_id, keyword);
    		}	
    		
    		// Log the keyword we processed, we're done!
    		console.log(keyword);
    	}
    	
    	// Save the changes.
    	form.saveAllChanges();
    }
    
    Update_Keywords();
    #41299
    Sam Moffatt
    Participant

    If you only had one keyword, then I think a JOIN link to field type would work for you because you could have a single “keyword” record and then it’d automatically JOIN on other records. If you made “articles” it’s own form and used “Link to Form” fields to join it all together then that would work. You’ve gone with a table approach which can’t be used to handle the JOIN.

    If you wanted to keep the table, what could work is to leverage a script to maintain the links. I did a really quick one here;

    function Build_Keyword_Map() {
    	// This is the other form that has our keywords in it.
    	let keywordForm = document.getFormNamed('Keywords');
    	
    	// This is the field ID in the "keywords" form for the link to field to Magazines.
    	let magazines_id = 'fld-b4724a28d4094b3faa9910f18374588f';
    
    	// This is the field ID in the "keywords" form for the keyword field.
    	let keywords_keyword_id = 'fld-ebaf7ffa3d484834bdcc34d3ffb9c5f2';
    	
    	// This is the field ID in the "magazine" form of the table.
    	let articles_id = 'fld-dca513dfa3894e8aaa3792a9eb09b106';
    	
    	// This is the field ID in the "magazine" form for the keyword field in the articles table.
    	let keyword_id = 'fld-055da0f29f764ea399e6f317427ff8ed';
    
    	// This is to store a map of keyword to keyword record.
    	let kwRecords = {};
    
    	// Build the map of keyword to keyword record (cache if you will).
    	for (let keywordRecord of keywordForm.getRecords())
    	{
    		kwRecords[keywordRecord.getFieldValue(keywords_keyword_id)] = keywordRecord;
    	}
    
    	// Iterate over every record in this form...
    	for (let sourceRecord of form.getRecords())
    	{
    		// And iterate over each row in the articles table...
    		for (let article of sourceRecord.getFieldValue(articles_id))
    		{
    			// Get the value of the keyword field...
    			let keyword = article.getFieldValue(keyword_id);
    			
    			// Skip it if it is empty...
    			if (!keyword)
    			{
    				continue;
    			}
    
    			// Check if a keyword record exists for it...
    			if (!kwRecords[keyword])
    			{
    				// Create keyword record if it doesn't exist...
    				console.log('Creating keyword record');
    				kwRecords[keyword] = keywordForm.addNewRecord();
    				kwRecords[keyword].setFieldValue(keywords_keyword_id, keyword);
    			}
    			
    			// Link the keyword record to this record.
    			kwRecords[keyword].addRecordToField(sourceRecord, magazines_id);
    			
    			// Save the changes to make sure the link persists properly...
    			form.saveAllChanges();
    			
    			// Log the keyword we processed, we're done!
    			console.log(keyword);
    		}
    	}
    }
    
    Build_Keyword_Map();

    There are comments throughout which should explain what it does and I’ve attached a sample archive with the script in it inside the “Magazine” form. You can play with it to get it to add links. What it doesn’t do right now is handle if you delete or remove a keyword. You’d also have to do a split or similar to do a comma separated list of keywords as well but this is a quick example of how you can use scripting to build that link.

    If you change from a table to a child form then you could link a single keyword easily. If you wanted to do multiple keywords (e.g. Big Sur, Photos) then you’d be back needing a script again.

    Hopefully this helps :)

    Attachments:
    You must be logged in to view attached files.
    #40902
    Sam Moffatt
    Participant

    The Javascript API gives you the ability to iterate over all records in a form (form.getRecords()), you can use a search term (form.getRecordsForSearchTerm('term')) and you can also use a saved search (with the saved search selected, search.getRecords()).

    If you know what your criteria is and you can programmatically validate them then you can use a form script to update the values. I think I’d aim to have a saved search because you can review the target records and then use search.getRecords() to set the value.

    Something like this should work, you’d need to replace the field ID with your own:

    search.getRecords().forEach(rec => rec.setFieldValue('fld-changeme', 1337))
    

    If you put that into a form script (delete all of the boilerplate), update the ‘fld-changeme’ to match the field ID (if you double click on the left it should insert a place holder you can get it from) and then change 1337 to the number you want it to be then it should bulk fill the field for you. I’ve typed this a little off the cuff but it should work.

    With a little bit more work you could probably set it up with a prompter to prompt for the value but that should work to begin with.

    #40826

    In reply to: JavaScript API 5.3

    Rocky Machado
    Participant

    Hi Brendan – Thanks, I am able to use them. I just wasn’t sure if they were being deprecated in the future. I have a question relating to form.getRecordsForSearchTerm(). Not sure if I am understanding the function correctly. In my Trades form, I have created a trade Id that is used to identifies a cycle of a trade (buy and sell). I noticed that when I use the entire key, I get no record results. Here is a sample of the Trade Id TSLA-2020-06-01-Butterfly:503 . However, If I use parts of the Trade Id I get results. (i.e Butterfly:503, TSLA or 2020-06-01). Is this the correct behavior of the function?

    thanks,
    rocky

    #40750

    In reply to: JavaScript API 5.3

    Brendan
    Keymaster

    Hi Rocky,

    No, those haven’t been deprecated. In fact, I recently added the form.getRecordsForSearchTerm() function. The issue is I forgot to add those as keywords to the keywords file that tells the editor what functions to colour.

    For recalculating the form, just click the refresh button at the bottom of the record. Or at the bottom of the records list view.

    #40748
    Rocky Machado
    Participant

    Hi Brendan – I have a couple of questions. Have the following methods been deprecated. form.getRecordsForSearchTerm() and record.setRecordColor() It doesn’t show up in the IntelliSense. Also, in the future will you being adding a feature to recalculate a form via the javascript API?

    Oh, I’m running version 5.3.11 (Build 962)

    thanks,
    rocky

    #40684
    Victor Warner
    Participant

    Sam,

    Thank for the further reply,

    I eventually worked out how to use

    search.getREcords()

    , by replacing:

    for (let rec of form.getRecords()){

    with

    for (let rec of search.getRecords()){

    Just one follow-up question: Does the it only work with an advanced search and not an ordinary search (Command + F)? I have tried with an ordinary search but the script does not operate.

    #40421
    Victor Warner
    Participant

    I am getting the following error:

    02/05/2020, 18:24:01 / Time spent / add 0s to Time charged for (not empty)
    add 0s to Time charged for (not empty): TypeError: value.replace is not a function. (In ‘value.replace(find_text, replace_with)’, ‘value.replace’ is undefined), line:(null)

    when running the following script

    var time_charged_for_id = 'fld-755fd264b59b42e59c7254edf03ea281';
    
    function findAndReplace(find_text, replace_with) {
    	for (let rec of form.getRecords()){
    		let value = rec.getFieldValue(time_charged_for_id);
    		if (value) {	
    			rec.setFieldValue(time_charged_for_id, value.replace(find_text, replace_with));
    		}
    	}
    	form.saveAllChanges();
    	return;
    }
    
    findAndReplace(/[a-zA-Z0-9]*/, '0');
    

    I created a test database and it ran and did what is should. But in a working database it is causing the error. The field type is the same (a number field) in the test database and the working database.

    The code was provided by Daniel Leu in the exchanges at https://www.tapforms.com/forums/topic/find-and-replace-script-to-replace-anything/.

    Because I do not really have any understanding of JavaScript I cannot tell why it is not working.

    I would grateful for any help in identifying what is wrong.

    #40233
    Daniel Leu
    Participant

    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!

    #40227
    Victor Warner
    Participant

    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.

    #39822
    Sam Moffatt
    Participant

    If you’re doing an import flow you might want to add a field with a flag on it to control if you already did the search/replace on the record already. Depending on what your search/replace looks for, running it more than once might be problematic for you.

    Another trick you might be interested in is creating a saved search using “date created” to specify when you import the records to ensure that you only select and modify the records that were recently created. A slight change to Brendan’s script is to change:

    var records = form.getRecords();
    

    To something like:

    var records = search.getRecords();
    

    You’ll need to be in the search for the search object to exist but then it’ll only apply the script to the currently searched records.

    #39818
    Brendan
    Keymaster

    The Basic Loop Snippet will get you part of the way. But here’s a more comprehensive example:

    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');
    #39643
    Sam Moffatt
    Participant

    I’ll walk through this a little as this is my bulk scan script. It’s probably a bit much but it has a bunch of piece that might help.

    document.getFormNamed('Script Manager').runScriptNamed('Prompter Functions');
    

    This is pulling in my Prompter Functions via the Script Manager method. Basically Script Manager is a Tap Forms form named ‘Script Manager’ and then it uses ‘Form Scripts’.

    var tracking_number_id = 'fld-c487390743c947969cbe661cff596855';
    var received_date_id = 'fld-e3e3539ee04f4cc7971c7098c572104d';
    var confirmed_id = 'fld-2adb9ba8cdd048bbbb614d46b415ada5';
    var alternate_tracking_numbers_id = 'fld-cf8718051bea4cc2aba0069ae76f32b7';
    var alternate_tracking_number_id = 'fld-7342203d8f36415191bf8419fb6f70dc';
    var carrier_id = 'fld-0950c430cb0c41f79c51d43a544b366b';
    var zip_code_id = 'fld-4f73faa8937446a0a3b24e6dd4624d6b';
    var shipper_id = 'fld-1789ae45fd1f4f588294eff0c2fb6045';
    

    These are all of the field ID’s I care about, you’ll see them spliced through. Obviously your fields will be different.

    function findRecord(targetTrackingNumber)
    {
    	var targetRecord = null;
    	MainLoop:
    	for(candidateRecord of form.getRecords())
    	{
    		if (targetTrackingNumber == candidateRecord.getFieldValue(tracking_number_id))
    		{
    			targetRecord = candidateRecord;
    			break MainLoop;
    		}
    		
    		
    		for (alternateRecord of candidateRecord.getFieldValue(alternate_tracking_numbers_id))
    		{
    			if (targetTrackingNumber == alternateRecord.getFieldValue(alternate_tracking_number_id))
    			{
    				targetRecord = candidateRecord;
    				break MainLoop;
    			}
    		}
    	}
    	
    	return targetRecord;
    }
    

    This is a quick method to basically brute force scan all of the records and search for them. I mentioned earlier that Brendan is adding a function to make this a little less relevant however what it’s doing is getting a list of all of the records, checking if the tracking number matches (in your case it’ll be your box barcode or item barcode) and it also checks the values of a table field that is in my shipments form. Because there are two levels of loops in this, I use a trick to make sure I break out of them easily. Shipping is fun where a package might have more than one tracking number, hence the crazy setup.

    async function start(){
    	try {
    		while (1)
    		{
    

    This is the start of the meat and it has three things to be interested in. The async keyword is required to get my prompter functions to work properly and enable you to do blocking calls without having to manage the callbacks all yourself. The try is a fallback for an exceptions that might escape and the while (1) is our infinite loop.

    			let zipcode = undefined;
    			let carrier = '';
    			let barcode = '';
    			let scanBarcode = await promptText("Scan Shipment", "Barcode:");
    

    This sets up some variables, in my case I’m tracking zipcode for the package (USPS embed this into their tracking number), a carrier that I can automatically infer (USPS and UPS) and then obviously the core barcode. This uses the promptText function which is in that Prompter Functions I mentioned earlier. It has await prefixed to make it block until we get a response back.

    			if (!scanBarcode)
    			{
    				break;
    			}
    

    This is pretty straight forward, if we don’t get anything back from the barcode then we abort the loop. This is our exit criteria.

    			let matches = scanBarcode.match(/420(9[0-9]{4})(.*)/);
    			if (matches)
    			{
    				carrier = 'USPS';
    				zipcode = matches[1];
    				barcode = matches[2];
    			}
    			else
    			{
    				barcode = scanBarcode;
    			}
    

    This isn’t relevant to you as much but it basically looks for a USPS barcode and extracts the useful information out of it. It resets the barcode to be what the label and tracking number actually is versus the full details that are encoded into the barcode. I’m in California so my zipcode is prefixed with a 9 which is why it looks the way it does.

    			matches = scanBarcode.match(/^1Z/);
    			if (matches)
    			{
    				carrier = 'UPS';
    			}
    

    This also isn’t as relevant but it looks for a UPS style barcode and sets it automatically. Depending on how your barcode generation is done, this might be something you can apply where you have different barcode prefixes for stuff (or not).

    			console.log(barcode);
    			
    			let targetRecord = findRecord(barcode);
    

    I logged the barcode because I wanted to see what it was in the console. This is useful for understanding when something weird happens. This is then fed into that findRecord method before to find a record that matches, or not.

    			if (targetRecord)
    			{
    				// Flip confirmed flag but otherwise leave it alone.
    				targetRecord.setFieldValue(confirmed_id, true);
    				document.saveAllChanges();
    				console.log("Updated existing record for " + barcode);
    			}
    

    For me I’m looking to confirm or create a record that I’m scanning. In this case if for some reason the shipping number already exists and I found a matching record, I just toggle a flag saying that I know it exists and move on.

    			else
    			{
    				let payload = {
    					[tracking_number_id]: barcode,
    					[confirmed_id]: true
    				};
    

    Ok, the else case means this tracking number doesn’t exist already so we need to create it. I start to create a new record here. This syntax with the square brackets is to get the value of tracking_number_id instead of using tracking_number_id as the key.

    				if (carrier)
    				{
    					payload[carrier_id] = carrier;
    					payload[zip_code_id] = zipcode;
    				}
    

    If there is a carrier set then this gets set up as well including the zipcode (USPS).

    				let shipper = await promptText("Enter Shipper Name", "Shipper: ");
    				console.log(shipper);
    				
    				if (shipper)
    				{
    					payload[shipper_id] = shipper;
    				}
    

    I ask for the shipper name in case that’s obvious, again with the promptText method. That’s useful for knowing where something is from if I want to add it in.

    				console.log(JSON.stringify(payload));
    
    				let newRecord = form.addNewRecord();
    				newRecord.setFieldValues(payload);
    				document.saveAllChanges();
    

    I log out what I’m about to create to see what it is during debugging. I then create a new record, use setFieldValues to set up the values and then save the changes. Too easy!

    			}
    		}
    	} catch (error) {
    		console.log("Error: " + error);
    	}
    }
    
    start();

    This is closing everything out and then triggering the script to begin with. The catch is the follow up to the try and is a fail safe to log the message and go from there. It’s closing out the loop so assuming you enter in a valid barcode, it’ll keep looping until it’s done.

    I thought I had another script that handled a little closer to what you were doing but I can’t find where I put it.

    Here’s the script in full:

    document.getFormNamed('Script Manager').runScriptNamed('Prompter Functions');
    
    var tracking_number_id = 'fld-c487390743c947969cbe661cff596855';
    var received_date_id = 'fld-e3e3539ee04f4cc7971c7098c572104d';
    var confirmed_id = 'fld-2adb9ba8cdd048bbbb614d46b415ada5';
    var alternate_tracking_numbers_id = 'fld-cf8718051bea4cc2aba0069ae76f32b7';
    var alternate_tracking_number_id = 'fld-7342203d8f36415191bf8419fb6f70dc';
    var carrier_id = 'fld-0950c430cb0c41f79c51d43a544b366b';
    var zip_code_id = 'fld-4f73faa8937446a0a3b24e6dd4624d6b';
    var shipper_id = 'fld-1789ae45fd1f4f588294eff0c2fb6045';
    
    function findRecord(targetTrackingNumber)
    {
    	var targetRecord = null;
    	MainLoop:
    	for(candidateRecord of form.getRecords())
    	{
    		if (targetTrackingNumber == candidateRecord.getFieldValue(tracking_number_id))
    		{
    			targetRecord = candidateRecord;
    			break MainLoop;
    		}
    		
    		
    		for (alternateRecord of candidateRecord.getFieldValue(alternate_tracking_numbers_id))
    		{
    			if (targetTrackingNumber == alternateRecord.getFieldValue(alternate_tracking_number_id))
    			{
    				targetRecord = candidateRecord;
    				break MainLoop;
    			}
    		}
    	}
    	
    	return targetRecord;
    }
    
    async function start(){
    	try {
    		while (1)
    		{
    			let zipcode = undefined;
    			let carrier = '';
    			let barcode = '';
    			let scanBarcode = await promptText("Scan Shipment", "Barcode:");
    			
    			if (!scanBarcode)
    			{
    				break;
    			}
    
    			let matches = scanBarcode.match(/420(9[0-9]{4})(.*)/);
    			if (matches)
    			{
    				carrier = 'USPS';
    				zipcode = matches[1];
    				barcode = matches[2];
    			}
    			else
    			{
    				barcode = scanBarcode;
    			}
    			
    			matches = scanBarcode.match(/^1Z/);
    			if (matches)
    			{
    				carrier = 'UPS';
    			}
    			console.log(barcode);
    			
    			let targetRecord = findRecord(barcode);
    				
    			if (targetRecord)
    			{
    				// Flip confirmed flag but otherwise leave it alone.
    				targetRecord.setFieldValue(confirmed_id, true);
    				document.saveAllChanges();
    				console.log("Updated existing record for " + barcode);
    			}
    			else
    			{
    				let payload = {
    					[tracking_number_id]: barcode,
    					[confirmed_id]: true
    				};
    				
    				if (carrier)
    				{
    					payload[carrier_id] = carrier;
    					payload[zip_code_id] = zipcode;
    				}
    
    				let shipper = await promptText("Enter Shipper Name", "Shipper: ");
    				console.log(shipper);
    				
    				if (shipper)
    				{
    					payload[shipper_id] = shipper;
    				}
    				
    				console.log(JSON.stringify(payload));
    
    				let newRecord = form.addNewRecord();
    				newRecord.setFieldValues(payload);
    				document.saveAllChanges();
    			}
    		}
    	} catch (error) {
    		console.log("Error: " + error);
    	}
    }
    
    start();
    #39638
    Sam Moffatt
    Participant

    I have recently been working on something like that to scan a bunch of items into boxes and similar. There are a couple of ways of handling it.

    I’m going to rephrase some of your questions:

    Can I pull up a box by scanning it’s barcode?

    This one is relatively straight forward application of the search functionality, that’s easy out of the box regardless of what platform you’re on. On the desktop you’ll need a barcode scanner to make that work but on iOS the built in barcode scanning functionality will make that work for you there.

    Can I scan barcodes of items as I place them into the box?

    The way I did this in January when I was doing a lot of box packing for moving was to create a second form called “Box Items” that is linked from my main “Box” form and then created child records. I was taking pictures of the item and adding some extra metadata in this case so I just used the built in Tap Forms barcode scanner to scan items and used voice to text to quick enter titles (worked enough times to save me having to type).

    Now if you invest in a barcode scanner, you can take it a little bit further and create a prompter loop to scan barcodes and automatically create new child records. You can also pivot and if you’re confident an item will only be in a single box, you can make it’s box barcode a property of the item. I have a prompter loop I use for bulk scanning shipments, I’ll post that on the Script Talk forum later.

    Can I checkout of a box?

    If you go with the Link to Form and enable show inverse relationship (or what ever that checkbox is called), then when you scan in your item in Tap Forms using the search feature it will show you the record and the “Link From Form” will show you the details of the box that it thinks it should be in. In Tap Forms on the desktop you can select this and change it to search for a new box which you should be able to do by barcode for your checkout box or just have a box named “checkout”. By changing the value of the Link from Form field parent will register it to a new box. The iOS version has a similar interaction here but I feel that it isn’t quite as smooth as the desktop more due to the limitations of iOS rather than Tap Forms (the desktop shows a popover which makes it much easier to interact with).

    If you go for the field based approach, you just need to update the field to where ever you put the item.

    In both cases you can script it to handle it easier. Scripting the Link to Form/Link from Form is a little more difficult because in the current production release of Tap Forms there is no search by text via the script interface. Brendan mentioned that he’s adding a form.getRecordsForSearchTerm() to the next beta which would make that easier because the JavaScript API for the link fields works with records. Until then, it’s a little easier to update a simple text field based on a search.

    I have a dedicated barcode scanner for the desktop, I bought a NETUM Bluetooth scanner that has both a USB cable for input and can pair via Bluetooth. I’m not sure I’d recommend it, it works fine in Bluetooth mode but when I plug in the USB cable it mangles the numbers some how (it’s like it’s pressing on the keypad numbers upside down). On the Mac I use Karabiner Elements whose mere existence seemed to make the scanner behave properly.

Viewing 15 results - 106 through 120 (of 146 total)
 
Apple, the Apple logo, iPad, iPhone, and iPod touch are trademarks of Apple Inc., registered in the U.S. and other countries. App Store is a service mark of Apple Inc.