Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Help with if statement
- This topic has 4 replies, 3 voices, and was last updated 3 years ago by Sam Moffatt.
-
AuthorPosts
-
December 22, 2021 at 6:47 AM #46148
Victor WarnerParticipantI wish to create a script to deal with the following where the document contains two email fields:
1. if only one email field (“email”) has an email address the script assigns to a variable just that email fields contact; but
2. 2. if both email fields (“email” and “econd_individual_email”) have email addresses then the script assigns to a variable the following: “email_field_1 = ” and ” + email_field_2″
using an if statement testing whether another field (second_individual_client_name) contains anything.It works fine when the other field (“second_individual_client_name”) contains data but generates an error when it does not.
The script is as follows:
var client_name = record.getFieldValue('fld-ffa7651ff5bd48a9aa8184a69be8550e'); var email = record.getFieldValue('fld-152d99920304499f8f89c3868eb144d1'); var second_individual_client_name = record.getFieldValue('fld-772ee93b5eb747d294e5a9a7b548ebda'); var second_individual_email = record.getFieldValue('fld-d09998df75864a0e8f82b6f61188b537'); // set email for prescribed information document if (second_individual_client_name.length===0){ var email_for_prescribed_information = email; } else { var email_for_prescribed_information = email + " and " + second_individual_email; } console.log(client_name + "\n" + email_for_prescribed_information);
The error message generated is:
New Script: TypeError: undefined is not an object (evaluating 'second_individual_client_name.length'), line:(null)
Any help would be gratefully received.
December 22, 2021 at 8:53 AM #46149
Daniel LeuParticipantI would just do
if (second_individual_client_name){ var email_for_prescribed_information = email + " and " + second_individual_email; } else { var email_for_prescribed_information = email; }
December 22, 2021 at 10:13 AM #46150
Sam MoffattParticipantTo expand a little,
second_individual_client_name.length===0
is failing because if the field isn’t set to a string value, it’s an undefined value or null. The.length
only works ifsecond_individual_client_name
is set to a string but if the field isn’t entered, Tap Forms returns a null/undefined value to signify the difference between a field with an empty string and a completely unset field.Javascript has the concept of “falsy” values that are values that evaluate to “false” when used in an
if
test situation. In this case not only is null and undefined a “falsy” value, but an empty string is also a falsy value. This means that for your purposes, that is to say to check if the field has a value, this simpleif
test does what you need without validating the length.[1] falsy: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
December 23, 2021 at 9:07 AM #46161
Victor WarnerParticipantDaniel, thank you for the solution.
Sam, thank you for the explanation.
Query: how is it possible to get the red text within a paragraph of a post?
December 24, 2021 at 12:05 AM #46165
Sam MoffattParticipantUse backticks to surround the text and it will automatically do the preformatted text with the colouring if there is no new line. If there is a new line it’ll change behaviour in the text as you had in the original post.
-
AuthorPosts
You must be logged in to reply to this topic.