- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 02:47 AM - edited 12-29-2023 07:45 AM
Hi friends,
I am creating insert a record. When the user clicks the record will be created in the custom table.
There is a reference field Instrument.
function insertDetails(){
var grInsert = new GlideRecord("u_details_data");
grInsert.initialize()
grInsert.u_instrument_manufacturer = "Lenovo";
// Inserting other variables
gr.insert();
Thank you
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 02:51 AM
Hello @Asmita7 ,
You can give a try to the code below and let me know how it works for you.
function insertDetails() {
var grInsert = new GlideRecord("u_details_data");
grInsert.initialize();
// Set the reference field to the manufacturer details record
grInsert.u_instrument_manufacturer = getManufacturerSysID("Lenovo");
// Set other variables
grInsert.some_other_field = "Some value";
// Insert the record
if (grInsert.insert()) {
gs.addInfoMessage("Record inserted successfully");
} else {
gs.addErrorMessage("Failed to insert record");
}
}
// Function to get the Sys ID of the manufacturer details record based on the name
function getManufacturerSysID(manufacturerName) {
var grManufacturer = new GlideRecord("u_manufacturer_details");
if (grManufacturer.get("u_name", manufacturerName)) {
return grManufacturer.sys_id;
} else {
gs.addErrorMessage("Manufacturer not found: " + manufacturerName);
return null;
}
}
Let me know your views on this and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 02:51 AM
Hello @Asmita7 ,
You can give a try to the code below and let me know how it works for you.
function insertDetails() {
var grInsert = new GlideRecord("u_details_data");
grInsert.initialize();
// Set the reference field to the manufacturer details record
grInsert.u_instrument_manufacturer = getManufacturerSysID("Lenovo");
// Set other variables
grInsert.some_other_field = "Some value";
// Insert the record
if (grInsert.insert()) {
gs.addInfoMessage("Record inserted successfully");
} else {
gs.addErrorMessage("Failed to insert record");
}
}
// Function to get the Sys ID of the manufacturer details record based on the name
function getManufacturerSysID(manufacturerName) {
var grManufacturer = new GlideRecord("u_manufacturer_details");
if (grManufacturer.get("u_name", manufacturerName)) {
return grManufacturer.sys_id;
} else {
gs.addErrorMessage("Manufacturer not found: " + manufacturerName);
return null;
}
}
Let me know your views on this and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 07:43 AM
Heyy Aniket,
It was just perfect !!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 02:59 AM
Hi @Asmita7 ,
try below script it should work
function insertDetails(){
var grInsert = new GlideRecord("u_details_data");
grInsert.initialize()
grInsert.u_instrument_manufacturer = "bb5ef18647093510105b8021336d43b0"; //for reference you need to give sysid
grInsert.insert();
}
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 03:06 AM - edited 12-29-2023 03:08 AM
Hi @Asmita7
For a Reference field type, it receives either the sys_id of the referencing record or the display value of that record.
So, in the line below, the value should be the sys_id of the Lenovo record.
grInsert.u_instrument_manufacturer = "Lenovo"; //Lenovo record sys_id
If you prefer to set the field with the Manufacturer's Name, ensure that the Display checkbox is checked for the Name field. Sample
Cheers,
Tai Vu