Scripted search sources

Ak8977
Tera Expert

Hello all,
I would like to search the approvals records from the portal with ritm number. For that I am using Scripted Search Sources. But it was not working. Could anyone help me on this. I written the following script -

Data Fetch Script -

(function(query) {
  var results = [];
  /* Calculate your results here. */
  var gr= new GlideRecord("sysapproval_approver");
    gr.addQuery('IR_AND_OR_QUERY', query);
    gr.orderBy('ir_query_score');
    gr.query();
    while (gr.next()) {
        var item = {};
        item.primary = gr.getDisplayValue('name');
        item.number = gr.getDisplayValue('number');
        item.sys_id = gr.getValue('sys_id');
        item.score = gr.getValue('ir_query_score');
        results.push(item);
    }  
  return results;
})(query);
 
Facet Generation Script -
(function(query, facetService, searchResults) {
    /* Calculate your facets here using facetService */
    /* var stateFacet = facetService.createFacet('State', 'state'); */
    /* stateFacet.addFacetItem('Facet Item Label', '123'); */

})(query, facetService, searchResults);
1 ACCEPTED SOLUTION

Bhavya11
Kilo Patron

Hi @Ak8977 ,

 

i have tired something like below for my requirement, here if am searching with number or short description it will show the results for me

 

Data fetch script 

 

(function(query) {
	var results = [];
  /* Calculate your results here. */
    var grApprovals=new GlideRecord('sysapproval_approver');
    grApprovals.addQuery('state', 'requested');
    //grApprovals.addQuery('approver', gs.getUserID());
    var sQuery="sysapproval.short_descriptionLIKE" + query +  "^ORsysapproval.numberLIKE" + query;
    grApprovals.addQuery(sQuery);
    grApprovals.query();
    while (grApprovals.next()){
        var myapproval = {};
        myapproval.table='sysapproval_approver';
        myapproval.sys_id=grApprovals.getValue("sys_id");
        myapproval.number=grApprovals.sysapproval.number+"";
        myapproval.short_description=grApprovals.sysapproval.short_description+"";
        myapproval.sys_created_on=grApprovals.getValue('sys_created_on');
        results.push(myapproval);
    }
  
  return results;
})(query);

 

  search page template

 

<div>
  <a href="?id=approval&table={{item.table}}&sys_id={{item.sys_id}}" class="h4 text-primary m-b-sm block">Approval for: {{item.number}}
    <span ng-bind-html="highlight(item.primary, data.q)"></span>
  </a>
 <span class="text-muted">
    {{item.short_description}}
 </span>
  <span class="m-l-xs m-r-xs"> &middot; </span>
  <span class="text-muted">
  {{item.sys_created_on}}
    </span>
</div>

 

 

 

Output:

Bhavya11_0-1722833975454.pngBhavya11_1-1722834028384.png

 

 

 

 

in your script try to change query then and search page template it will work fine.

 

 

Please mark helpful & correct answer if it's really worthy for you.

 

 

Thanks,

BK

 

View solution in original post

1 REPLY 1

Bhavya11
Kilo Patron

Hi @Ak8977 ,

 

i have tired something like below for my requirement, here if am searching with number or short description it will show the results for me

 

Data fetch script 

 

(function(query) {
	var results = [];
  /* Calculate your results here. */
    var grApprovals=new GlideRecord('sysapproval_approver');
    grApprovals.addQuery('state', 'requested');
    //grApprovals.addQuery('approver', gs.getUserID());
    var sQuery="sysapproval.short_descriptionLIKE" + query +  "^ORsysapproval.numberLIKE" + query;
    grApprovals.addQuery(sQuery);
    grApprovals.query();
    while (grApprovals.next()){
        var myapproval = {};
        myapproval.table='sysapproval_approver';
        myapproval.sys_id=grApprovals.getValue("sys_id");
        myapproval.number=grApprovals.sysapproval.number+"";
        myapproval.short_description=grApprovals.sysapproval.short_description+"";
        myapproval.sys_created_on=grApprovals.getValue('sys_created_on');
        results.push(myapproval);
    }
  
  return results;
})(query);

 

  search page template

 

<div>
  <a href="?id=approval&table={{item.table}}&sys_id={{item.sys_id}}" class="h4 text-primary m-b-sm block">Approval for: {{item.number}}
    <span ng-bind-html="highlight(item.primary, data.q)"></span>
  </a>
 <span class="text-muted">
    {{item.short_description}}
 </span>
  <span class="m-l-xs m-r-xs"> &middot; </span>
  <span class="text-muted">
  {{item.sys_created_on}}
    </span>
</div>

 

 

 

Output:

Bhavya11_0-1722833975454.pngBhavya11_1-1722834028384.png

 

 

 

 

in your script try to change query then and search page template it will work fine.

 

 

Please mark helpful & correct answer if it's really worthy for you.

 

 

Thanks,

BK