lamarh Posted August 15, 2021 Report Share Posted August 15, 2021 My app has two user bases, teachers and docents. Teachers schedule art lessons and docents sign up to teach the lessons. All docents and teachers have unique IDs and must log-in to the app. Docents can search the scheduled lessons and select a lesson to teach. Each lesson has 2 docents slots for lead and assistant. There are 2 fields for each slot - Lead_Docent and a virtual checkbox (check to lead); Assist_Docent and virtual checkbox (check to assist). If there is a value in Lead_Docent or Assist_Docent, I have a rule to disable the checkbox and make the docent field display only so the user can't delete/overwrite another docent. I need to assign the field Lead_Docent or Assist_Docent with the user's ID if the box is checked. This is what I've tried but it doesn't work. Help! Virtual1 is set to receive onload the value of the docent's ID through the authentication fields. Virtual 3 and Virtual 4 are checkboxes, value if checked is Y Lead_Docent and Assist_Docent are text fields assign docent.rtf Quote Link to comment Share on other sites More sharing options...
CoopperBackpack Posted August 16, 2021 Report Share Posted August 16, 2021 Hello @lamarh, Since you are using 'EditRecordField_name', I understand that this is the Details or Single Record Update DataPage. The code looks fine except for the quotes in the attached file. For example: This code works on my side: <script> document.addEventListener('BeforeFormSubmit', function(event) { const docentID = document.getElementById('cbParamVirtual1').value; const leadbox = document.getElementById('cbParamVirtual3').checked; const assistbox = document.getElementById('cbParamVirtual4').checked; if (leadbox) { document.getElementById('EditRecordLead_Docent').value = docentID; alert("you are signed up to lead"); } else if (assistbox) { document.getElementById('EditRecordAssist_Docent').value = docentID; alert("you are signed up to assist"); } }); </script> Did you check errors in console? Quote Link to comment Share on other sites More sharing options...
lamarh Posted August 16, 2021 Author Report Share Posted August 16, 2021 (edited) I didn't know about checking for errors in console (newbie to javascript!) Thanks for catching the quotes typo! And yes, this is a details page. It's still not working though. I copied and pasted your script and checked for errors. At the line document.getElementById('EditRecordLead_Docent').value = docentID I get the error Uncaught type error: cannot set property 'value' of null If I comment out that line and change the alert to alert(DocentID + ' is signed up to lead'); the alert works, with the correct DocentID, so DocentID isn't null The javascript is in the footer and doesn't run until the user submits. I've triple checked for typos in the field name. Any clues? Edited August 16, 2021 by lamarh added text Quote Link to comment Share on other sites More sharing options...
TellMeWhy Posted August 16, 2021 Report Share Posted August 16, 2021 35 minutes ago, lamarh said: I didn't know about checking for errors in console (newbie to javascript!) Thanks for catching the quotes typo! And yes, this is a details page. It's still not working though. I copied and pasted your script and checked for errors. At the line document.getElementById('EditRecordLead_Docent').value = docentID I get the error Uncaught type error: cannot set property 'value' of null If I comment out that line and change the alert to alert(DocentID + ' is signed up to lead'); the alert works, with the correct DocentID, so DocentID isn't null The javascript is in the footer and doesn't run until the user submits. I've triple checked for typos in the field name. Any clues? It's the document.getElementById('EditRecordLead_Docent') that's non-existent, are you sure this is Update Form or Submission Form? If this is a Submission Form. use InsertRecord instead of EditRecord Quote Link to comment Share on other sites More sharing options...
lamarh Posted August 16, 2021 Author Report Share Posted August 16, 2021 This is definitely an update form. The art docent searches the table for lessons that have been scheduled and selects one to teach. By checking the box, the Lead_Docent field should update to the user's ID, which is loaded into virtual1 as an authentication field. Once this is working, I add a rule to hide the checkbox when Lead_Docent is already taken by another docent so it can't be overwritten. Quote Link to comment Share on other sites More sharing options...
lamarh Posted August 17, 2021 Author Report Share Posted August 17, 2021 Problem solved and got rid of the javascript completely. I added a virtual field (Lead_on_load) that loads the value of Lead_Docent when the page loads. The Lead_Docent field is now a calculated field that either loads the user's ID or Lead_on_load depending on the status of the check box. When there is already a value in lead_docent, there is a rule to hide the checkbox so it's value is zero and Lead_Docent is "reset" to Lead_on_load. I hope that makes sense! I couldn't figure out why the Lead_Docent field was throwing the error. Love that I got rid of the javascript! ChristianM 1 Quote Link to comment Share on other sites More sharing options...
ChristianM Posted April 18, 2022 Report Share Posted April 18, 2022 Thanks to lamarh for commenting how they did this without Javascript. I couldn't get the Javascript to work and instead tried to do it all in CASPIO. Here is some code that might help. Using a single hidden calculated field, it stamps the author if another field has a certain value. In this case, I wanted this field to only be author stamped once, and never update if other fields are updated. Another example that updates an author field only if a certain field value is changed using a combination of a Triggered Action and a Calculated Field: Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.