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

saicharan3
Tera Expert

Hi @Asmita7

Here 

grInsert.u_instrument_manufacturer = "Lenovo";

 

Instead of providing Lenovo , If you paste the sys_id of the lenovo it will work fine

If it helps please click Accept as Solution/hit the Thumb Icon.

 

Prathamesh G
Kilo Sage
Kilo Sage

Hello @Asmita7,

 

Please use the below code to fulfil your requirement:

function insertDetails(){
var grInsert = new GlideRecord("u_details_data");
grInsert.initialize()
grInsert.u_instrument_manufacturer = "Put the har-coded sys_id of the Lenovo record";  
grInsert.insert();
}

 

If the above solution resolve your issue, Please mark the solution as 'Accepted Solution' and also mark it as 'Helpful'.

 

Thank You!

Prathamesh

Prathamesh G
Kilo Sage
Kilo Sage

Hello @Asmita7 , 

 

If my previous response helps in your issue to be resolve. Please mark my previous response as 'Accepted Solution'.

 

Thank You!

Prathamesh.