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,036 through 1,050 (of 3,053 total)
  • Author
    Search Results
  • #46714

    In reply to: Help with a script

    Stig Widell
    Participant

    You wrote this earlier:
    The Kalva function is there to create the calf, the first set of lines set up the various field ID’s we’re going to need later and then grabs the Avelstjur value for the current record. If that isn’t set we have an error message and return. It then looks for a match of that ID in the Avelstjurar form and if it doesn’t find it, also returns out. I did fix the type to match as number with the same formatting everywhere because I think initially it didn’t match properly due to a type mismatch (Javascript is a little more picky than TF).

    I cannot find that you are comparing the grupp field in form Aveltjurar with the grupp field in form Rekryteringsdjur? When these two match we have the correct Avelstjur and the father of the calf? That should be the crucial point.
    So my question is: is there a search for match between the grupp fields in the two forms in the script Kalva??

    #46708

    In reply to: Help with a script

    Stig Widell
    Participant

    You created the script Kalva to identify the field Avelstjur in form Rekryteringsdjur as I understood it??

    #46707

    In reply to: Help with a script

    Stig Widell
    Participant

    But the intention was not to use picklist at all but to let the script identify Avelstjur when grupp is the same in form Avelstjurar and form Rekryteringsdjur?

    #46705

    In reply to: Help with a script

    Stig Widell
    Participant

    Yes I know but why is the Avelstjur in form “Rekryteringsdjur” empty? The script should identify Avelstjur when field “grupp” is the same in records in form “Avelstjurar” and in form “Rekryteringsdjur”. When I insert a bull via picklist in form “Rekryteringsdjur” a calf record is created.

    #46703

    In reply to: Help with a script

    Sam Moffatt
    Participant

    The screenshot shows that Avelstjur is empty? It uses that field to determine the bull ID, with it empty then the script can’t find the bull to link to the child record.

    #46702

    In reply to: Help with a script

    Stig Widell
    Participant

    OK Sam,
    The font problem is fixed with Brendan´s help.
    The script problem is still there.

    #46691

    In reply to: Help with a script

    Sam Moffatt
    Participant

    That should only fire if Avelstjur is not set on the record you have selected in the Rekryteringsdjur form. If you’ve created a new field or are using a different field for the ID of the bull then you’ll need to update the field ID to match the new field ID on this line:

    	let keyValue = record.getFieldValue('fld-db52d7055ddd40cf95f50bcf8e2fff3d'); // ID of the Avelstjur field in Rekryteringsdjur form.
    
    

    The field ID is below the field description box which you can copy from there.

    #46690
    Sam Moffatt
    Participant

    I added this to my Script Manager so that if you want to set this up for a new script field it can look like this (once Script Manager is installed and update run):

    document.getFormNamed("Script Manager").runScriptNamed("PicklistID");
    
    PicklistID('fld-fa37906add2942c88bce3b500561c42d', 'Marketplace');

    One minor change was to fix an issue where the pick list was returned at the end if nothing was found and that now is returning a null that should show up as an empty value for the field (it was showing up was something like [TFPicklist object] I think).

    #46689

    In reply to: Help with a script

    Stig Widell
    Participant

    Sorry Sam but now when I run the Kalva script I am always stopped by this part:
    // check if the bull is even set or error.
    if (!keyValue) {
    Utils.alertWithMessage(“Fel!”, “Avelstjur värde ej inställt!”);
    return;
    Whats the problem?

    #46686
    Sam Moffatt
    Participant

    Yeah, you need to return to Shortcuts after the execution. I only recently figured this one out, here is a quick snippet from one of my scripts:

    
    		if (parameters && parameters["x-success"]) {
    			Utils.openUrl(parameters["x-success"] + "?message=Updated%20existing%20record&tfrec="+ targetRecord.getUrl());
    		} else {
    			form.selectRecord(targetRecord);
    			Utils.openUrl(targetRecord.getUrl());
    		}
    		return "Updated existing record for " + clipboard;
    

    I also updated the shortcut so that it displayed the result text as a notification and then bounced me back to Tap Forms. I need to work on figuring out if I can update the UI before the bounce to have me land back on the record after the script completes.

    Also attached a screenshot of the end of my Shortcuts that pairs with that script snippet.

    Attachments:
    You must be logged in to view attached files.
    #46683
    Mathieu Masseboeuf
    Participant

    Oh, indeed I didn’t noticed that answer !
    (just tried with the ascii stuff, but I would have to refill everything, or script a replacement)

    Indeed, last time I checked TF didn’t had scripting, never used it … will have to check that out :P

    #46679
    Prashant M
    Participant

    HI Sam,
    I’ve looked at the video you provided and I was able to call the script to work using ShortCuts .

    I did notice that the shortcut keeps working in the background & I need to manually stop it ,
    is there a way to stop the same ?

    #46675
    Sam Moffatt
    Participant

    I have a set of forms I call the Script Manager that has a combination of various scripts that can be relatively easily downloaded and imported into Tap Forms for re-use.

    #46674
    Rob Naaijkens
    Participant

    Thank you Sam! I gonna take a look at it and hope I understand what I have to put where. Did you mean by the last sentence that you put this script into my export?

    #46670
    Sam Moffatt
    Participant

    Ok, so some changes. I ditched the table field and made it a new form and added a link to form field to connect it. In the new form, creatively labelled “Verkocht”, I added a form script (or three): Aggregate by Date, aggregateForm and md5sum. Attached is a copy of the template that should upgrade an existing document with the appropriate fields and scripts though let me know if it’s missing something.

    Aggregate By Date is the script you’ll want to run to generate a report in “Nieuw formulier” grouped by date, name and the sum of aantal. If you look at it’s implementation, it looks like this:

    form.runScriptNamed("aggregateForm");
    
    function Aggregate_By_Date() {	
    	let fieldMap = {
    		['fld-cf72e8f115344d2fa33889757f9f19f0']: 'fld-28cad0a0ea4d4177aecd20e6f63fe470', // naam
    		['fld-ac527a6647d049869d5b3b26f8ef6d1d']: 'fld-4f2a8e2e7d974fc08f012a1889760397', // datum
    		['fld-ecae6c80bd38432abaaace22806dfb25']: 'fld-30f5df230f0b44479e53a83df9295e38', // aantal
    	};
    	
    	// set our date format, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
    	// remove "day" from it and you'll roll up by month for example.
    	let dateFormat = { "day": "2-digit", "month": "2-digit", "year": "numeric"};
    		
    	let translationMap = {
    		['fld-ac527a6647d049869d5b3b26f8ef6d1d']: (inputValue) => { return inputValue ? inputValue.toLocaleDateString("sv-SE", dateFormat) : ""; }
    	}
    	
    	let targetForm = document.getFormNamed("Nieuw formulier");
    	return aggregateForm(form, targetForm, 'fld-ecae6c80bd38432abaaace22806dfb25', fieldMap, translationMap, {'logcsv':true, 'hashedrecord': true});
    }
    
    Aggregate_By_Date();

    Ok, so we’re loading in the script “aggregateForm” at the start, that’s the utility/library file. Then we set up a field map, this maps the fields from the source form (in this case “Verkocht”) to the destination form (“Nieuw formulier”). Add extra to add more fields to the key/rollup or change up the ID’s to match.

    We want to rollup by date so we nee dto format the date, that’s what this dateFormat line does, specify the format for your date. Check out the docs to learn more but that’s pretty straight forward.

    The translationMap basically says to take the field fld-ac527a6647d049869d5b3b26f8ef6d1d (which should be a date) and turn it into a string representation of the date. If we don’t do this then we get big long date strings, not fun.

    Last lines say to get the Nieuw formulier form and then run this aggregateForm thing. It takes a source form (which is the current form selected; you could make this search too), it has a target form to create records in (or not if unset), the field ID of the field to aggregate, the two maps we talked about earlier and then the configuration options (logcsv creates a CSV in your console log and hashedrecord means that it creates and updates the same record).

    The aggregateForm script looks like this:

    form.runScriptNamed("md5sum");
    
    function aggregateForm(sourceForm, targetForm, aggregateField, fieldMap, transformerMap = {}, options = {}) {
    	for (let defaultKey in defaults = {
    		'logcsv': false,
    		'hashedrecord': false,
    		'returncsv': false,
    		'returnjson': false,
    	}) {
    		options[defaultKey] = options[defaultKey] !== undefined ? options[defaultKey] : defaults[defaultKey];
    	}
    
    	// check we have a source form, a field to aggregate and a mapping field.
    	if (!sourceForm) {
    		throw new ReferenceError("Unset source form");
    	}
    	
    	if (!aggregateField) {
    		throw new ReferenceError("Unset aggregate field");
    	}
    	
    	if (!fieldMap) {
    		throw new ReferenceError("Unset field map");
    	}
    	
    	if (fieldMap.length < 2) {
    		throw new ReferenceError("Field map must have at least two entries (aggregate field and key)");
    	}
    	
    	let rollups = {};
    	let destField = fieldMap[aggregateField];
    
    	// iterate to all of the records in the form
    	for (var rec of sourceForm.getRecords()) {
    		//console.log(rec.getId());
    
    		let keyFields = [];
    		let aggEntry = 0;
    		let keyEntries = {};
    		
    		for (let srcField in fieldMap) {
    			//console.log(srcField + " => " + fieldMap[srcField])
    			let value = rec.getFieldValue(srcField);
    			if (transformerMap[srcField]) {
    				//console.log("Transforming...");
    				value = transformerMap[srcField](value, rec);
    			}
    			//console.log(value);
    			
    			if (srcField == aggregateField) {
    				aggValue = value;
    			} else {
    				keyEntries[srcField] = value;
    				keyFields.push(value);	
    			}
    		}
    
    		var rollupKey = keyFields.join(",");
    
    		// Rollup to this key, add to the existing value or set it if not set.
    		if (!rollups[rollupKey]) {
    			rollups[rollupKey] = {};
    			for (let srcField in fieldMap) {
    				rollups[rollupKey][fieldMap[srcField]] = keyEntries[srcField];
    				rollups[rollupKey][destField] = aggValue;
    			}
    		} else {
    			rollups[rollupKey][destField] += aggValue;
    		}
    	}
    	
    	let retval = [];
    
    	// log to console the aggregated values.
    	for (let rollupKey in rollups) {
    		if (options['logcsv']) {
    			console.log(rollupKey + "," + rollups[rollupKey][destField]);
    		}
    		
    		if (options['returncsv']) {
    			retval.push(rollupKey + "," + rollups[rollupKey][destField]);
    		}
    		
    		if (targetForm) {
    			let destRecord;
    			if (options['hashedrecord']) {
    				let targetKey = "rec-" + md5(sourceForm.getId()+targetForm.getId()+rollupKey);
    				destRecord = targetForm.getRecordWithId(targetKey);
    				if (!destRecord) {
    					destRecord = targetForm.addNewRecordWithId(targetKey);
    				}
    			} else {
    				destRecord = targetForm.addNewRecord();
    			}
    			destRecord.setFieldValues(rollups[rollupKey]);
    		}
    	}
    	document.saveAllChanges();
    	
    	if (options['returnjson']) {
    		return JSON.stringify(Object.values(rollups));
    	}
    	
    	if (options['returncsv']) {
    		return retval.join("\n");
    	}
    	
    	return rollups;
    }
    

    Nothing to customise there, it’s all parameterised. I’ll put it up in the script manager over the weekend.

    Attachments:
    You must be logged in to view attached files.
Viewing 15 results - 1,036 through 1,050 (of 3,053 total)