How to save output of Prompter to a record

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk How to save output of Prompter to a record

Tagged: 

Viewing 4 reply threads
  • Author
    Posts
  • May 14, 2021 at 6:35 AM #44390

    Victor Warner
    Participant

    I am trying to learn how to the use Prompter command/function. I have created a simple form script using the Prompter class, and added what I believe are the correct commands to save the output. But on running the script it is not saving the output to the fields of a blank record.

    The form script:

    var date_id = 'fld-55a1678dc7b847f6be303140ad4779a3';
    var type_of_disbursement_id = 'fld-1214396547f84c9b8fcccac7917e0147';
    var unit_price_id = 'fld-8af21853bb8949bbbac92a1919fb5597';
    var number_id = 'fld-0616ad94503b4bd1b87bd4a1b0ce9d44';
    
    var output = function printOut(continued) {
           if (continued == true) {
    	   console.log(date_requested + ", " + type_of_disbursement + ", " + unit_price + ", " + number);
           } else {
              console.log("Cancel button pressed.");
           }
    }
    var date_requested;
    var type_of_disbursement;
    var unit_price;
    // var number;
    // var other_field;
    var number;
    var numbers = ['1', '2', '3', '4', '5'];
    
    let prompter = Prompter.new();
    prompter.cancelButtonTitle = 'Cancel';
    prompter.continueButtonTitle = 'OK';
    prompter.addParameter('Date: ','date_requested')
    .addParameter('Type of Disbursement: ', 'type_of_disbursement')
    .addParameter('Unit Price: ', 'unit_price')
    .addParameter('Number: ', 'number', 'popup', numbers)
    .show('Enter the type of disbursement, unit price and number wanted', output);
    
    record.setFieldValue(type_of_disbursement_id,type_of_disbursement);
    record.setFieldValue(unit_price_id,unit_price);
    record.setFieldValue(number_id,number);
    
    document.saveAllChanges();

    The Form is attached, with the form script (‘Prompt’)

    Also what commands would I need to add to create a new record before populating the fields with the output of the running the prompter?

    Any help would be gratefully received.

    Attachments:
    You must be logged in to view attached files.
    May 14, 2021 at 11:10 PM #44395

    Sam Moffatt
    Participant

    Creating a new record is done by form.addNewRecord(). It’ll return to you a new record object that you can use.

    The other item is that prompter runs async which means that after the show() line, the script keeps running. Conceptually, think of that as the last line of the script and when it’s finished, it will call the function you gave it, output in this case.

    Some personal preference, I try to keep the variables that are being predefined at the top. It sometimes makes a difference but I can never remember which language it’s important in.

    This should get you a little closer:

    var date_id = 'fld-55a1678dc7b847f6be303140ad4779a3';
    var type_of_disbursement_id = 'fld-1214396547f84c9b8fcccac7917e0147';
    var unit_price_id = 'fld-8af21853bb8949bbbac92a1919fb5597';
    var number_id = 'fld-0616ad94503b4bd1b87bd4a1b0ce9d44';
    var date_requested;
    var type_of_disbursement;
    var unit_price;
    // var number;
    // var other_field;
    var number;
    var numbers = ['1', '2', '3', '4', '5'];
    
    var output = function printOut(continued) {
           if (continued == true) {
    	   console.log(date_requested + ", " + type_of_disbursement + ", " + unit_price + ", " + number);
    	   let newRecord = form.addNewRecord();
    	   newRecord.setFieldValue(type_of_disbursement_id,type_of_disbursement);
    	   newRecord.setFieldValue(unit_price_id,unit_price);
    	   newRecord.setFieldValue(number_id,number);
    	   document.saveAllChanges();
           } else {
              console.log("Cancel button pressed.");
           }
    }
    
    let prompter = Prompter.new();
    prompter.cancelButtonTitle = 'Cancel';
    prompter.continueButtonTitle = 'OK';
    prompter.addParameter('Date: ','date_requested')
    .addParameter('Type of Disbursement: ', 'type_of_disbursement')
    .addParameter('Unit Price: ', 'unit_price')
    .addParameter('Number: ', 'number', 'popup', numbers)
    .show('Enter the type of disbursement, unit price and number wanted', output);
    May 15, 2021 at 12:17 AM #44396

    Victor Warner
    Participant

    Sam,

    Thank you very much.

    I cannot write that I understand the logic but it works.

    One remaining query, the date field is a Date Type, but in whatever format I enter it through the Prompter (01/01/20, 01/01/2020, 01 Jan 2020, etc.) a date is not added.

    Is there a way of entering a date?

    May 15, 2021 at 12:58 AM #44399

    Brendan
    Keymaster

    The Prompter doesn’t have direct support for date input, so you would need to parse it yourself.

    Here’s a link to various solutions for that:

    https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript

    May 15, 2021 at 2:36 AM #44401

    Victor Warner
    Participant

    Brendan,

    Thank you for the information.

Viewing 4 reply threads

You must be logged in to reply to this topic.