Upvise Form Scripting Samples
Formulas | When a form is submitted |
On Change |
Download Form Template Samples on GitHub
Overview
The Forms application allows to create custom Form Templates. You can even go further and create custom script to integrate it with other Upvise applications.
Form scripting can be as simple as adding 2 fields or as complex as you want, because you have access to the entire UpviseJS API. You can select, create or modify any record from any application and implement custom business and workflow logic with the power of Javacript.
The Form scripting language is Javascript and can be done at multiple levels:
- When a form is created, to set the initial form field value to a dynamically computed value
- When a form value changes during the form editing. This can be used to change other fields values
- When the form is submitted. This can be used to validate some form values, prevent the submission or start a custom action in the background
Compute the surface of a room
- Create a new form template
- Add 2 fields, named Width (F1) and Height (F2)
- Add a new formula field named Surface (F3)
- in the formlua field of this field, paste the following code
=F1*F2;
Display the Address of linked company in a formula field
- Create a new Template
- Add some fields
- Add a formula field, called "Company Address"
- Paste the following code in the formula field
=(link!=null) ? link.street : "";
Display a custom field value of linked project in a formula field
- Go to the Project App, under Options, click on Custom Fields tab
- Add a new project custom field, named "Type" (F1)
- In Forms app, Create a new Form Template
- Add some fields
- Add a formula field, called "Project Type"
- Paste the following code in the formula field
= (link != null) ? Forms.getCustomField("Projects.projects", link.id, "F1") : "";
Display the email of a selected contact field
- Create a new Template
- Add a ComboBox field of type Contact (F1)
- Add a Formula Field named "Contact Email"
- Paste the following code in the formula field
var contact = Query.selectId("Contacts.contacts"", F1); = (contact != null) ? contact.email : "";
Compute a score for a form based on response values
- Create a new template
- Add a first field (F1) of type Formula, to contain the score.
- Add multple questions and/or checkbox fields
- Edit the Score Field and paste the following code in the Formula field
var good = 0; var total = 0; var fields = Forms.getAllFields(form); for (var i = 0; i < fields.length; i++) { var field = fields[i]; if (field.type == "checkbox" || field.type == "toggle") { total++; if (field.value != "0" && field.value != "P" && field.value != "") good++; } } var score = (total > 0) ? Math.round(good*100/total) : 0; score + "%";
Change automatically an end date field when the start date changes
- Create a new template
- Add a first field "Start Date" (F1) of type Date
- Add a second field "End Date" (F2) of type Date
- Paste the following code in the onchange value for F1 field
var startdate = parseInt(Forms.getValue("F1")); enddate = Date.addMonths(startdate, 1); Forms.setValue("F2", enddate);
Dynamic Combo box options (1)
- We want to create a form. This form, when created from a Contact should display a combobox containing the list of all contracts for this contact
- Create a new template
- Add a new ComboBox field, named "Select Contract to renew"
- in the options field, paste the following code
= Query.options("Sales.contracts", "contactid=" + esc(link.id));
Dynamic Combo box options (2)
- Create a new template
- Add a new combo box field (F1) named "Continent"
- In the options textbox of F1 field, type in Europe|Asia|Australia
- In the on change textbox of F1 field, paste the following code
Forms.setValue("F2", "")
- Add a second combo box field (F2) named "City"
- in the Options textbox of F2 field, paste the following code
javascript: var continent = Forms.getValue("F1"); if (continent == "Europe") "Paris|London|Roma"; else if (continent == "Asia") "Singapore|Tokyo|Beijing"; else if (continent == "Australia") "Melbourne|Sydney|Perth"; else "Error : Please choose Continent First";
Modify the Form name during form submission
- This is useful if you want to override the default name of the form when it is submitted
- Create a new template
- Add a new ComboBox field of type Company (F31
- Add a new Text Box field (F2)
- Paste the following code in the on Submit field
var name = form.name; var company = Query.selectId("Contacts.companies", F1); if (company != null) name += "-" + company.name + "-" + F2; Query.updateId("Forms.forms", form.id, ""name"", name);
Create a Contract record from the form details
- Create a new form template
- Add a new text field named "Contract Name" (F1)
- Add a new date field named "Start Date" (F2)
- Add a new decimal field named "Amount" (F3)
- Add a new decimal field named "Sales Tax" (F4)
- Add a new long text field named "Note" (F5)
- Paste the following code in on submit
var contract = {}; contract.name = F1; contract.startdate = F2; contract.enddate = Date.addMonths(F2, 12); contract.period = 365; // days contract.amount = F3; contract.vat = F4; contract.note = "Aircon maintenance Contract: " + F5; contract.contactid = link.id; contract.number = form.name; Query.insert("Sales.contracts", contract); return 1;
Sending an instant notification when a form is submitted if a Form values has at least one NO Response
var hasFailed = false; var fields = Forms.getAllFields(form); for (var i = 0; i < fields.length; i++) { var field = fields[i]; if (field.type == "toggle" && field.value == "0") hasFailed = true; } if (hasFailed == true) { var msg = {}; msg.title = "FAILED " + Query.names("Forms.templates", form.templateid); msg.body = "submitted by: " + User.getName(); msg.onclick = "Forms.viewForm(" + esc(form.id) + ")"; var emails = "someone@gmail.com"; // leave empty to notify all managers Notif.sendNow(msg, emails); App.alert("Form has FAILED"); } return 1;
Sending a form PDF report in an email during the validation process.
- Create a new Form Template
- Add a Combobox field of type company (F1)
- Add some other fields
- Paste the following code in the on submit
var company=Query.selectId("Contacts.companies", F1); if (company != null && company.email != "") Forms.emailPdf(form.id, company.email, "Email Subject", "Email Body");
Creating a Quote when a form is submitted
- Create a new form template
- Add a Company ComboBox Field (F1)
- Add some fields
- Copy / Paste the following code in the OnSubmit field
- It will use all form values as the description for the quote
var companyid = F1; Forms.newQuote(form.id, F1);
Create a Task when a form is submitted
- Create a new Template
- Add a Date Field (F1)
- Add a Contact Combo Field (F2)
- Copy / Paste the following code in the onsubmit field
var values = {}; values.duedate = F1; values.contactid = F2; values.name = "This is the task name"; values.description = "This is the task content"; values.owner = User.getName(); values.startdate = Date.now(); var id = Query.insert("Tasks.tasks", values); App.alert("New Task Created!"); return 1;
Send by email a CSV File containing all values in the form
- Create a new template and add some fields
- Copy / Paste this code in the Onsubmit field of the template
var emails = "youremail@gmail.com"; Forms.emailCsv(emails, form.id);
How to change the Form ID based on the values of the form during submit
var name = form.name; // contains the default auto increment number for the form var company = Query.selectId("Contacts.companies", F1); // get the name of the company for the F1 field name += " - " + company.name; Query.updateId("forms", form.id, "name", name);
Change a custom field value of the linked Job during submit
- Make sure you have a custom field (F1) defined for Jobs App
- Create a new form template and add some fields
- Copy / Paste this code in the Onsubmit field of the template
if (link != null) Forms.setCustomField("Jobs.jobs", link.id, "F1", "Form Submitted!");
Extract values from barcode Scan
- Create a new form template
- Add a new Text Box field (F1) of type "scan"
- Add a new text field (F2) named "Raw Value"
- Add a new text field (F3) named "Label1 Value"
- Add a new text field (F4) named "Label2 Value2"
- Copy / Paste this code in the onchange field of the F1 field
Note: this code supposes that the raw value encoded in the scanned barcode has the format: LABEL1:xxxxx LABEL2:yyyyy
Forms.setValue("F2", value); // for debug var value1 = Forms.extractValue(value, "LABEL1:"); var value2 = Forms.extractValue(value, "LABEL2:"); Forms.setValue("F3", value1); Forms.setValue("4", value2);
Compute and display a score using a Score label
- Download this Score field template.
- Import it into your account Templates
- There are three Question fields and a Score label field
- The Score is computed each time a Question is replied
- The Form.updateScore is called in each 'onchange' of a Question. It is defined in the Scripting section of the Template (On Form Edit Custom Script)
Forms.updateScore = function(formid) { let ids = ["F1", "F2", "F3"]; let score = 0; for (let id of ids) { if (Forms.getValue(id, formid) == "1") score++; } let color = ""; if (score == 3) color = "green"; else if (score == 2) color = "blue"; else color = "red"; Forms.setValue("F4", score + ":" + color); }