Utility Script for Incident Creation periodically
Script Include:
var DataGenerator = Class.create();
DataGenerator.prototype = {
var type = ['reassign1','reassign2','close2','close3','close10','breach', 'reopen', 'other'];
var type1 = Math.floor((Math.random() * type.length));
this.category = ['inquiry','software','hardware','network','database'];
this.channel = ['chat','email','phone','self-service','virtual_agent','walk-in','monitoring'];
this.state = [1,2,3,6,7,8];
this.close_code = ['Duplicate','Known error','No resolution provided','Resolved by caller','Resolved by change','Resolved by problem','Resolved by request','Solution provided','Workaround provided','User error'];
this.close_notes = ['Duplicate','Known error','No resolution provided','Resolved by caller','Resolved by change','Resolved by problem','Resolved by request','Solution provided','Workaround provided','User error'];
"business_service":"57ed1b00dbc7ff408606f2131f96194f",
"cmdb_ci" : "0aa9e40d3790200044e0bfc8bcbe5df2",
"service_offering":"5cf56f8cdbc7ff408606f2131f961918",
"assignment_group":"d625dccec0a8016700a222a0f7900d06",
"short_description" : ["Users are not able to access the application","The HDMI port on my PC stopped working","Application is down"],
"description": ["Users are not able to access the application","The HDMI port on my PC stopped working","Application is down"],
"business_service":"57ed1b00dbc7ff408606f2131f96194f",
"cmdb_ci" : "0aa9e40d3790200044e0bfc8bcbe5df2",
"service_offering":"5cf56f8cdbc7ff408606f2131f961918",
"assignment_group":"d625dccec0a8016700a222a0f7900d06",
"short_description" : ["Users are not able to access the application","The HDMI port on my PC stopped working","Application is down"],
"description": ["Users are not able to access the application","The HDMI port on my PC stopped working","Application is down"],
"business_service":"57ed1b00dbc7ff408606f2131f96194f",
"cmdb_ci" : "0aa9e40d3790200044e0bfc8bcbe5df2",
"service_offering":"5cf56f8cdbc7ff408606f2131f961918",
"assignment_group":"d625dccec0a8016700a222a0f7900d06",
"short_description" : ["Users are not able to access the application","The HDMI port on my PC stopped working","Application is down"],
"description": ["Users are not able to access the application","The HDMI port on my PC stopped working","Application is down"],
var json = JSON.stringify(jsonOBJ);
var jsonObj = jsonOBJ[Math.floor(Math.random() * jsonOBJ.length)];
var group = jsonObj["assignment_group"];
var grMember = new GlideRecord('sys_user_grmember');
grMember.addEncodedQuery('group=' + group);
while (grMember.next()) {
mem.push(grMember.user.toString());
var createdBy = this.getCaller();
var updatedBy = this.getCaller();
var inccount = tickets+Math.floor((Math.random() * 30) + 1);
var surveyID = '87186844d7211100158ba6859e610378';
incCreation :function(jsonObj,createdOn, createdBy, updatedBy, type){
gs.print('JSON-- ' + jsonObj);
var inc = new GlideRecord('incident');
inc.business_service = jsonObj["business_service"];
inc.cmdb_ci = jsonObj["cmdb_ci"];
inc.service_offering = jsonObj["service_offering"];
var cat1 = Math.floor((Math.random() * this.category.length));
inc.category = jsonObj["category"] || this.category[cat1];
var imp1 = Math.floor((Math.random() * this.impact.length));
inc.impact = jsonObj["impact"] || this.impact[imp1];
var urg1 = Math.floor((Math.random() * this.urgency.length));
inc.urgency = jsonObj["urgency"] || this.urgency[urg1];
inc.assignment_group = jsonObj["assignment_group"];
var assn1 = Math.floor((Math.random() * this.assigned_to.length));
inc.assigned_to = this.assigned_to[assn1];
var sd1 = jsonObj["short_description"];
var sd2 = Math.floor((Math.random() * sd1.length));
inc.short_description = sd1[sd2];
var desc1 = jsonObj["description"];
var desc2 = Math.floor((Math.random() * desc1.length));
inc.description = desc1[desc2];
var con1 = Math.floor((Math.random() * this.contact_type.length));
inc.contact_type = jsonObj["contact_type"] || this.contact_type[con1];
inc.caller_id = createdBy;
inc.sys_created_on = createdOn;
inc.sys_created_by = createdBy;
inc.opened_at = createdOn;
inc.opened_by = createdBy;
inc.sys_updated_on = createdOn;
inc.sys_updated_by = updatedBy;
// var assn1 = Math.floor((Math.random() * this.assigned_to.length));
// inc.assigned_to = this.assigned_to[assn1];
//inc.incident_state = 2;
var tic = new GlideRecord('u_tickets');
tic.u_task_type = inc.sys_id;
tic.u_status = 'initiated';
tic.u_payload = JSON.stringify(jsonObj);
tic.u_caller = createdBy;
tic.sys_created_on = createdOn;
tic.sys_created_by = createdBy;
tic.sys_updated_on = createdOn;
tic.sys_updated_by = updatedBy;
var temp = new GlideRecord('sys_user');
temp.addEncodedQuery('active=trueemail!=NULL');
user.push(temp.sys_id.toString());
var caller = Math.floor((Math.random() * user.length));
var userID = user[caller];
calcStartDate : function(incr){
inc_start_date = days - (interval*incr);
gdt = new GlideDateTime();
gdt.addDays(-1 * inc_start_date);
calcTypes : function(inccount){
gs.print("inside"+inccount);
gs.print(Math.floor(inccount*reassign1f));
res["reassign1"]= Math.floor(inccount*reassign1f);
res["reassign2"]= Math.floor(inccount*reassign2f);
res["close2"]= Math.floor(inccount*close2f);
res["close3"]= Math.floor(inccount*close3f);
res["close10"]= Math.floor(inccount*close10f);
res["breach"]= Math.floor(inccount*breachf);
res["reopen"]= Math.floor(inccount*reopenf);
res["other"] = inccount - Math.floor(inccount*reassign1f) - Math.floor(inccount*reassign2f) - Math.floor(inccount*close2f) - Math.floor(inccount*close3f) - Math.floor(inccount*breachf) - Math.floor(inccount*close10f)-Math.floor(inccount*reopenf);
var iter = (days/interval)-1;
var sdate = this.calcStartDate(i);
var robj = this.calcTypes(inccount);
gs.print(JSON.stringify(robj));
var reassign1Count = robj["reassign1"];
gs.print('ReassiCount1-- ' + reassign1Count);
for(var j=0; j<reassign1Count; j++)
var callerID = this.getCaller();
var updatedBY = this.getCaller();
this.reassign1(sdate,callerID,updatedBY);
var reassign2Count = robj["reassign2"];
gs.print('ReassiCount2-- ' + reassign2Count);
for(var k=0; k<reassign2Count; k++)
callerID = this.getCaller();
updatedBY = this.getCaller();
this.reassign2(sdate,callerID,updatedBY);
var close2Count = robj["close2"];
gs.print('CloseCount2-- ' + close2Count);
for(var l=0; l<close2Count; l++)
callerID = this.getCaller();
updatedBY = this.getCaller();
var closeInc2 = this.close2(sdate,callerID,updatedBY);
this.triggerSurvey(surveyID,callerID);
var close3Count = robj["close3"];
gs.print('CloseCount3-- ' + close3Count);
for(var m=0; m<close3Count; m++)
callerID = this.getCaller();
updatedBY = this.getCaller();
this.close3(sdate,callerID,updatedBY);
this.triggerSurvey(surveyID,callerID);
var close10Count = robj["close10"];
gs.print('CloseCount10-- ' + close10Count);
for(var n=0; n<close10Count; n++)
callerID = this.getCaller();
updatedBY = this.getCaller();
this.close10(sdate,callerID,updatedBY);
this.triggerSurvey(surveyID,callerID);
var otherCount = robj["other"];
gs.print('OtherCount-- ' + otherCount);
for(var p=0; p<otherCount; p++)
callerID = this.getCaller();
updatedBY = this.getCaller();
this.other(sdate,callerID,updatedBY);
reassign1: function(sdate,callerID,updatedBY){
var incRec = this.incCreation(jsonObj,sdate, callerID,updatedBY,'reassign1');
gs.print('IncidentRec ' + incRec);
incRec.assignment_group = 'ddf44252d7133100816403548e61035e';
incRec.assigned_to = '0d5b61dfc0a8026601c8e80d8bb57f6e';
incRec.reassignment_count = 1;
incRec.opened_at = sdate;
incRec.sys_created_on = sdate;
incRec.sys_updated_on = new GlideDateTime();
incRec.sys_updated_by = updatedBY;
reassign2: function(sdate,callerID,updatedBY){
var incRec2 = this.incCreation(jsonObj,sdate, callerID,updatedBY,'reassign2');
gs.print('IncidentRec2 ' + incRec2);
incRec2.assignment_group = 'ddf44252d7133100816403548e61035e';
incRec2.assigned_to = '0d5b61dfc0a8026601c8e80d8bb57f6e';
incRec2.reassignment_count = 2;
incRec2.opened_at = sdate;
incRec2.sys_created_on = sdate;
incRec2.sys_updated_on = new GlideDateTime();
incRec2.sys_updated_by = updatedBY;
close2: function(sdate,callerID,updatedBY){
var incRec3 = this.incCreation(jsonObj,sdate, callerID,updatedBY,'close2');
gs.print('IncidentRec3 ' + incRec3);
var cc = Math.floor((Math.random() * this.close_code.length));
var cc1 = this.close_code[cc];
var cn = Math.floor((Math.random() * this.close_notes.length));
var cn1 = this.close_notes[cn];
incRec3.opened_at = sdate;
incRec3.sys_created_on = sdate;
incRec3.close_code = jsonObj["close_code"] || cc1;
incRec3.close_notes = jsonObj["close_notes"] || cn1;
incRec3.resolved_at = new GlideDateTime();
incRec3.resolved_by = updatedBY;
incRec3.sys_updated_on = new GlideDateTime();
incRec3.sys_updated_by = updatedBY;
incRec3.closed_at = new GlideDateTime();
incRec3.closed_by = updatedBY;
close3: function(sdate,callerID,updatedBY){
var incRec4 = this.incCreation(jsonObj,sdate, callerID,updatedBY, 'close3');
gs.print('IncidentRec4 ' + incRec4);
var cc = Math.floor((Math.random() * this.close_code.length));
var cc1 = this.close_code[cc];
var cn = Math.floor((Math.random() * this.close_notes.length));
var cn1 = this.close_notes[cn];
incRec4.opened_at = sdate;
incRec4.sys_created_on = sdate;
incRec4.close_code = jsonObj["close_code"] || cc1;
incRec4.close_notes = jsonObj["close_notes"] || cn1;
incRec4.resolved_at = new GlideDateTime();
incRec4.resolved_by = updatedBY;
incRec4.sys_updated_on = new GlideDateTime();
incRec4.sys_updated_by = updatedBY;
incRec4.closed_at = new GlideDateTime();
incRec4.closed_by = updatedBY;
close10: function(sdate,callerID,updatedBY){
var incRec5 = this.incCreation(jsonObj,sdate, callerID,updatedBY,'close10');
gs.print('IncidentRec5 ' + incRec5);
var cc = Math.floor((Math.random() * this.close_code.length));
var cc1 = this.close_code[cc];
var cn = Math.floor((Math.random() * this.close_notes.length));
var cn1 = this.close_notes[cn];
incRec5.opened_at = sdate;
incRec5.sys_created_on = sdate;
incRec5.close_code = jsonObj["close_code"] || cc1;
incRec5.close_notes = jsonObj["close_notes"] || cn1;
incRec5.resolved_at = new GlideDateTime();
incRec5.resolved_by = updatedBY;
incRec5.sys_updated_on = new GlideDateTime();
incRec5.sys_updated_by = updatedBY;
incRec5.closed_at = new GlideDateTime();
incRec5.closed_by = updatedBY;
other: function(sdate,callerID,updatedBY){
var incRec = this.incCreation(jsonObj,sdate, callerID,updatedBY,'other');
incClosure : function(incSysID){
var inc = new GlideRecord('incident');
inc.addQuery('sys_id',incSysID);
var cc = Math.floor((Math.random() * this.close_code.length));
var cc1 = this.close_code[cc];
inc.close_code = jsonObj["close_code"] || cc1;
var cn = Math.floor((Math.random() * this.close_notes.length));
var cn1 = this.close_notes[cn];
inc.close_notes = jsonObj["close_notes"] || cn1;
inc.resolved_at = new GlideDateTime();
inc.sys_updated_on = new GlideDateTime();
triggerSurvey : function(surveyID,callerID)
var grSurvey = new GlideRecord("asmt_metric_type");
if (grSurvey.get(surveyID)) {
if (grSurvey.schedule_type == 'on_demand' &&
grSurvey.evaluation_method == 'survey' &&
grSurvey.publish_state != "draft" &&
new AssessmentUtils().hasSurveyQuestions(grSurvey)) {
new SNC.AssessmentCreation().createAssessments(surveyID, '', callerID);
var incSurvey = new GlideRecord('incident');
incSurvey.addQuery('caller_id',callerID);
//var rec = incSurvey.sys_id;
var ticRec = new GlideRecord('u_tickets');
ticRec.addEncodedQuery('u_typeINclose2,close3,close10');
ticRec.addQuery('u_caller',callerID);
ticRec.u_survey_triggered = true;
incUpdate : function(incSysID){
var inc = new GlideRecord('incident');
inc.addQuery('sys_id',incSysID);
Calling Script Include through script:
var jsonOBJ =[{"business_service":"57ed1b00dbc7ff408606f2131f96194f","cmdb_ci" : "0aa9e40d3790200044e0bfc8bcbe5df2","service_offering":"5cf56f8cdbc7ff408606f2131f961918","category":"","impact":"","urgency":"","assignment_group":"d625dccec0a8016700a222a0f7900d06","assigned_to": "","short_description" : ["Users are not able to access the application","The HDMI port on my PC stopped working","Application is down"],"description": ["Users are not able to access the application","The HDMI port on my PC stopped working","Application is down"],"contact_type": "phone","close_notes" : "","close_code":""},{"business_service":"57ed1b00dbc7ff408606f2131f96194f","cmdb_ci" : "0aa9e40d3790200044e0bfc8bcbe5df2","service_offering":"5cf56f8cdbc7ff408606f2131f961918","category":"","impact":"","urgency":"","assignment_group":"d625dccec0a8016700a222a0f7900d06","assigned_to": "","short_description" : ["Users are not able to access the application","The HDMI port on my PC stopped working","Server is down"],"description": ["Users are not able to access the application","The HDMI port on my PC stopped working","Server is down"],"contact_type": "phone","close_notes" : "","close_code":""},{"business_service":"57ed1b00dbc7ff408606f2131f96194f","cmdb_ci" : "0aa9e40d3790200044e0bfc8bcbe5df2","service_offering":"5cf56f8cdbc7ff408606f2131f961918","category":"","impact":"","urgency":"","assignment_group":"d625dccec0a8016700a222a0f7900d06","assigned_to": "","short_description" : ["Users are not able to access the application","The HDMI port on my PC stopped working","App is not working"],"description": ["Users are not able to access the application","The HDMI port on my PC stopped working","App is not working"],"contact_type": "phone","close_notes" : "","close_code":""}];var json = JSON.stringify(jsonOBJ);//gs.print(json);var jsonObj = jsonOBJ[Math.floor(Math.random() * jsonOBJ.length)];gs.print('Random JSON -- ' + JSON.stringify(jsonObj));var incr=0;var tickets = 20;var reassign1f = 0.1;var reassign2f = 0.06;var close2f = 0.1;var close3f = 0.05;var close10f = 0.02;var breachf = 0.04;var reopenf = 0.01;var inccount = tickets+Math.floor((Math.random() * 30) + 1);var days = 91;
var interval = 5;
var surveyID = '87186844d7211100158ba6859e610378';var createdOn = new global.DataGenerator().calcStartDate(incr);var createdBy = new global.DataGenerator().getCaller();var updatedBy = new global.DataGenerator().getCaller();var openedAt = new global.DataGenerator().calcStartDate(incr);var updatedOn = new global.DataGenerator().calcStartDate(incr);var type = ['reassign1','reassign2','close2','close3','close10','breach', 'reopen', 'other'];var type1 = Math.floor((Math.random() * type.length));var type2 = type[type1];var test = new global.DataGenerator();test.calcIter();// }
// }
https://www.servicenow.com/community/developer-articles/utility-script-for-incident-creation-periodically/ta-p/2485034
