Adding record

Asmita7
Tera Expert

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

 

1 ACCEPTED SOLUTION

Aniket Chavan
Tera Sage
Tera Sage

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

 

View solution in original post

7 REPLIES 7

Aniket Chavan
Tera Sage
Tera Sage

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

 

Heyy Aniket,

 

It was just perfect !!!!

Pavankumar_1
Mega Patron

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

  

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Tai Vu
Kilo Patron
Kilo Patron

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

Timi_0-1703848097486.png

 

 

Cheers,

Tai Vu