Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Global function call for each widget in Service Portal

Deepika Mishra
Mega Guru

Hi All,

Scenario:
I have few widgets where in each widget it does multiplication of two numbers. So in each widget I call a function where it does the multiplication part. These individual widgets have some other functionality as well along with this multiplication.

So, I want to understand is there any way I can have this  "multiplication of two numbers" function in a place (like a global function out of all widgets) defined and call that particular part when required.

Note: I need different widgets which have a common function for multiplication. So, is there any way to only have the function define at a separate place and call when required inside widget.

1 ACCEPTED SOLUTION

Deepika Mishra
Mega Guru

I figured out a way to resolve this issue:

Step 1: Go To Widget Angular Provider

Type: Service

Name: testService

Client Script:

function testService() {
	
   
    this.showResult = function() {
        return "Hi";
    };
}

 

Step 2: Save

Step 3: Open widget where this Angular Provider needs to be called.

Step 4: Go to the Related List of the widget and add the service under "Angular Provider"

Step 5: Add below code in Client Controller of that Widget

api.controller = function($scope,testService) {
    /* widget controller */
    var c = this;
	
	$scope.serviceCall = testService.showResult();
	console.log("$scope.serviceCall: "+$scope.serviceCall);
};

View solution in original post

13 REPLIES 13

Sebastian L
Mega Sage

Hi,

You could also just create it in a script include, and then call that on all your widgets. 

 

var result = new nameOfScriptInclude().functionInScriptInclude();

Best regards,
Sebastian Laursen

Hi,

The function which I want to invoke globally has HTML element and attribute changes.
Will adding SI will help in such scenario ?

Deepika Mishra
Mega Guru

I figured out a way to resolve this issue:

Step 1: Go To Widget Angular Provider

Type: Service

Name: testService

Client Script:

function testService() {
	
   
    this.showResult = function() {
        return "Hi";
    };
}

 

Step 2: Save

Step 3: Open widget where this Angular Provider needs to be called.

Step 4: Go to the Related List of the widget and add the service under "Angular Provider"

Step 5: Add below code in Client Controller of that Widget

api.controller = function($scope,testService) {
    /* widget controller */
    var c = this;
	
	$scope.serviceCall = testService.showResult();
	console.log("$scope.serviceCall: "+$scope.serviceCall);
};

Hi @Deepika Mishra & @Thameem Ansari , I want to have same kind of functionality but I want to call this function in server script of every widget instead of calling in client script.

Can you please tell me how can I do that?