- This topic has 3 replies, 2 voices, and was last updated 3 years, 9 months ago by Sam Moffatt.
-
AuthorPosts
-
March 24, 2021 at 4:35 AM #43922
Victor WarnerParticipantThe database I use for some of my work has
1. a form which contains client contact details (this has separate sections for contact details for company clients and then individual clients)
2. a second form which contains the individual jobs
3. there is a Link to Form field from 1 to 2.In the jobs form I have a section which contains details of invoicing and I want to pull in the first name and address from the client contact form.
There are separate first name and email addresses fields in the client contact form for companies and individuals.
In the jobs form I wish to create a script the text of the email and would use the first name and address within the script (which I then export as CSV, and using Keyboard Maestro automatically create the email and automatically attach the invoice)
I have created a script where I have pulled in the fields from the client contact details and wish to create an if statement which selects the first name and email of whether the client is a company or an individual.
However, the script only produces ‘undefined’ although I know when I use the actual fields from the client contact form the console.log returns the correct information.
The script is as follows:
// Form ID var client_contact_details_id = 'fld-c53e48e48e3a40c7a5656ab92f39ecc9'; var result = ''; var EmailAddressRecord = record.getFieldValue(client_contact_details_id); var email_id = 'fld-8f9a928a7d554527b4127ef811136210'; var emailaddressfield = EmailAddressRecord.getFieldValue(email_id); result = emailaddressfield; result; var client_first_name_and_email_address_details = record.getFieldValue(client_contact_details_id) //Company details // company name var company_name_id = 'fld-a3300e3db044421598ae171c0a2d4834'; var company_name=client_first_name_and_email_address_details.getFieldValue(company_name_id); // first name of company contact var first_name_company_contact_id = 'fld-24ee4dae33d941bf81e1667853a7a949'; var first_name_company_contact=client_first_name_and_email_address_details.getFieldValue(first_name_company_contact_id); // email address of company contact var email_company_contact_id = 'fld-8cfa7613f04f4a8b82652bc27f3c05df'; var email_company = client_first_name_and_email_address_details.getFieldValue(email_company_contact_id); //Individual details // first name of individual var first_name_id = 'fld-45599a4f0a6342d8973faed53dfbfed8'; var first_name = client_first_name_and_email_address_details.getFieldValue(first_name_id); // Email address of individual var email_id = 'fld-8f9a928a7d554527b4127ef811136210'; var email_individual = client_first_name_and_email_address_details.getFieldValue(email_id); if (company_name == null) { var Dear_Name = first_name_company_contact; var Email_to_send_invoice = email_company; } else { var Dear_Name = first_name; var Email_to_send_invoice = email_individual; } // console.log(yesnot); console.log(Dear_Name + " " + Email_to_send_invoice);
Running the script produces:
24/03/2021, 11:36:03 / Notarial Act / Email adress for invoice
undefined undefinedHelp would be gratefully received.
March 24, 2021 at 11:14 PM #43927
Sam MoffattParticipantI think what might be happening is that you’re getting tricked up by Javascript variable scope, it doesn’t feel right but you’re declaring the
Dear_Name
andEmail_to_send_invoice
inside theif
statement. I would personally probably declare these at the top and maybe put a sentinel value in:// Return values var Dear_Name = "Placeholder"; var Email_to_send_invoice = "test@example.com";
This would then go to validate that they’re being overwritten properly. I don’t think that’s your problem here but it’s an approach I’d take as a first pass.
The second pass is rewriting so that the branches are a little more clear. You’re setting up a bunch of variables at the top of the script and then using a smaller
if
statement at the bottom so it’s hard to know which statements belong to the block. My next step is a rewrite to try to group the variables together:// Return values var Dear_Name = "Placeholder"; var Email_to_send_invoice = "test@example.com"; // Form ID var client_contact_details_id = 'fld-c53e48e48e3a40c7a5656ab92f39ecc9'; var client_first_name_and_email_address_details = record.getFieldValue(client_contact_details_id) //Company details // company name var company_name_id = 'fld-a3300e3db044421598ae171c0a2d4834'; var company_name = client_first_name_and_email_address_details.getFieldValue(company_name_id); console.log(company_name); if (company_name == null) { // first name of company contact var first_name_company_contact_id = 'fld-24ee4dae33d941bf81e1667853a7a949'; var first_name_company_contact = client_first_name_and_email_address_details.getFieldValue(first_name_company_contact_id); // email address of company contact var email_company_contact_id = 'fld-8cfa7613f04f4a8b82652bc27f3c05df'; var email_company = client_first_name_and_email_address_details.getFieldValue(email_company_contact_id); Dear_Name = first_name_company_contact; Email_to_send_invoice = email_company; } else { //Individual details // first name of individual var first_name_id = 'fld-45599a4f0a6342d8973faed53dfbfed8'; var first_name = client_first_name_and_email_address_details.getFieldValue(first_name_id); // Email address of individual var email_id = 'fld-8f9a928a7d554527b4127ef811136210'; var email_individual = client_first_name_and_email_address_details.getFieldValue(email_id); Dear_Name = first_name; Email_to_send_invoice = email_individual; } console.log(Dear_Name + " " + Email_to_send_invoice);
There was some dead code at the top that I eliminated along the way which makes it a little easier to follow.
I put in a
console.log(company_name)
to see what is in that variable so I would know what pathway to take. You’ve got a couple of theconsole.log
statements but I’d probably beef it up to make sure what you get back fromgetFieldValue
is set to a value.The last step is to make sure all of the field ID’s make sense. You’re not getting any runtime errors so the
client_first_name_and_email_address_details
field is set to a correct value but I’d double check the other ID fields. It’s easy to accidentally hook up the wrong fields (in fact I have it on video!) so it never hurts to go back and double check. Keep in mind that Tap Forms won’t give you an error if you give it a field ID that isn’t set in the record, it’ll return undefined. One other trick I have here is that in my Script Manager is a Form Logger that can output variables similar to the Tap Form’s Script Editor but prefixed with the form name as well, e.g.:// Boxes var boxes_formID = "frm-8ba92c77b5e8476a987945f40ff2b41a"; var boxes__title_fldID = "fld-e289240daccc40409d75aee26493110c"; // text var boxes__box_id_fldID = "fld-f25492cf56384369b278f7ea41ee258f"; // text var boxes__note_fldID = "fld-69754dee7e9247e0b3087e2526881626"; // note var boxes__dimensions_fldID = "fld-3e5e30f1d352495f989cc0dc1b306128"; // text var boxes__box_type_fldID = "fld-97b2ab2bdbb1400d9384c7634e7c6d48"; // text var boxes__box_photos_fldID = "fld-72a520cff50a4bbd92f1e8ff6eec91db"; // photo var boxes__box_is_full_fldID = "fld-c824d1ff2ced4b948b5907e0a4804bca"; // check_mark var boxes__purchases_fldID = "fld-482c4f5c71ba4cd7a06c32d1c1d7d983"; // form var boxes__containing_box_fldID = "fld-fe868108cdd844d895ea982e820b346c"; // text var boxes__boxes_fldID = "fld-74466a4b96dc43b4bcbe153646b76911"; // form var boxes__tracking_number_fldID = "fld-54af5c1e20e14d20a7c82a40679a2a8a"; // text var boxes__shipments_in_box_fldID = "fld-bc84b43e3cf649cdb27e4085b401aa2b"; // form
It makes it clear which field a variable is from because I ended up running into situations where it was hard to keep track of the forms for which a field applied. It also includes the field type as an extra hint which is sometimes useful to be reminded about.
March 25, 2021 at 3:34 AM #43930
Victor WarnerParticipantSam,
Thank you very much and for the revised code. At first (other than the first console.log) I was still getting “undefined”.
Then realised that the if statement is the wrong way around for the if part of the if/else:
if (company_name == null)
statement the code that follows should not be for the company for the individual.
After this it works fine.
Thank you again. I will have to explore your Script Manager Form Logger
March 25, 2021 at 8:33 PM #43934
Sam MoffattParticipantGreat to hear you solved it! The simplest things are sometimes the hardest to find, I did something not dissimilar earlier today where I flipped something that should be less than to greater than though fortunately a test case caught that quickly.
-
AuthorPosts
You must be logged in to reply to this topic.