Script to create record in another form with content from another

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Script to create record in another form with content from another

Viewing 7 reply threads
  • Author
    Posts
  • July 27, 2020 at 5:17 AM #41545

    Victor Warner
    Participant

    I have two forms (Client enquiry, Client Contact). In the example both have the same fields in each:

    First Name
    Last Name
    Description

    Would I would like to do:

    1. From a currently selected record in the Client Enquiry form;
    2. Create a new record in the Client Contact form;
    3. Put the values of First Name, Last Name and Description in the new record in the Client Contact form.

    Would I be creating a Form script rather than a field script?

    Would I be starting from the Client Enquiry form?

    Would be the code to do so?

    Any help would be gratefully received.

    July 27, 2020 at 7:10 AM #41547

    T.L. Ford
    Participant

    It sounds like what you might want is a linked table so you don’t have to duplicate the fields and can see all of the enquiries from a client.

    This would also sync things correctly if there is a typo in the Client’s name that you have to fix.

    July 27, 2020 at 8:28 AM #41548

    Victor Warner
    Participant

    T.L.

    Thank you for the reply. I do in fact want to do fact I described as the two forms will after creating the new record in the Client Contact form will no longer need to be in context with each other.

    The Client Enquiry form is there to capture details of a client enquiry, and if I am instructed I would like to then create a record in the Client Contact form without having to type some details again. But after that I will not be referring t the Client Enquiry form again.

    Also using scripting will help me to learn JavaScript.

    Any further help will be gratefully received…

    July 27, 2020 at 8:58 AM #41549

    T.L. Ford
    Participant

    Here’s a code solution and how I did it. Ignore where I went and got the form id – didn’t need it. There’s no sound (me and my computer recording software are at war just now).

    You can make a menu shortcut to run the script if you want. That’s at the bottom under the script name.

    Basically, the script gets the values for the fields on the current record, and puts those values on a new record on the Client Contact form. Your field IDs will be different.

    – T

    Attachments:
    You must be logged in to view attached files.
    July 27, 2020 at 9:06 AM #41552

    Daniel Leu
    Participant

    Yes, you would use a form script for this and start it from the client inquiry form. If you use a custom layout, you can even put a button there to start this script!

    Following is a form script that creates a new record in the form named “Client Contact” and copies first name, last name and description fields. This script assumes that the field names are “First Name”, “Last Name”, and “Description”:

    // Define enquiry form ids
    const firstNameId = form.getFieldNamed("First Name").getId();
    const lastNameId = form.getFieldNamed("Last Name").getId();
    const descriptionId = form.getFieldNamed("Description").getId();
    
    console.log(record.getFieldValue(firstNameId));
    
    function New_Client_Contact() {
    
    	// Define client form and ids
    	const clientForm = document.getFormNamed("Client Contact");
    	const clientFirstNameId = clientForm.getFieldNamed("First Name").getId();
    	const clientLastNameId = clientForm.getFieldNamed("Last Name").getId();
    	const clientDescriptionId = clientForm.getFieldNamed("Description").getId();
    
    	// Create new record
    	let clientRecord = clientForm.addNewRecord();
    	
    	// Populate new record
    	clientRecord.setFieldValue(clientFirstNameId, record.getFieldValue(firstNameId));
    	clientRecord.setFieldValue(clientLastNameId, record.getFieldValue(lastNameId));
    	clientRecord.setFieldValue(clientDescriptionId, record.getFieldValue(descriptionId));
    	
    	
    	// Save
    	clientForm.saveAllChanges();
    }
    
    New_Client_Contact();

    The beauty of this script is that it doesn’t use hard coded field ids. But if a field name changes down the road, the script will not run anymore. That’s the disadvantage of not using hard coded field ids ¯\_(ツ)_/¯

    If you want to copy all fields from “Client Enquiry” to “Client Contact”, I have a shorter version of the script that just loops over the fields. This script assumes that the fields are named the same in both forms and have the same type (eg, text field):

    function New_Client_Contact() {
    
    	// Define client form 
    	const clientForm = document.getFormNamed("Client Contact");
    	
    	// Create new record
    	let clientRecord = clientForm.addNewRecord();
    	
    	// Loop over fields and copy their content
    	for (field of form.getFields()){
    		let clientFieldId = clientForm.getFieldNamed(field.name).getId();
    		clientRecord.setFieldValue(clientFieldId, record.getFieldValue(field.getId()));
    	}	
    	// Save
    	clientForm.saveAllChanges();
    }
    
    New_Client_Contact();

    Note: I personally would use one form and have a field “status” that identifies if this is just an enquiry or if it turned into a client.

    July 28, 2020 at 8:42 AM #41561

    Victor Warner
    Participant

    T.L. and Daniel,

    Thank you both for the scripts. I am very grateful.

    Daniel,

    I have query on your second script. Using the Form names based on the following:

    1. the Client Enquiry has more fields then the 3 in the example but the additional fields do not exist in the Client Contact form.
    2. when the second script is run all the fields are copied for a record into the Client Contact form but although the ones for First Name, Last Name and Description are copied across to the Client Contact form the rest of disregarded.

    There is no downside (data corruption etc)?

    July 28, 2020 at 10:48 AM #41562

    Daniel Leu
    Participant

    Hi Victor,

    When I run the script where the Enquiry form has more fields than the Contact form, then the script creates errors. In my case, the additional field is before some of the valid fields and this results in not all fields being copied…

    I have enhanced the script by adding a check to verify that the Contact form contains a field with the same name:

    function New_Client_Contact() {
    
    	// Define client form 
    	const clientForm = document.getFormNamed("Client Contact");
    	
    	// Create new record
    	let clientRecord = clientForm.addNewRecord();
    	
    	for (field of form.getFields()){
    		if (clientForm.getFieldNamed(field.name)){
    			let clientFieldId = clientForm.getFieldNamed(field.name).getId();
    			clientRecord.setFieldValue(clientFieldId, record.getFieldValue(field.getId()));
    		} else {
    			console.log("Waring: Field '" + field.name + "' not found in 'Client Contact' form");
    		}
    	} 
    	// Save
    	clientForm.saveAllChanges();
    }
    
    New_Client_Contact();
    July 28, 2020 at 11:40 PM #41565

    Sam Moffatt
    Participant

    I did a script to copy records from one form to another generically a while back as well, a little longer than Daniel’s script but also leverages a prompter to offer a target form to use.

Viewing 7 reply threads

You must be logged in to reply to this topic.