Add fields dynamically on form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2014 01:51 PM
Hi All,
I have a form for which users need to add additional fields. Rather than offer them an "other" choice which limits them to just 1 entry, we want to have a "+" symbol which will allow them to create and name the field(s). These fields should also be available for reporting.
Another issue which needs to be considered is when two different people enter the same name as the dynamic field.
Any suggestions?
Regards,
RV
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2014 07:53 AM
Hi RV,
This is a crude sample, so adjust as needed:
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<img id="customFieldAdder" src="images/add_circle.gif"/>
<script>
var u_customFieldAdderCount = 0; // Keep track of which 'Other' field was the last displayed
// When the plus image is clicked, the contained function will use g_form to show/hide as needed
Event.observe('customFieldAdder', 'click', function(event) {
u_customFieldAdderCount++; // Increment the counter
g_form.setDisplay('u_other' + u_customFieldAdderCount, true);
});
</script>
</j:jelly>
The key is to create this as a UI Macro. Then create a formatter that uses this macro for the table you desire. Finally, from Personalize Form, add the formatter you created AFTER all the 'Other' fields which you have hidden.
The script within the <script> tags then has access to g_form and all the associated API's you would normally use in Client Scripts. The Event.observe is simply using Prototype.js to run the script when the image is clicked.
Edit: And bear in mind, if you are willing to forgo the '+' image, you can accomplish this same behavior using a UI Action.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2014 08:04 AM
Thanks very much Travis. I'll try it out and get back to you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2018 11:02 AM
i was able to add a field to a table by doing a gliderecord, try this code below.
var gr = new GlideRecord ('sys_dictionary');
gr.intialize();
gr.name='u_iwo_table'; // this should be the table you want to add the field to.
gr.internal_type = 'String'; // the type of field, to see what kind of fields you can enter go to the sys_dictionary table
gr.column_label='Name';// the name of the field that you want
gr.element = 'u_iwo';//the variable name of that field, make sure you have a way of not creating duplicates
gr.max_length = '40';//the max length
gr.insert();//this oficially inserts the record
gr.update();// and finally this line updates it
I ran this from a backgroundscript but you should be able to do this on anything that permits gliderecord, it worked for me let me know if it works for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2020 03:50 PM
Hi Dhailu,
Your solution above is great. Do you know how can I specify which form tab the new field added should appear under? For instance a form can have several sections (tab) however I want the field to appear in the section of the form that is called Notes for instance. Kindly advise.
Thanks,
Johannes