Inserting data into a table

yundlu316
Kilo Guru

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();

1 ACCEPTED SOLUTION

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();


}



View solution in original post

13 REPLIES 13

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();


}



Thanks Tanumoy, so is the 2nd line needed?   Should the code be like this?



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();


}


Your code looks perfect to me. In case you just have to set any static value then the option is to add line like gr.field_name3 = "Test Here";


Hi Tanumoy, very strange...When I use the code above on 3-4 variables, it works great.   However, my form has 30+ fields, when I try it with all of the fields, the submit and page redirect stop working.   It's as if SN can't handle the script.   Any thoughts on this?


could you please post the full code.



Also make sure that u_basic_info has all the fields present whatever you mentioned in the script (field names should be same as in the table also).