Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

how to insert a record from back ground script through JSON format?

KM SN
Tera Expert

I need to insert a record in Incident table through JSON format can anyone help to let it happen??

1 ACCEPTED SOLUTION

Deepak Shaerma
Mega Sage

Hi @KM SN 
if your JSON looks like: try background script


{
“short_description”: “Sample Incident”,
“urgency”: “2”,
“impact”: “2”,
“category”: “software”,
“subcategory”: “email”,
“assignment_group”: “287ebd7da9fe198100f92cc8d1d2154e”, // Example Sys ID of an Assignment Group
“assigned_to”: “9eecfbd7a9fe198100f92cc8d1d2154e” // Example Sys ID of a User
}

 

// Define the JSON string
var jsonData = '{"short_description":"Sample Incident","urgency":"2","impact":"2","category":"software","subcategory":"email","assignment_group":"287ebd7da9fe198100f92cc8d1d2154e","assigned_to":"9eecfbd7a9fe198100f92cc8d1d2154e"}';

// Parse the JSON string into an object
var dataObj = JSON.parse(jsonData);

// Create a new GlideRecord instance for the incident table
var gr = new GlideRecord('incident');

// Initialize a new record
gr.initialize();

// Assign values from the JSON object to the record
gr.short_description = dataObj.short_description;
gr.urgency = dataObj.urgency;
gr.impact = dataObj.impact;
gr.category = dataObj.category;
gr.subcategory = dataObj.subcategory;
if (dataObj.assignment_group) {
    gr.assignment_group = dataObj.assignment_group;
}
if (dataObj.assigned_to) {
    gr.assigned_to = dataObj.assigned_to;
}

// Insert the new record into the database
var newRecordSysId = gr.insert();

// Output the sys_id of the new record for confirmation
gs.info("New incident inserted with sys_id: " + newRecordSysId);

 


 Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma 

View solution in original post

1 REPLY 1

Deepak Shaerma
Mega Sage

Hi @KM SN 
if your JSON looks like: try background script


{
“short_description”: “Sample Incident”,
“urgency”: “2”,
“impact”: “2”,
“category”: “software”,
“subcategory”: “email”,
“assignment_group”: “287ebd7da9fe198100f92cc8d1d2154e”, // Example Sys ID of an Assignment Group
“assigned_to”: “9eecfbd7a9fe198100f92cc8d1d2154e” // Example Sys ID of a User
}

 

// Define the JSON string
var jsonData = '{"short_description":"Sample Incident","urgency":"2","impact":"2","category":"software","subcategory":"email","assignment_group":"287ebd7da9fe198100f92cc8d1d2154e","assigned_to":"9eecfbd7a9fe198100f92cc8d1d2154e"}';

// Parse the JSON string into an object
var dataObj = JSON.parse(jsonData);

// Create a new GlideRecord instance for the incident table
var gr = new GlideRecord('incident');

// Initialize a new record
gr.initialize();

// Assign values from the JSON object to the record
gr.short_description = dataObj.short_description;
gr.urgency = dataObj.urgency;
gr.impact = dataObj.impact;
gr.category = dataObj.category;
gr.subcategory = dataObj.subcategory;
if (dataObj.assignment_group) {
    gr.assignment_group = dataObj.assignment_group;
}
if (dataObj.assigned_to) {
    gr.assigned_to = dataObj.assigned_to;
}

// Insert the new record into the database
var newRecordSysId = gr.insert();

// Output the sys_id of the new record for confirmation
gs.info("New incident inserted with sys_id: " + newRecordSysId);

 


 Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma