Simple Prompter for adding via Siri

Viewing 12 reply threads
  • Author
    Posts
  • February 1, 2022 at 10:06 AM #46561

    Prashant M
    Participant

    This is my first Script Trial , so apologise for my n00bs qs

    There are multiple prompter scripts floating on forum but I don’t know which one to pick up. I’ve tried to patch together adding a simple record in site_id but it fails

    var site_id = ‘fld-790b9d32090d4fd1873119a3e6401ec6’;
    function textPrompter(title, text=””) {
    return new Promise(function(resolve, reject) {
    let prompter = Prompter.new();
    prompter.cancelButtonTitle = ‘Cancel’;
    prompter.continueButtonTitle = ‘Continue’;
    prompter.addParameter(text,’prompterVar’)
    .show(title, ((status) => {
    if (status == false || prompterVar == undefined) {
    reject(‘Cancel’);
    } else {
    resolve(prompterVar);
    }
    }));
    });
    }

    var newRecord = form.addNewRecord();
    newRecord.setFieldValue(site_id, prompterVar);

    textPrompter();

    February 1, 2022 at 10:40 AM #46562

    Prashant M
    Participant

    I get error on above & below

    newRecord.setFieldValue(site_id,textPrompter);

    February 1, 2022 at 1:02 PM #46563

    Daniel Leu
    Participant

    The code you copied returns a Promise. To use this it, the function calling it must be defined as asynch. Additionally, you would need to call document.saveAllChanges() in order to save your new record.

    var site_id = 'fld-...';
    var prompterVar;
    
    function textPrompter(title, text="") {
    	return new Promise(function(resolve, reject) {
    		let prompter = Prompter.new();
    		prompter.cancelButtonTitle = 'Cancel';
    		prompter.continueButtonTitle = 'Continue';
    		prompter.addParameter(text,'prompterVar')
    		.show(title, ((status) => { 
    			if (status == false || prompterVar == undefined) {
    				reject('Cancel');
    			} else {
    				resolve(prompterVar);
    			}
    		}));
    	});
    }
    
    async function newRecord(){
    	try {
    		await textPrompter("Title", "Enter Name") 
    		var newRecord = form.addNewRecord();
    		newRecord.setFieldValue(site_id, prompterVar);
    		document.saveAllChanges();
    		
    	} catch (errorText) {
    		console.log('cancelled textPrompter: ' +errorText);
    	} 		
    }
    
    newRecord()

    Two notes: when you encompass your code segment in back-ticks, it nicely shows. Additionally, if you copy code from another post, it would be nice to link to that post in order to make it easier to help you.

    February 1, 2022 at 1:58 PM #46564

    Sam Moffatt
    Participant

    I also have some Prompter Functions in the Script Manager that might help you as well. Same note as Daniel that your calling function needs to be declared async to use these.

    February 2, 2022 at 5:47 AM #46571

    Prashant M
    Participant

    Dear Daniel , I will definitely use back-ticks and link to the post

    Thanks for the script , It works properly on my OSX also when I try to use the same on iPhone manually it works (via Scripts->RUN)

    BUT if I add the script to Siri Shortcuts , I simply get a DONE message and no prompt to enter. Please see screenshot attached for the same.

    Dear Sam, Thanks for the link , I’ve installed the same on OSX , and I see 19 scripts :) I will be studying these and updating !

    Attachments:
    You must be logged in to view attached files.
    February 2, 2022 at 8:18 PM #46579

    Sam Moffatt
    Participant

    When you use Siri Shortcuts, you need to use the x-callback-url feature to load Tap Forms to trigger the document interaction. I did a video on using the x-callback-url to trigger a script from Siri Shortcuts. Otherwise I believe it invokes Tap Forms in the background (via a library I think?) and doesn’t permit any interaction with scripting. The Siri interactions API is kind of limited in what it allows so if you want any sort of interactive experience, I’ve only really seen it possible via the Shortcuts app.

    February 10, 2022 at 11:29 PM #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 ?

    February 11, 2022 at 10:01 AM #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.
    February 13, 2022 at 7:36 AM #46722

    Prashant M
    Participant

    HI Sam ,
    Thanks for the reply ,as always they are really appreciated.

    1. The snippet you’ve showcased from your script , this is to be added “where” ? in Shortcuts to be defined as a variable “message” ?

    February 13, 2022 at 10:26 AM #46728

    Sam Moffatt
    Participant

    The snippet goes at the end of your script once you’ve finished everything. If you’re hooking into the prompter stuff, something like this is perhaps a slightly more concise example (this could be the callback from a prompter for example):

    var callbackFunction = function(continued) {
    	if (continued)
    	{
    		let newRecord = form.addNewRecord();
    		newRecord.setFieldValue(tracking_number_id, clipboard);
    		newRecord.setFieldValue(received_date_id, new Date());
    		newRecord.setFieldValue(confirmed_id, true);
    		document.saveAllChanges();
    		if (parameters && parameters["x-success"]) {
    			Utils.openUrl(parameters["x-success"] + "?message=Created%20record&tfrec=" + newRecord.getUrl());
    		} else {
    			Utils.openUrl(newRecord.getUrl());
    		}
    	} else {
    		if (parameters && parameters["x-cancel"]) {
    			Utils.openUrl(parameters["x-cancel"] + "?message=Cancelled");
    		}
    	}
    };
    

    The message in Shortcuts is the ?message=Blah on the Utils.openUrl line. You could call it anything really so long as it matches and the text is URL encoded. I hand craft those messages though there is encodeURIComponent that would take an arbitrary string and URL encode it for you.

    Hope that helps!

    February 20, 2022 at 4:19 PM #46801

    Prashant M
    Participant

    I apologise in advance , I’m trying to add the backpacks , but to no avail

    Hi Sam,

    I’m ashamed to say inspite of your dead simple instruction , I still haven’t managed to get this to work despite my multiple attempts . Items are added to the database but Shortcut still keeps running.

    Here is the code

     
    
    var site_id = 'fld-790b9d32090d4fd1873119a3e6401ec6';
    var prompterVar;
    
    function textPrompter(title, text="") {
    	return new Promise(function(resolve, reject) {
    		let prompter = Prompter.new();
    		prompter.cancelButtonTitle = 'Cancel';
    		prompter.continueButtonTitle = 'Continue';
    		prompter.addParameter(text,'prompterVar')
    		.show(title, ((status) => { 
    			if (status == false || prompterVar == undefined) {
    				reject('Cancel');
    			} else {
    				resolve(prompterVar);
    			}
    		}));
    	});
    }
    
    async function newRecord(){
    	try {
    		await textPrompter("Title", "Enter Name") 
    		var newRecord = form.addNewRecord();
    		newRecord.setFieldValue(site_id, prompterVar);
    		document.saveAllChanges();
    		
    	} catch (errorText) {
    		console.log('cancelled textPrompter: ' +errorText);
    	} 		
    }
    
    newRecord()
    document.saveAllChanges()
    
    var callbackFunction = function(continued) {
    	if (continued)
    	{
    		let newRecord = form.addNewRecord();
    		newRecord.setFieldValue(tracking_number_id, clipboard);
    		newRecord.setFieldValue(received_date_id, new Date());
    		newRecord.setFieldValue(confirmed_id, true);
    		document.saveAllChanges();
    		if (parameters && parameters["x-success"]) {
    			Utils.openUrl(parameters["x-success"] + "?message=Created%20record&tfrec=" + newRecord.getUrl());
    		} else {
    			Utils.openUrl(newRecord.getUrl());
    		}
    	} else {
    		if (parameters && parameters["x-cancel"]) {
    			Utils.openUrl(parameters["x-cancel"] + "?message=Cancelled");
    		}
    	}
    };
    
    

    Here is my complete ShortCut in attachment.

    Attachments:
    You must be logged in to view attached files.
    February 20, 2022 at 11:42 PM #46803

    Sam Moffatt
    Participant

    Just need to integrate it to your normal exec flow, so in the example the success is after the document.saveAllChanges so we integrate that there and similarly handle the else case:

    async function newRecord(){
    	try {
    		await textPrompter("Title", "Enter Name") 
    		var newRecord = form.addNewRecord();
    		newRecord.setFieldValue(site_id, prompterVar);
    		document.saveAllChanges();
    		if (parameters && parameters["x-success"]) {
    			Utils.openUrl(parameters["x-success"] + "?message=Created%20record&tfrec=" + newRecord.getUrl());
    		} else {
    			Utils.openUrl(newRecord.getUrl());
    		}		
    	} catch (errorText) {
    		console.log('cancelled textPrompter: ' +errorText);
    		if (parameters && parameters["x-cancel"]) {
    			Utils.openUrl(parameters["x-cancel"] + "?message=" + encodeURIComponent(errorText));
    		}
    	} 		
    }
    
    February 27, 2022 at 1:50 PM #46892

    Prashant M
    Participant

    Hi Sam,

    This finally works for me !! Thank you so much

Viewing 12 reply threads

You must be logged in to reply to this topic.