I created two new libraries, Members and Applications, analogous to your ClientBase and Registration. I used very few fields:
- Members field, Name type Text single-line, role Entry Name.
- Members field Type, type Radio Buttons, items Individual, Family, and Institutional.
- Applications field Date, type Date, default current date, role Entry Name.
- Applications field City, type Text single-line autocomplete, role Entry Description.
- Applications field Years in city, type Integer, role Entry Description.
- Applications field Type, type Radio Buttons, items Individual, Family, and Institutional.
Then, here's what: did:
- I opened Applications, which took me into the Entries List screen, though there were as yet no entries.
- I tapped the menu (3-dot) menu icon and pressed Triggers on the drop-down list, taking me into the Triggers List card, though there were no triggers as yet.
- I pressed the shield icon on the top bar, opening the Permissions card.
- I set the switch for "Access to all libraries" to On and pressed OK.
- I pressed the round, blue + button in the lower-right corner to add a new trigger.
- I changed the default trigger name (Trigger 1) to "Creating, Before save".
- I set the Event to "Creating a new entry".
- I set the Phase to "Before saving the entry".
- I entered the script listed below.
- I pressed the checkmark in the upper-left corner of the card to save and exit back to the Entries List card.
Now, I'm done setting up -- time to test this out:
- I verified there were no entries in Members.
- I verified there were no entries in Applications.
- I pressed the round, blue + button in the lower-right corner of the screen to add an entry.
- I added a few. Each one completed normally; I checked frequently to confirm that entries were staying in Members.
- I tried to add one with the name "Bill Crews". When I pressed the checkmark to save it, it instead gave me the message "Not him. Try somebody else.", leaving me still in the Entry Edit card with my application data still there. I verified that no entry was created in Members for Bill Crews. That's my name; I put that code in the script as an example of data validation you could do and how it would work.
So, I suggest you do what I did. You may of course want to do it directly with your libraries instead of mine. Just make sure to set the permission as you go in to Registration to enter the trigger. While adding the trigger into Registration:
- Change the fields to your fields.
- Remove or replace the data validation with your own.
Script start ==>
// Get the application entry that's being saved and call it e (because you'll use it a lot).
// With that, you can readily reference fields in the new application entry about to be saved.
var e = entry();
// If you have any checks on the values of fields the user entered, you could do them here.
// If any validations fail, call cancel(), and the user remains in the Entry Edit card
// and will be prompted to fix something & try again.
if (e.field("Name") === "Bill Crews") {
message("Not him. Try someone else.");
cancel();
} else {
// From here on, the new application will be saved,
// so we must also create the new client entry.
// To create the entry in Members, we need to reference Members.
var members = libByName("Members");
// Start a new entry for Members.
var newMember = new Object();
// Set client fields from application data.
newMember["Name"] = e.field("Name");
newMember["Type"] = e.field("Type");
members.create(newMember);
}
Script end <==
Please let us (or just me) know how it went. Good luck.
Let me know if you have any comments.
Please provide the error message.
I've just made a trigger, to check JS field in the process.
message(entry().field("myJavaScruptField"));
(Opening, after display)
it showed the contents of the field correctly, which means that JS does return contents if there are contents.
Might it be that you need to establish the right order of data transfer (after update and after saving), so your JS field contents be transfered only *after* they are created?
(Eugene, it's a Creating a new entry / Before saving the entry trigger. I would guess she uses it also during Updating an entry; I don't know.)
I edited Members and created a one-line Text field called clientID there. I then edited Applications and added a Phone field called Phone and a JavaScript field also called clientID (-: I have no imagination. :-) with the following code:
field("Name").substr(0, 3) + field("Phone").substr(0, 3)
Finally, I edited the trigger and added this line of code (same as the others):
newMember["clientID"] = e.field("clientID");
My guess is that you didn't create the clientID field in Members or maybe used a field type inconsistent with a string or something like that. The trigger script plugs the value, but the field has to be there already.
A JavaScript field is real JavaScript too, but Memento hands off to JavaScript differently and provides the semicolon itself. As for multi-line JavaScript field expressions, I haven't explored this much, so I tend to put semicolons, but I'm not sure where/when they are needed.
As for the sample data validation code I gave you...
if (e.field("Name") === "Bill Crews") {
message("Not him. Try someone else.");
cancel();
} else {
// From here on, the new application will be saved,
...the main thing is to avoid mismatching braces. To excise this from your After script and just do the plugging, delete precisely the code I've shown above PLUS the closing brace at the very bottom of the script. That makes 4 braces deleted - a nice even number. You might copy & paste the script somewhere first, just in case things don't work as you anticipated.
Also, do you see the Play button on the trigger edit card (triangle pointing right)? Whenever you are done editing, you can test/preview it by tapping this button. The main benefit of doing this is to see if it completes without an error, and if there is an error, to try to figure out, from the top few lines of the message, what might have gone wrong. It might even have said "missing semicolon".
If you do not do it at the end of line, the environment may try to insert it itself, but there is no guarantee it will do so.
Omiting semicolons is generally a bad style that may often result in ambiguity, accidental merger of unrelated statements and hidden bugs.
The same is about html properties and quote marks. It is always more secure to write things like <font color="red"> rather than <font color=red>. If you try exporting the second code to some html parsers and analysers - it is highly likely to cause an error.
Html browsers allow omitting semicolons in JavaScript, to make messy internet pages still functional.
Nonetheless, it should be regarded as a deviation.
Like in real languages "Me ain't got no money" - may be regarded as a both correct and incorrect statement.
In Java, these frivolities are never allowed.
If your current script does a create() to the other library, the scripts will need to differ. The Update script will have to locate the corresponding entry in the other library, update it, and save it.