- 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-25-2016 08:51 PM
Hi Tanumoy, here's my full code below: Edit fiddle - JSFiddle. I've added all of my variables back into the code and tested the form by entering a value and pressing submit, variable by variable. The submit button doesn't work until I enter a certain number of variables in the form. I've checked my syntax countless times now and can't seem to locate the issue. Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2016 05:48 AM
What I have noticed that you have use document.getElementById('<variable_name>').value; for all type of inputs like text, option, select-box etc. But this thing will work on text input not all of the inputs.
Like example for select-box you have to use document.getElementById('<variable_name>').innerHTML;
Please look into the below link to get what we need to use for different type of inputs.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2016 02:41 PM
Hi tanumoy, I have a follow up question that is really just for my own knowledge. I've read that GlideAjax may be a better option for scripts such as this. Just out of curiosity, what would the syntax be for the below GlideRecord code?
function submitRequest()
{
var gr = new GlideRecord('u_basic_info');
gr.initialize();
gr.field_name1 = document.getElementById('name1').value;
gr.field_name2 = document.getElementById('name2').value;
gr.field_name3 = document.getElementById('name3').value;
gr.insert();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2016 01:56 AM
You can try below codes. But please note I have not tested it in my instance.
UI page code:
<script>
function submitRequest()
{
var field1 = document.getElementById('name1').value;
var field2 = document.getElementById('name2').value;
var field3 = document.getElementById('name3').value;
var ajax = new GlideAjax('saveDataToTable');
ajax.addParam('sysparm_name','saveData');
ajax.addParam('sysparm_field1', field1);
ajax.addParam('sysparm_field2', field2);
ajax.addParam('sysparm_field3', field3);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer)
{
alert('Data has been saved successfully');
}
}
}
</script>
Script Include:
Name: saveDataToTable
Client Callable: True
Code:
var saveDataToTable = Class.create();
saveDataToTable.prototype = Object.extendsObject(AbstractAjaxProcessor, {
saveData: function(){
var fname1 = this.getParameter('sysparm_field1');
var fname2 = this.getParameter('sysparm_field2');
var fname3 = this.getParameter('sysparm_field3');
var gr = new GlideRecord('u_basic_info');
gr.initialize();
gr.field_name1 = fname1;
gr.field_name2 = fname2;
gr.field_name3 = fname3;
var sys_id = gr.insert();
return sys_id;
},
type: 'saveDataToTable'
});