- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2016 07:12 AM
Hi Everyone, I was originally trying to build a record producer that would map to a custom table, but the formatting limitations of record producers is making me consider just building a Macro or UI Page instead. If I were to go this route, I plan to create a custom form using CSS/HTML, but am curious how to map variables back to my table. I did a bunch of googling and wound up here: http://wiki.servicenow.com/index.php?title=GlideRecord#Insert_Methods. I'm not familiar with javascript so was wondering if someone can confirm that this is the right approach to mapping variables back to and inserting into an existing table. Also, can someone explain to me how this code works? Would this be a client side script?
For example if i have a table named u_basic_info with a field called u_credit_card_number and I build a custom form with a insert text field, <input type="text" id="ccnum" name="u_credit_card_number">, how would I generate the below script so that when a user enters in their credit card number, it gets mapped back to the u_credit_card_number field in my my table u_basic_info?
var gr = new GlideRecord('to_do');
gr.initialize();
gr.name = 'first to do item';
gr.description = 'learn about GlideRecord';
gr.insert();
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2016 05:17 PM
If you have the input type in UI page then use document.getElementbyId('field_id').value to get the value. Then set that value to the table field by using gr.field_name.
function submitRequest()
{
var get = document.getElementById('ccnum').value;
var gr = new GlideRecord('u_basic_info');
gr.initialize();
gr.field_name1 = document.getElementById('abc').value;
gr.field_name2 = document.getElementById('xyz').value;
gr.field_name3 = "Test Here"; //and so on gr.u_credit_card_number = get;
gr.insert();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2016 07:22 AM
So basically what you have to do is, first you have to get the value what someone has given in u_credit_card_number in UI page.
You must have some button on that page. And after clicking that input will go to the table.
Example: onClick="submitRequest();"
<script>
function submitRequest()
{
var get = document.getElementById('ccnum').value;
var gr = new GlideRecord('u_basic_info');
gr.initialize();
gr.u_credit_card_number = get;
gr.insert();
}
</script>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2016 07:36 AM
Hi Tanuomy, thank you, you've been helping me so much! One follow up question regarding onClick button, if I create this Macro or UI page with the script you provided above, and I decide to insert it into a Record Producer that already has a default Submit button, would that be sufficient?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2016 10:03 AM
I just tried that in my dev instance. If you create a UI Page with the below code and create a UI Page type variable in a catalog item/record producer, it will insert a record in that table whenever that catalog item/record producer is loading. No need to submit anywhere.
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<input type="text" id="ccnum" name="u_credit_card_number">Text</input>
<script>
var get = document.getElementById('ccnum').value;
var gr = new GlideRecord('incident');
gr.initialize();
gr.short_description = "From UI page";
gr.insert();
</script>
</j:jelly>
I am not sure if you want this or not but this is not a good practice I believe.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2016 12:21 PM
Hi Tanumoy, your code works great! I have one more follow up question (excuse me, I'm brand new to JS), your below code will enter one variable into the table, what if I have 10 different variables that I want to enter into the same table upon Submit? What would that syntax look like?
function submitRequest()
{
var get = document.getElementById('ccnum').value;
var gr = new GlideRecord('u_basic_info');
gr.initialize();
gr.u_credit_card_number = get;
gr.insert();
}