Create a script that combines 2 records and that automatically fill a field

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Create a script that combines 2 records and that automatically fill a field

Tagged: 

Viewing 6 reply threads
  • Author
    Posts
  • January 2, 2025 at 11:16 AM #51432

    Jo M
    Participant

    Hi the community !

    I am a new user of tapforms since a few months but I want to develop my skills in automation and so on.

    Is there a way to create a script that combines 2 records and that automatically fill a field ?

    I explain myself :

    Field 1 : number phone field entered manually
    Field 2 : link field : https://api.whatsapp.com/send?phone= > this field never changes it’s a default value
    Field 3 : I want to automatically combine field 2 + field 1

    I tried with chatGPT, it gave me something that seemed to work but it doesn’t.

    So, my question is : is it possible to do so and is it possible for one of you to help me with this kind of script ?

    Thank you so much for your answer and sorry for my english if I did mistakes I am french ahaha

    Best regards,
    Jo M

    January 2, 2025 at 2:06 PM #51433

    Daniel Leu
    Participant

    Hi Jo,

    You can use the concat function or just + in the calculation field to combine two fields. But this wouldn’t create a valid URL since the phone number uses the format +1 (123) 456-7890.

    Here’s a field script that takes the phone number as input and sets the URL field as the result. Then you can click on the url globe and the Whatsapp API is called using your default browser.

    The whatsapp field is of type Web Site, and phone number is of type Phone Number. You have to update the id for these two fields. You can find their values in the script editor on the left side.

    function Script() {
    
    	// define used fields
    	const phone_number_id = 'fld-xxx';
    	const whatsapp_id = 'fld-xxx';
    
    	// define whatsapp API
    	const whatsappUrl = "https://api.whatsapp.com/send?phone=";
    	
    	// get phone number and format it to work with the API
    	let phone_number = record.getFieldValue(phone_number_id).replace(/[ ()-]/g,"").replace("+","00");
    	
    	// define call URL
    	let url = whatsappUrl + phone_number;
    	
    	// set URL
    	record.setFieldValue(whatsapp_id, url);
    	
    	// save changes
    	document.saveAllChanges();
    	
    	// output result to console
    	console.log(url);
    }
    
    Script();
    

    Cheers
    /daniel

    • This reply was modified 2 weeks, 5 days ago by Daniel Leu.
    January 2, 2025 at 4:36 PM #51441

    Brendan
    Keymaster

    There is another way to combine a field with a Website address field.

    Create a Website Address field and set the Default Value to be something like:

    https://api.whatsapp.com/send?phone=[Phone Number]

    If your other field is called Phone Number then when you click the globe button next to your Website Address field, Tap Forms will inject the value of the Phone Number field into the URL before it activates.

    January 2, 2025 at 6:56 PM #51442

    Daniel Leu
    Participant

    Wow, I didn’t know that you can do this here…. Cool feature, although it doesn’t work in this case because the phone number inserted is of the format +1 (234) 567-8901, which results in an invalid URL (https://api.whatsapp.com/send?phone=+1%20(234)%20567-8910 instead of https://api.whatsapp.com/send?phone=0012345678910).

    January 3, 2025 at 12:12 AM #51443

    Brendan
    Keymaster

    Tap Forms has lots of tricks up its sleeve :)

    I guess WhatsApp does not URL decode the parameter.

    January 3, 2025 at 9:43 AM #51446

    Jo M
    Participant

    Wow !

    Well, first of all, thank you so so so much for your answers guys ! I appreciate so much and it helps me to improve myself as much as I can but it’s not that easy when you’re a beginner all alone

    In fact, I want to do this kind of things with many fields such as links for Whatsapp, Telegram, Instagram and so on in order to create a big adress book of my contacts.

    The script you created seemed to contain an error (or I don’t know but I had an error message) that I could solve thanks to chatGPT (I wasn’t able to do it alone) :

    The issue is in the following line:

    javascript
    const phone_number = var phone_number_id = ‘fld-XXX’;
    In this line, you declare a constant (const phone_number) and try to initialize it with another declaration (var phone_number_id). This is not valid in JavaScript because a declaration cannot be used as an initialization value.

    Fix
    You need to separate the declarations and initializations properly. Here’s a corrected version of the script:

    Changes Made
    Separated Declarations:

    Removed const phone_number = var phone_number_id = … and declared phone_number_id and whatsapp_id_id separately.
    Correct Identifier Usage:

    Used phone_number_id and whatsapp_id_id in the appropriate places.

    I tried another one by myself, can you tell me what you think (take a look at the type of the field because it’s not a number phone here) ? It seems to work perfectly :

    function Nouveau_Script() {
        // Replace with your own code
        var hello_world = "Bonjour tout le monde !"; 
        return hello_world;
    }
    
    Nouveau_Script();
    
    function Script() {
        // Define used fields
        const id_id = 'fld-XXX';
        const lien_id_id = 'fld-XXX';
    
        // Define Twitter API
        const lien = "https://twitter.com/i/user/";
    
        // Get the ID from the record (assuming this method exists)
        let id = record.getFieldValue(id_id);
    
        // Define call URL
        let url = lien + id;
    
        // Set URL
        record.setFieldValue(lien_id_id, url);
    
        // Save changes
        document.saveAllChanges();
    
        // Output result to console
        console.log(url);
    }
    
    Script();

    – Is there a way to add the ID directly to the link or have I to create a 3rd field in which there will be [Link] + [ID] ? Or at least I’ll hide the useless field with the default value.
    – If I use the method of Brendan : will it create a complete link in the tapforms app (I mean will it combine the two fields in the link field and show the complete link or will it combine only if I click on the globe button in order to access to the website) ?

    Sorry in advance for my English mistakes.

    Best regards,
    Jo M

    (sorry for the duplication but I edited my message and it doesn’t appear so I post it again)

    January 3, 2025 at 12:11 PM #51447

    Brendan
    Keymaster

    My technique will only complete the link when you click the globe button.

    I suspect your error arose from a copy and paste error. Daniel’s script looks proper to me.

    Also, in your script you can delete the boilerplate code Nouveau_Script() stuff.

    January 3, 2025 at 4:51 PM #51450

    Daniel Leu
    Participant

    The script runs in my demo form.

    The whatsapp field is of type Web Site, and phone number is of type Phone Number. You have to update the id for these two fields. You can find their values in the script editor on the left side.

    You skipped this step and this resulted in the error. You have to set phone_number_id and whatsapp_id according to your form.

    Regarding your new script, you can remove Nouveau_Script() as this is just the default template generated when creating a new script. Otherwise, the script seems to be okay, apart that the two field ids are not set.

Viewing 6 reply threads

You must be logged in to reply to this topic.