Classless Script Include and use it to create Incident whenever any new issue is created in the system

sivaiah
Kilo Contributor

Classless Script Include and use it to create Incident whenever any new issue is created in the system

3 REPLIES 3

Dan H
Tera Guru

Hi,

I have tested this in my Instance and it is working.

 

Steps:

1. Create a Script Include where the name and function name are the same.

2. Create your business rule and your conditions.

 

In my example, I have a update/insert rule on Issue table, so when an Issue record is updated/inserted an incident is created by the classless script include.

See below:

Script include: look at Name and function Name. These have to be the same.

find_real_file.png

 

Business rule: call the function.

find_real_file.png

find_real_file.png

 

Then I created a new issue and a new incident was created.

find_real_file.png

 

So all you need to do is copy these screenshots and add any more fields to the incident that you want.

 

Scripts:

Script include -

function createIncidentNow(callerId, shortDescription){ //add any other fields here
	gs.log('creating incident');
	var newIncident = new GlideRecord('incident');
	newIncident.newRecord(); //set any other fields you pass in below
	newIncident.caller_id = callerId;
	newIncident.short_description = shortDescription;
	newIncident.insert();
}

 

Business rule:

(function executeRule(current, previous /*null when async*/) {

	gs.log('business rule triggered');
	createIncidentNow('c6445481eb310100fcfb858ad106fef1', 'short description text');

})(current, previous);

 

Please mark my answer as Correct/Helpful based on impact

Regards,

Dan H

shloke04
Kilo Patron

Hi @sivaiah 

Couple of points to note here to achieve your solution:

1) You can write a Classless Script Include but the problem with classless script include is since the Name of Script Include and Method name are always same there can be scenario in future where this can contradict with any other classless script include created in future with same method name getting called in other areas like Business Rule etc.

My suggestion here would be to go with a class type script include only:

1) Please create a Script Include as shown below:

var createIncident = Class.create();
createIncident.prototype = {
    initialize: function() {
    },
	
	createINC : function(current){
		var gr = new GlideRecord('incident');
		gr.initialize();
		gr.short_description = current.description;
		gr.FIELD_Name = current.fielname;
		gr.insert();
	},

    type: 'createIncident'
};

find_real_file.png

Now create a After Insert Business Rule on Issue Table and just call the Script Include defined above as shown below:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	new createIncident().createINC(current);

})(current, previous);

BR Screenshot:

find_real_file.png

This will work for your scenario and will be my recommended method as well.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

shloke04
Kilo Patron

Hi @sivaiah 

Also would like to share the knowledge on Classless based script include since you mentioned it, here is the approach below:

1) Create a Script Include and make sure the Script Include Name and Method Name in script is same.

Script:

function createIncident(current) {
    var gr = new GlideRecord('incident');
    gr.initialize();
    gr.short_description = current.description;
    gr.FIELD_Name = current.fielname;
    gr.insert();
}

Screenshot below for reference:

find_real_file.png

Now just write a After Insert Business Rule on Issue Table and call the Classless script include as below:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	createIncident(current);

})(current, previous);

Screenshot for reference:

find_real_file.png

Please refer the below doc link to learn more on classless script include:

https://developer.servicenow.com/dev.do#!/learn/learning-plans/quebec/new_to_servicenow/app_store_learnv2_scripting_quebec_on_demand_script_include

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke