How to call a script include function from Business rule

PRASHANTHI SATH
Tera Contributor

Dear,

 

I've written a script include and I wanted to call a function from that creates a record in another application from ServiceNow.

I've configured the business rule as,

PRASHANTHISATH_0-1739434429939.png

PRASHANTHISATH_1-1739434492514.png

 Can you please tell what is the mistake here.

 

Regards,

Prashanthi Sathri

 

1 ACCEPTED SOLUTION

maheshkhatal
Mega Sage

@PRASHANTHI SATH ,  You can execute the script include in the following manner,

If your script include looks like the one given below:

var TestDemoFunc = Class.create();
TestDemoFunc.prototype = {
    initialize: function() {
    },
	returnSomething:function(sysId){
		gs.info('Inside Script Include');
		//process some logic using sysID
		return 'Hello';
	},
	executeSomething:function(){
		gs.info('Execute Something');
	},

    type: 'TestDemoFunc'
};

 

Then go the Business rule and click on Advanced tab, from there you can invoke the script include as shown below:

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

	// Add your code here
	//Exeute the script include and store the result
	var storedValue = new global.TestDemoFunc().returnSomething(current.sys_id);
	gs.info('Stored Value is:'+storedValue);
	//Just execute the script include
	new global.TestDemoFunc().executeSomething();
})(current, previous);

You can take the help of the current object and pass it to your function in the script include. Attaching the screenshots for your reference. sc_demo.JPGBR_demo.JPG

Kindly mark my response as helpful if it helped you and let me know if you have any questions further.

Thanks,

Mahesh.

View solution in original post

6 REPLIES 6

BillMartin
Mega Sage

Hi @PRASHANTHI SATH ,

 

I have created a comprehensive lesson on Business Rules and Script Includes, designed to help you gain a deeper understanding of how to use them effectively. This lesson will enhance your ability to write high-quality, maintainable scripts by applying best practices in code quality and optimization.

 

Hope this helps.

 

Complete Training Course on ServiceNow Business Rules for Developers, Admins and IT Professionals

 

Mastering GlideAjax and Script Includes: A Guide for ServiceNow Developers

 

Leveraging Software Design Patterns with extendObject() in ServiceNow Script Include for Developers

 

Master Good Software Architecture with Object.extendsObject() | A Guide for ServiceNow Developers

 

Object-Oriented Principles in ServiceNow | A Guide for Architects & Developers

maheshkhatal
Mega Sage

@PRASHANTHI SATH ,  You can execute the script include in the following manner,

If your script include looks like the one given below:

var TestDemoFunc = Class.create();
TestDemoFunc.prototype = {
    initialize: function() {
    },
	returnSomething:function(sysId){
		gs.info('Inside Script Include');
		//process some logic using sysID
		return 'Hello';
	},
	executeSomething:function(){
		gs.info('Execute Something');
	},

    type: 'TestDemoFunc'
};

 

Then go the Business rule and click on Advanced tab, from there you can invoke the script include as shown below:

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

	// Add your code here
	//Exeute the script include and store the result
	var storedValue = new global.TestDemoFunc().returnSomething(current.sys_id);
	gs.info('Stored Value is:'+storedValue);
	//Just execute the script include
	new global.TestDemoFunc().executeSomething();
})(current, previous);

You can take the help of the current object and pass it to your function in the script include. Attaching the screenshots for your reference. sc_demo.JPGBR_demo.JPG

Kindly mark my response as helpful if it helped you and let me know if you have any questions further.

Thanks,

Mahesh.