how to search incidents in custom search widget?

Mr_ D_
Tera Contributor

We have this custom widget that searches KB articles. There are no issues with this. The SAYT feature works great. They would also like to incorporate incidents with assignment_group LOS Admin that belong to this custom portal page.

I'm not familiar with how the ServiceNow code works. Can anyone guide or help me with the script and how to append/incorporate it all?

--

Body HTML Template

<h1 class="text-center text-1x m-b-lg sp-tagline-color" ng-bind="options.title"></h1>
<!-- <form method="get" action="?"> -->
  <input type="hidden" name="id" value="carlos_search"/>
  <input type="hidden" name="t" value="{{data.searchType}}"/>
  <div class="input-group input-group-{{::c.options.size}}">
    <!-- uses ui.bootstrap.typeahead -->
    <input name="q" type="text" placeholder="Enter System Rule/Error or Issue Lookup" ng-model="c.selectedState"
           ng-model-options="{debounce: 250}" autocomplete="off"
           uib-typeahead="item as item.label for item in c.getResults($viewValue)"
           typeahead-focus-first="false"
           typeahead-on-select="c.onSelect($item, $model, $label)"           
          typeahead-template-url="customTemplate.html" class="form-control input-typeahead">  
    <span class="input-group-btn">
      <button name="search" class="btn btn-{{::c.options.color}}" data-toggle="modal" data-target="#losSearchPage" ng-click="c.onSubmit()" >
    	<i ng-if="::c.options.glyph" class="fa fa-{{::c.options.glyph}}"></i>
      </button>
    </span>
  </div>
  
 
   <!-- Modal -->
 <div class="modal fade" id="losSubmitTicket" role="dialog">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">{{::c.carlosRuleTitle}}</h4>
        </div>
        <div class="modal-body">
          <sp-widget widget="c.losSubmitTicket"></sp-widget>
        </div>
        
      </div>
    </div>
  </div>
  

   <!-- Modal -->
 <div class="modal fade" id="losSearchPage" role="dialog">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">{{::c.carlosSearchPageTitle}}</h4>
        </div>
        <div class="modal-body">
          <sp-widget widget="c.losSearchPage"></sp-widget>
        </div>
        
      </div>
    </div>
  </div>

<!-- </form> -->
<br />

<script>
  <a>
      <span ng-bind-html="match.label | uibTypeaheadHighlight:query"></span>
  </a>
</script>

Server Script

(function() {
	data.searchType = $sp.getParameter("t");
	data.results = [];
    data.searchMsg = " ";  // gs.getMessage("Search");
	data.limit = options.limit || 15;
	var textQuery = '123TEXTQUERY321';

/*	if (!input)
		return;
*/
	
	data.q = input.q;
	getKnowledge();
	
	gs.flushMessages();

	
	function getKnowledge() {
		var kb = new GlideRecord('kb_knowledge');
		kb.addEncodedQuery( 'kb_knowledge_base=36f1c6521b8b60104a2ded37b04bcb20^kb_category=402e4394dbfbcf00baeef456bf96195e');
		kb.addQuery('workflow_state', 'published');
		//kb.addQuery('valid_to', '>=', (new GlideDate()).getLocalDate().getValue());
		kb.addQuery(textQuery, data.q);
		//kb.addQuery('kb_knowledge_base', $sp.getValue('kb_knowledge_base'));
		kb.query();
		var kbCount = 0;
		while (kb.next() && kbCount < data.limit) {
			if (!$sp.canReadRecord(kb))
				continue;

			var article = {};
			article.type = "kb";
			$sp.getRecordDisplayValues(article, kb, 'sys_id,number,short_description,published,text');
			if (!article.text)
				article.text = "";
			//article.text = $sp.stripHTML(article.text);
			article.text = article.text.substring(0,200);
			//article.score = parseInt(kb.ir_query_score.getDisplayValue());
			article.label = article.short_description + "";
			article.escalation_group = kb.u_knowledge_owner + "";
			data.results.push(article);
			kbCount++;
		}
	}

})();

Client Controller

function ($filter, $location, $uibModal, spModal, $scope, $rootScope, spUtil) {
	var c = this;
	c.options.glyph = c.options.glyph || 'search';
	c.options.title = c.options.title || c.data.searchMsg;

	c.onSelect = function($item, $model, $label){

		c.losRuleTitle = $item.label;
		$('#losSubmitTicket').find('.modal-title').text($item.label);
		
		spUtil.get("los-submit-ticket", {sys_id: $item.sys_id}).then(function(response) {
			c.losSubmitTicket = response;
		});
		
		$("#losSubmitTicket").modal("show");
		c.selectedState = "";

	};
	
	c.getResults = function(query) {
		return c.server.get({q: query}).then(function(response) {
			var a = $filter('orderBy')(response.data.results, '-score');
			return $filter('limitTo')(a, c.data.limit);
		});
	}
	
	$("#losSubmitTicket").on('hidden.bs.modal', function () {
	     c.submitted = 'false';
    });
	
	c.onSubmit = function(){

		c.losSearchPageTitle = "Search Results";
		$('#losSearchPage').find('.modal-title').text("Search Results");
		
		spUtil.get("los-search-page", {q: c.selectedState}).then(function(response) {
			c.losSearchPage = response;
		});
		
		//$("#losSearchPage").modal("show");
		c.selectedState = "";

	};
	
	$scope.$on('updateSubmitTicketTitle', function(event,obj) {
		
		$('#losSubmitTicket').find('.modal-title').text(obj.title);
    });
	
	$scope.$on('updateSubmitPageTitle', function(event,obj) {
		
		$('#losSearchPage').find('.modal-title').text(obj.title);
    });	
	$scope.$on('autoCloseModal', function(event,obj) {
		
		$('#losSubmitTicket').modal("hide");
		$('#losSearchPage').modal("hide");
    });	
	
}	
1 REPLY 1

Swarup Patra
Kilo Guru

Sure, I can guide you on how to incorporate incidents with assignment_group LOS Admin into your custom widget. Here's a high-level overview of the steps you'll need to take:

1. **Create a Server Script**: In your custom widget, you'll need to create a server script that fetches incidents with the assignment_group LOS Admin. You can use GlideRecord to query the incident table.

Sample Code:

var gr = new GlideRecord('incident');
gr.addQuery('assignment_group.name', 'LOS Admin');
gr.query();
while(gr.next()) {
// your code here
}

2. **Create a Client Script**: In your client script, you'll need to call the server script using the server.get() or server.update() method.

Sample Code:

c.server.get().then(function(response) {
c.data.incidents = response.data.incidents;
});

3. **Update HTML Template**: In your HTML template, you'll need to update it to display the incidents data. You can use AngularJS directives like ng-repeat to loop through the incidents data.

Sample Code:


{{incident.number}} - {{incident.short_description}}

 


4. **Test Your Widget**: Finally, you'll need to test your widget to make sure it's working as expected. You can do this by adding the widget to a portal page and checking if it displays the incidents correctly.

Remember, this is a high-level overview. Depending on your specific requirements, you might need to modify the code or take additional steps.


nowKB.com
If you want to know any information about Service Now . Visit to https://nowkb.com/home