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

I tried the same way but getting below error:

$rootScope.add is not a function

Please share the screenshot of client script of both widgets where you defined the funtion and calling it. so that i can help you debug

 

Widget 1: In first step

find_real_file.png

 

Widget 2: In Second step

api.controller = function($scope,$rootScope) {

alert($rootScope.callFunction(10,20));

};

 

Error: 

$rootScope.callFunction is not a function

 

on loading Widget 2

 

 

 

on the second widget. please call it on demand. it will become issue if the first widget took time to load.

so call that function from when clicking a button that time it will surely work

 

<button ng-click="c.callAdd()"> Click Me <button>

 

c.callAdd=function(){

here callthe rootscope funtion

}

 

Still getting same error on adding it inside button click:

 $("#buttonAbc").click(function() {

			$rootScope.callFunction(10,20);
});

 

Error:

$rootScope.callFunction is not a function