Kriti Gupta2
ServiceNow Employee
ServiceNow Employee

Script Include:

var DataGenerator = Class.create();
DataGenerator.prototype = {
initialize: function() {

 
//Global Variables
var mem = [];
var incSysID;
var incr =0;
var days = 91;
var interval = 5;
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 inc_start_date;
var gdt;
var type = ['reassign1','reassign2','close2','close3','close10','breach', 'reopen', 'other'];
var type1 = Math.floor((Math.random() * type.length));
var type2 = type[type1];
this.impact = [1,2,3];
this.urgency = [1,2,3];
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'];
 
//payload/json
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","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","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":""
}];
var json = JSON.stringify(jsonOBJ);
var jsonObj = jsonOBJ[Math.floor(Math.random() * jsonOBJ.length)];

//Group/Group members
var group = jsonObj["assignment_group"];
var grMember = new GlideRecord('sys_user_grmember');
grMember.addEncodedQuery('group=' + group);
grMember.query();
while (grMember.next()) {
mem.push(grMember.user.toString());
}
this.assigned_to = mem;

//sys fields
var createdBy = this.getCaller();
var updatedBy = this.getCaller();

//Incidents Count
var inccount = tickets+Math.floor((Math.random() * 30) + 1);
 
//Survey ID
var surveyID = '87186844d7211100158ba6859e610378';
},


 
//Incident Create
incCreation :function(jsonObj,createdOn, createdBy, updatedBy, type){
gs.print('JSON-- ' + jsonObj);
var inc = new GlideRecord('incident');
inc.initialize();
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;
inc.setWorkflow(false);
inc.insert();
// var assn1 = Math.floor((Math.random() * this.assigned_to.length));
// inc.assigned_to = this.assigned_to[assn1];
inc.state = 2;
//inc.incident_state = 2;
inc.update();
var tic = new GlideRecord('u_tickets');
tic.initialize();
tic.u_task_type = inc.sys_id;
tic.u_status = 'initiated';
tic.u_type = type;
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;
tic.setWorkflow(false);
tic.insert();
incSysID = inc;
return incSysID;
},


//Get Caller
getCaller : function(){
var user = [];
var temp = new GlideRecord('sys_user');
temp.addEncodedQuery('active=true^email!=NULL');
temp.query();
while (temp.next()) {
user.push(temp.sys_id.toString());
}
var caller = Math.floor((Math.random() * user.length));
var userID = user[caller];
return userID;
},
 
//Incident Creation Date
calcStartDate : function(incr){
inc_start_date = days - (interval*incr);
gdt = new GlideDateTime();
gdt.addDays(-1 * inc_start_date);
return gdt;
},

//Calculation Types
calcTypes : function(inccount){
gs.print("inside"+inccount);
gs.print(Math.floor(inccount*reassign1f));
var res={};
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);
return res;
},

//calculate iterations
calcIter : function(){
var iter = (days/interval)-1;
for(var i=0;i<iter;i++){
var sdate = this.calcStartDate(i);
gs.print(sdate);
var robj = this.calcTypes(inccount);
gs.print(JSON.stringify(robj));

//Iteration
//reassign1
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);
}
//reassign2
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);
}
//close2
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);
//close3
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);
//close10
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);
//other
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.state = 2;
incRec.opened_at = sdate;
incRec.sys_created_on = sdate;
incRec.sys_updated_on = new GlideDateTime();
incRec.sys_updated_by = updatedBY;
incRec.update();
},

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.state = 2;
incRec2.opened_at = sdate;
incRec2.sys_created_on = sdate;
incRec2.sys_updated_on = new GlideDateTime();
incRec2.sys_updated_by = updatedBY;
incRec2.update();
},

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.state = 7;
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;
incRec3.update();
},

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.state = 7;
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;
incRec4.update();
},

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.state = 7;
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;
incRec5.update();
},

other: function(sdate,callerID,updatedBY){
var incRec = this.incCreation(jsonObj,sdate, callerID,updatedBY,'other');
},
 
//Incident Closure
incClosure : function(incSysID){
var inc = new GlideRecord('incident');
inc.addQuery('sys_id',incSysID);
inc.query();
if(inc.next())
{
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();
inc.state = 7;
inc.update();
}
},
 
//Survey trigger
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);
incSurvey.query();
while(incSurvey.next())
{
//var rec = incSurvey.sys_id;
var ticRec = new GlideRecord('u_tickets');
ticRec.addEncodedQuery('u_typeINclose2,close3,close10');
ticRec.addQuery('u_caller',callerID);
ticRec.setLimit(1);
ticRec.query();
while(ticRec.next())
{
ticRec.u_survey_triggered = true;
ticRec.update();
}
}
}
}
},

/*
incUpdate : function(incSysID){
var inc = new GlideRecord('incident');
inc.addQuery('sys_id',incSysID);
inc.query();
if(inc.next())
{
inc.state = 2;
inc.update();
}
},*/
 
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();
// }
// }

 

type: 'DataGenerator'
};
Comments
Allen Andreas
Administrator
Administrator

Hello,

Thank you for writing this article. Perhaps you can give us some sort of use case and breakdown of what this is about and why people would find it helpful/useful?

 

Also, it's a lot of code and would look and be read better on these forums by using the appropriate forum feature "Insert/Edit code sample", which can be access by clicking the three dots:

AllenAndreas_0-1677008270138.png

 

and then clicking here:

AllenAndreas_1-1677008285842.png

 

Version history
Last update:
‎02-21-2023 11:11 AM
Updated by:
Contributors