Data generator

Miroslav5
Tera Contributor

Hello, for testing purpose I need to create a class which will generates incidents. Parameters: amount of incidents, data captured from already existing incidents, groups and users. 

Thanks for help guys M.

5 REPLIES 5

Allen Andreas
Administrator
Administrator

Hi,

We can't really help you with the information you've supplied thus far.

Perhaps you can give us a bit more information to include what you mean by "create a class", etc.

And then let us know what you've tried, what has/hasn't worked...otherwise, you've just posted requirements/a statement without giving us any information on your efforts.

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi, just write script with easy function, make while cycle for generate data by this requiements. Thanks

raphaelcrv
Kilo Guru

sure, I can help you create a ServiceNow Script Include that generates incidents with the specified parameters. Here's an example script include that you can use as a starting point:

 

 

var IncidentGenerator = Class.create();
IncidentGenerator.prototype = {
  initialize: function(num_incidents, incidents_data, groups, users) {
    this.num_incidents = num_incidents;
    this.incidents_data = incidents_data;
    this.groups = groups;
    this.users = users;
  },
  
  generateIncidents: function() {
    var gr = new GlideRecord('incident');
    var count = 0;
    
    for (var i = 0; i < this.num_incidents; i++) {
      var incident_data = this.incidents_data[Math.floor(Math.random() * this.incidents_data.length)];
      var short_description = incident_data.short_description;
      var description = incident_data.description;
      var group = this.groups[Math.floor(Math.random() * this.groups.length)];
      var assigned_to = this.users[Math.floor(Math.random() * this.users.length)];
      
      gr.initialize();
      gr.short_description = short_description;
      gr.description = description;
      gr.assignment_group.setDisplayValue(group);
      gr.assigned_to.setDisplayValue(assigned_to);
      
      try {
        gr.insert();
        count++;
        gs.log('Incident ' + count + ' created successfully.');
      } catch (ex) {
        gs.log('Error creating incident: ' + ex);
      }
    }
  },
  
  type: 'IncidentGenerator'
};

 

 

 

To use this script include, create a new script include record in ServiceNow and paste the above code into the script field. Then, create a new script record and call the generateIncidents function on an instance of the IncidentGenerator class, passing in the appropriate parameters:

 

var num_incidents = 10;
var incidents_data = [{short_description: 'Incident 1', description: 'This is incident 1'}, {short_description: 'Incident 2', description: 'This is incident 2'}];
var groups = ['Group 1', 'Group 2'];
var users = ['User 1', 'User 2'];

var generator = new IncidentGenerator(num_incidents, incidents_data, groups, users);
generator.generateIncidents();

 


This will create 10 new incidents on the incident table in ServiceNow using the specified data, groups, and users.

 

If my response was helpful and/or provided a solution, please consider marking it as such. Thank you!

Hi,

Thanks for trying to help!

It may be best to try and get more information from the author and guide them versus using something like ChatGPT to offer scripts. It's definitely beneficial, but even that script is unnecessarily complex and someone who is asking for assistance and doesn't seem to know what to do, that may confuse them? In any case, they have an example now, so maybe it has helped 🙂

 

Also, nice signature, haha...


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!