I took the script out of the automatic generated function (Nieuw_Script) and now it works fine!
// *** Bereken het excl bedrag van het gegeven veld ***
var output = function berekenExBtw(continued) {
var exBtw = 0;
var btw = 0;
var exBedrag_ID = form.getFieldNamed('Bedrag 1').getId();
var BTW_TYPE_ID = form.getFieldNamed('btwType').getId();
var btwAmount = record.getFieldValue(BTW_TYPE_ID);
if (continued == true) {
if (inclusief) {
if (btwAmount == '9') {btw = 9};
if (btwAmount == '21') {btw = 21};
exBtw = (inclusief/(btw+100))*100;
// console.log("Het incl. bedrag is: " + inclusief + " Het excl. bedrag is: " + exBtw);
record.setFieldValue(exBedrag_ID, exBtw);
form.saveAllChanges();
}
} else {
console.log("Cancel button pressed.");
}
}
var inclusief = 0;
let prompter = Prompter.new();
prompter.cancelButtonTitle = 'Afbreken';
prompter.continueButtonTitle = 'Berekenen';
prompter.addParameter('Inclusief bedrag: ', 'inclusief')
.show('Bereken Exclusief BTW', output);
I found the post that I was thinking of but it was a generic counter implementation.
It’s a little more involved, here’s a simpler script I use with a link to form use case.
var order = record.getFieldValue('fld-c3a6725d7d9446da92bbb880ffe90a9e');
var order_items = order.getFieldValue('fld-9db0c7698499435ab6b18b7eb420e0ae');
var line_number = record.getFieldValue('fld-f95b68d488cb4b058bbf3de84e1a7c3b');
if (!line_number)
{
record.setFieldValue('fld-f95b68d488cb4b058bbf3de84e1a7c3b', order_items.length);
console.log('Setting line number to ' + order_items.length);
}
This is a little tricky, order is the parent record (the Link from Form field) , order_items are all of the children of the parent (a Link to Form field in the parent reecord) and then line_number is the line number. I could probably change it to use the max function that was added but for the most part this works for me. As Brendan suggested you could add in a multiplier (e.g. order_items.length * 10) to give yourself some space around the numbers.
The order and line_number field equivalents will be accessible via the script editor directly and for order_items I opened up the order form and got it’s field ID from there.
You should be able to modify it to work with a table field as well though I’ve personally never done much with table fields and scripts. There is something special about the scripting environment inside of a table though a quick glance at the documentation doesn’t reveal it. Perhaps Brendan can chime in on this one.
I tried the example script ‘Prompter class’ from the documentation,
but I get ‘undefined’ for every variable….
Is this a bug or do I something wrong?
Thanks, Andreas.
Attachments:
You must be
logged in to view attached files.
Yes, you could certainly do that with a Form Script.
You would use the functions: record.getFieldValue(field_id) to get the value from your Total field, then perform your calculation on that value, then call record.setFieldValue(vat_id) to set the value on your VAT field.
Then call form.saveAllChanges(); to save the results.
Yes, I could indeed use the calculation field, but in most cases I don’t need this calculation.
I now have an invoice with the input of services that are added together and then calculated with VAT to the total. Only in very rare cases do I have to calculate the other way round, so I have the total and have to calculate the amount without VAT.
Is this then possible to do it with the help of a script?
Tanks!
Attachments:
You must be
logged in to view attached files.
I have a script I use that I think I’ve posted elsewhere for the ordering of child records, that means you can edit the ordering field relatively straightforward. It’s a reply to a similar sort of thread. I’ll reply here if I find it again.
Thank you, but a brief look at the link does not provide how, what and where I add the relevant regular expression. The code provided in the linked page does not seem to relate at all (for someone who does not know JavaScript) to the examples you have provided.
Can you, Brendan, or someone else provide how it is possible to search for any text appearing in a field (as a wildcard, whether with regular expression or without) and replace it with something?
I have a prompter that allows a user to key in several pieces of information about a contact such as First Name, Last Name, Street Address, City, State, Zipcode, Phone Number, Email, Emergency Contact First Name, Emergency Contact Last Name, Emergency Contact Phone Number, etc. Due to the number of .addParameter entries the prompter window gets a little cluttered. To try and make it look a little more organized I would like to add some space in the prompter window between the Contact collection fields and the collection fields for their Emergency Contact. Is there a way to do this by adding a line with just plain text to show where the Emergency Contact entry fields start?
Hi,
I need a script which makes a simple calculation on user input.
The user inputs: amount inclusive tax and: a tax rate (from a list); the script then calculates the amount excl tax and puts it in the appropriate field.
e.g. user input 150,00 and 21%
output from script: 123,97
I tried it with the Prompter class example from the documentation but without succes…
I am not a programmer..
Thanks in advance!
Andreas.
You would definitely need a Script field for this.
So you would want to return the first or last sub-record within the table field, depending on how you’re sorting it. Then you would set your parent form to sort by the Script field.
Here’s a sample script:
function getTableDateValue() {
var table_id = 'fld-c433974d87de4bbca4eae626bc412408';
var date_time_id = 'fld-56d1a49577574de1a229e66f1acaf676';
var table_records = record.getFieldValue(table_id);
var date_time;
if (table_records.length > 0) {
var first_record = table_records[0];
var last_record = table_records[table_records.length - 1];
date_time = first_record.getFieldValue(date_time_id);
}
return date_time;
}
getTableDateValue();
Either you return the first_record’s date_time value or the last_record’s date_time value depending on how your Table field records are sorted and if you want to get the minimum or maximum date value.
Hope that gets you started. You’ll need to use your own field IDs of course.
Brendan
Is there a way to sort records by a table-field date? And if I have more than one event in the table, sorting the records by the newest date? The table itself is sorted in ascending order (the newest is last).
I can imagine that’s a good job for a script! I can read and adapt scripts, but not so well at all in writing them ?. But maybe there is a standard way…
For the second problem, in conventional find and replace, the find part would done through a wildcard
You can use regex: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
Okay, but neither work were
1. the 3 Test fields contain, respectively, 1, 2, and 3; and
2 I wish to replace with them with 0.
The second does, as you write, does add a 0 where the field is empty. That is one problem I am trying to solve. The other is to replace any text appearing in the field with a 0 (as a I mentioned already, the first script, simply adds the replacement 0 to the existing text and not replaces it). For the second problem, in conventional find and replace, the find part would done through a wildcard
My problem is that I do not know JavaScript so I do not know how to fix it to do it what I want.
Thank you for sharing your script, Victor.
The first script is NOT to be used with empty text. You need to use the second script for that. I deleted one test field and run the second script which properly updates the empty field with 0.
I created two scripts so others can profit as well. I don’t consider search for “” and replace with “something” to be a normal use case. That’s why I added the second script. For your use case, you only use the second script! It does a regular search and replace, and for cases where the field is empty, it puts the replacement text in there.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
Thank you for the further explanation. But I am clearly missing something.
In the attached sample Form I have a field “Test” with 3 records, containing 1, 2, 3.
I would like to replace all the entries with 0, but neither set of code works (creating Form Scripts you provided). The first one (“F&R – anything from something”) adds a 0 to 1, 2, 3 (01, 02, 03) while the second one (“F&R – nothing with something) does nothing at all.
Am I doing something wrong? Any further help would be gratefully received.
Attachments:
You must be
logged in to view attached files.