Get input to pass into server script for a service portal widget

Jake Sadler
Kilo Sage

I'm trying to get the user input from my widget and pass it into my server script to get a result.

 

HTML 

<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Table Extensions</h4>
</div>
<div class="panel-body">
<sn-record-picker field="field" table="'sys_db_object'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" ></sn-record-picker>
<form class="m-t form-horizontal" role="form">
<div class="form-group">
<label class="col-sm-2 control-label">Hierarchy</label>
<div class="col-sm-10">
<input type="text" placeholder="{{data.res}}" class="form-control" readonly>

</div>

 

Client Script

function ($scope) {

var c = this;
$scope.table = "sys_db_object";

c.server.update();
}

 

server script

 

(function() {

var tableName = input.setTable;
var table = new TableUtils(tableName);
var tableArrayList = table.getTables();
var tableArray = j2js(tableArrayList);

var result = tableArray.reverse();
if(result!=''){
data.res = result.join(" >>> ");
}
})();

 

 Widget Preview

 

find_real_file.png

Any help would be greatly appreciated

 

1 ACCEPTED SOLUTION

palanikumar
Mega Sage

Hi,

Try this code. This should work

HTML

<div class="panel panel-default">
  <div class="panel-heading">
    <h4 class="panel-title">Table Extensions</h4>
  </div>
  <div class="panel-body">
    <sn-record-picker field="c.field" 
                  table="'sys_db_object'" 
                  display-field="'name'" 
                  value-field="'name'" 
                  search-fields="'name'" 
                  page-size="100" 
                  ng-change="c.getHierarchy()">
    </sn-record-picker>
    <form class="m-t form-horizontal" role="form">
      <div class="form-group">
        <label class="col-sm-2 control-label">Hierarchy</label>
        <div class="col-sm-10">
          <input type="text" placeholder="{{data.res}}" class="form-control" readonly>
        </div>

Client Controler:

function($scope) {

    var c = this;
    $scope.table = "sys_db_object";

    c.server.update();
    c.getHierarchy = function() {
        c.server.get({
            action: 'get_hierarcgy',
            table: c.field.value
        }).then(function(response) {
            c.data = response.data;
        });
    }
}

Server Script:

(function() {
    if (input && input.action == "get_hierarcgy") {
        var tableName = input.table;
        var table = new TableUtils(tableName);
        var tableArrayList = table.getTables();
        var tableArray = j2js(tableArrayList);

        var result = tableArray.reverse();
        if (result != '') {
            data.res = result.join(" >>> ");
        }
    }
})();
Thank you,
Palani

View solution in original post

6 REPLIES 6

vkachineni
Kilo Sage
Kilo Sage

What is the value in "input.setTable"

 

Here is a code snippet that works.

 

(function() {
var table = new TableUtils("cmdb_ci");
var ciTableList = j2js(table.getTableExtensions());
for (var i = 0; i < ciTableList.length; i++) {
var tableGR = new GlideRecord('sys_db_object');
tableGR.get('name',ciTableList[i])
gs.print(tableGR.label + '\t' + ciTableList[i]);
}
})();

 

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

That's what I'm struggling with I want input.setTable to equal the table the user selects like the below screenshot input.setTable = cmdb_ci_server and so the output would be 

cmdb >>> cmdb_ci >>> cmdb_ci_hardware >>> cmdb_ci_computer >>> cmdb_ci_server

find_real_file.png

See https://serviceportal.io/sn-record-picker/ on how to set the variable in the client script and send it to the server.

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you want user to enter record picker?

then based on that set the hierarchy?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader