How to insert multiple records in a table through scheduled job
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2024 04:14 AM
I will appreciate the explanation with the Script.
Thanks In Advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2024 04:25 AM
Am I missing the definition of 'attribute' in your script, or did I read over it? Could it be from the 'obj[i]' JSON? What is the exact issue? Is it only creating one record? That could be due to the check on existing records ('grassoc_check.query(); if grassoc_check.next()) .... It will update the record found instead of creating a new one.
I made some assumptions, but maybe this will help you along:
var grs = new GlideRecord("cmdb_ci_auto_data");
grs.addEncodedQuery("u_application_idISNOTEMPTY");
grs.query();
while (grs.next()) {
var app_id = grs.u_application_id;
var app_sysid = grs.sys_id;
var r = new sn_ws.RESTMessageV2('APPDATA', 'GET_APP_TRACK');
r.setStringParameterNoEscape('app_id', app_id);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
if (httpStatus == 200) {
var obj = JSON.parse(responseBody);
for (var i = 0; i < obj.length; i++) {
var attribute = obj[i].Attribute; // Ensure you have an Attribute in your JSON
var email = obj[i].Email;
var user_sysid = getUserSysId(email);
if (user_sysid) {
updateAssociation(app_sysid, user_sysid, attribute);
}
}
// Additional logic for setting operational status to 6 can go here
}
}
function getUserSysId(email) {
var gruser = new GlideRecord('sys_user');
gruser.addQuery('email', email);
gruser.query();
if (gruser.next()) {
return gruser.sys_id.toString();
}
return null;
}
function updateAssociation(appSysId, userSysId, attribute) {
var grassoc = new GlideRecord('u_associations');
grassoc.initialize();
grassoc.name = "Application Contacts";
grassoc.u_association_record = appSysId;
grassoc.u_user_association = userSysId;
grassoc.u_attribute = attribute; // Assuming this is how you determine RGE, incident, string, etc.
grassoc.operational_status = '1';
grassoc.sys_class_name = 'u_associations';
grassoc.insert();
}
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2024 04:58 AM
Hi Mark,
Thanks for your response,
But The above script is not working,
Here Records need to insert in the association table based on attribute value
Eg: If Attribute is ->RGE have 5 records
incidents have 3 records and
String have 4 records.
But this script showing only first record in each attribute. We need all records need to display in the association table.
Thanks