Verification in widget

Hafsa1
Mega Sage

I have a custom widget where I'm asking for input from user table. As soon as agent input user id, tool should cross check, if any active incident is raised with CI=Standard Laptop for this userid or not. If active incident exist then it should not allow to proceed further and make this input field blank/clear.

I'm new in portal script, can anyone help here.

1 ACCEPTED SOLUTION

Kit Cheong
Giga Guru

HTML Template:

<sn-record-picker field="user" 
                  table="'sys_user'" 
                  display-field="'name'" 
                  value-field="'sys_id'" 
                  search-fields="'name'" 
                  page-size="100"></sn-record-picker>

 

Client Script:

api.controller=function($scope, spUtil) {
	/* widget controller */
	var c = this;

	
	$scope.user = {displayValue: "",
								 value: "",
								 name: 'user'};


	$scope.$on("field.change", function(evt, parms) {

		if (parms.field.name == 'user') {
			c.server.get({"action":"verify", //action is only required if you will be doing additional different call backs
										"id":$scope.user.value}).then(function(_response){

				if (_response.data.actIncExists) {
					$scope.user.displayValue = "";
					$scope.user.value = "";
					spUtil.addInfoMessage("Incident exists")
				}

			})
		}

	});


};

 

Server Script:

(function() {
	var stdLapId = 'b4fd7c8437201000deeabfc8bcbe5dc1'; //move this to a script include used to hold constants.
	
	if (input && input.action == 'verify') {
		var grsInc = new GlideRecordSecure('incident');
		grsInc.addQuery('caller_id', input.id);
		grsInc.addActiveQuery();
		grsInc.addQuery('cmdb_ci', stdLapId);
		grsInc.query();
		data.actIncExists = grsInc.hasNext() || false;
	}

})();

 

 

More Info:

https://developer.servicenow.com/dev.do#!/reference/api/utah/client/spUtilAPI#SPU-addInfoMessage_S?n... 

https://www.servicenow.com/community/developer-blog/reference-fields-with-the-snrecordpicker-directi... 

View solution in original post

3 REPLIES 3

Kit Cheong
Giga Guru

HTML Template:

<sn-record-picker field="user" 
                  table="'sys_user'" 
                  display-field="'name'" 
                  value-field="'sys_id'" 
                  search-fields="'name'" 
                  page-size="100"></sn-record-picker>

 

Client Script:

api.controller=function($scope, spUtil) {
	/* widget controller */
	var c = this;

	
	$scope.user = {displayValue: "",
								 value: "",
								 name: 'user'};


	$scope.$on("field.change", function(evt, parms) {

		if (parms.field.name == 'user') {
			c.server.get({"action":"verify", //action is only required if you will be doing additional different call backs
										"id":$scope.user.value}).then(function(_response){

				if (_response.data.actIncExists) {
					$scope.user.displayValue = "";
					$scope.user.value = "";
					spUtil.addInfoMessage("Incident exists")
				}

			})
		}

	});


};

 

Server Script:

(function() {
	var stdLapId = 'b4fd7c8437201000deeabfc8bcbe5dc1'; //move this to a script include used to hold constants.
	
	if (input && input.action == 'verify') {
		var grsInc = new GlideRecordSecure('incident');
		grsInc.addQuery('caller_id', input.id);
		grsInc.addActiveQuery();
		grsInc.addQuery('cmdb_ci', stdLapId);
		grsInc.query();
		data.actIncExists = grsInc.hasNext() || false;
	}

})();

 

 

More Info:

https://developer.servicenow.com/dev.do#!/reference/api/utah/client/spUtilAPI#SPU-addInfoMessage_S?n... 

https://www.servicenow.com/community/developer-blog/reference-fields-with-the-snrecordpicker-directi... 

If I want to show user name and his email in alert what change I need to make?

On the server side, open the incident glide record and store the caller_id's email on data object.

On the client side add the email string directly into popup by referencing the email from the _response.data object.