
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2018 11:34 PM
Hi,
I am a newbie to Service Portal.
I have to create a page on which there are 2 dropdowns and 1 button.
In the first drop-down, there are tables which extend the Task table.
In the second drop-down, there are options(xls,docx) in which format user wants to download the file.
On click of a button, a user should get the file downloaded with the 'list of column labels and names of the table choose from first drop-down and add/insert the data into the (xls,docx) file'.
My questions are:
1. How to send the chosen table to server from client OR is there any other alternative to get all the columns of the table at client side?
2. How to download a (docs,xls) file on click of a button with the column labels and names in it?
Please find below what I have coded.
HTML Template:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Green Folder</title>
</head>
<body>
<div>
<div class="form-group" >
<select class="form-control" id="sel1" ng-model="optionText" ng-options="value for value in data.tables">
<option value="" disabled selected hidden>Select Table</option>
</select>
</div>
<div class="form-group" >
<select class="form-control" id="format">
<option value="" disabled selected hidden>Select Format</option>
<option value="xls">xls</option>
<option value="docx">docx</option>
</select>
</div>
<div class="container">
<button type="submit" class="btn btn-default" ng-click="c.getMergeFields();">Get Merge Fields</button>
</div>
</div>
</body>
</html>
Client Script:
function() {
/* widget controller */
var c = this;
var optionText = c.data.tables;
console.log("Option Text: " + optionText);
c.getMergeFields = function(){
var tableName= document.getElementById('sel1').value
console.log("Table name: " + tableName);
c.server.getmFields(tableName).then(function(resp){
var getfields = resp.data.mFields;
});
}
}
Server Script:
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
data.tables = getExtendTables();
function getExtendTables(){
//To fetch tables those extends Task table
gs.include('TableUtils');
var table = new global.TableUtils("task");
var tableList = table.getAllExtensions();
tableList = tableList.toString();
gs.info("tableList of type " + typeof tableList + " == " + tableList);
tableList = tableList.slice(1,-1);
gs.info("Slicing of tableList of type: " + typeof tableList + " here: " + tableList);
var arr = tableList.split(",");
gs.info("Arr: " + arr);
arr.sort();
return arr;
}
//To fetch column labels and name of table selected by user
if(input){
data.mFields = getmFields(input.tableName);
gs.info("mFields: " + mFields);
}
function getmFields(tableName){
gs.info("Input: " + tableName);
var gr = new GlideRecord(tableName);
gr.initialize();
gr.query();
gr.next();
var elements = gr.getElements();
var element;
var result = [];
for (var i=0; i<elements.length; i++) {
element = elements[i];
result[i]=element.getLabel();
}
result.sort();
gs.info("Result: " + result);
return result;
}
})();
Is there any better approach or best practice for this?
Any help would be appreciated.
Thanks in advance!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2018 03:48 AM
Hi Nikita,
The code I gave you exactly does the same thing. Please see below screenshot and updated code
if(input){
columnArray = [];
data.fields = $sp.getListColumns(input.table, 'default');
data.fields_array = data.fields.split(',');
var grForLabels = new GlideRecord(input.table);
for (var i in data.fields_array) {
console.log('going here');
var field = data.fields_array[i];
var ge = grForLabels.getElement(field);
if (ge == null)
continue;
var dataobj = {};
dataobj.value = field.toString();
dataobj.text = ge.getLabel().toString();
columnArray.push(dataobj);
}
data.columnDetails = columnArray;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2018 11:36 PM
Hi Rahul,
I tried this but I am getting an error.
Please see my updated code below.
I have pointed error in server script as comments beside gr.query();
Client Script:
function() {
/* widget controller */
var c = this;
var $scope = this;
//List of tables stored
var optionText = c.data.tables;
console.log("Option Text: " + optionText);
$scope.getMergeFields = function(){
//Fetch the selected table value from HTML using ng-model
$scope.data.tables = $scope.options;
console.log("Scope table name:" + $scope.data.tables);
//Call to server
$scope.server.update();
}
}
Server Script:
if(input){
gs.log("Input: " + input.tables);//Getting user selected table.
var gr = new GlideRecord(input.tables);
gr.initialize();
gr.query(); //Error at this point.It says " org.mozilla.javascript.EvaluatorException: GlideRecord.addQuery() - invalid table name: incident (sp_widget.418270c1db0623005f151fc768961924.script; line 29)"
gr.next();
var elements = gr.getElements();
var element;
var result = [];
for (var i=0; i<elements.length; i++) {
element = elements[i];
result[i]=element.getLabel();
}
result.sort();
gs.info("Result: " + result);
return result;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2018 01:36 AM
Hi,
Do you want to get the labels of a tables ? is it your requirement ? if yes, please use below code
if(input)
{data.fields = $sp.getListColumns(input.tables, input.view ||'default');
data.fields_array = data.fields.split(',');
var grForLabels = new GlideRecord(data.table);
for (var i in data.fields_array) {
console.log('going here');
var field = data.fields_array[i];
var ge = grForLabels.getElement(field);
if (ge == null)
continue;
var dataobj = {};
dataobj.value = field.toString();
dataobj.text = ge.getLabel().toString();
columnArray.push(dataobj);
}
data.columnDetails = columnArray;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2018 03:35 AM
No, I have already fetched table names.
I want column labels and column names of the table.
For eg.: Consider Incident Table, which has Assigned to column with a label as "Assigned to" and name as "assigned_to".
Likewise, I need all the columns of the selected table.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2018 03:48 AM
Hi Nikita,
The code I gave you exactly does the same thing. Please see below screenshot and updated code
if(input){
columnArray = [];
data.fields = $sp.getListColumns(input.table, 'default');
data.fields_array = data.fields.split(',');
var grForLabels = new GlideRecord(input.table);
for (var i in data.fields_array) {
console.log('going here');
var field = data.fields_array[i];
var ge = grForLabels.getElement(field);
if (ge == null)
continue;
var dataobj = {};
dataobj.value = field.toString();
dataobj.text = ge.getLabel().toString();
columnArray.push(dataobj);
}
data.columnDetails = columnArray;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2018 05:56 AM
Hi Rahul,
The code is working fine when tested with manually entering the table name as you did in the screenshot. But it is not working when I update the table name with input.tables.
Neither I got value for the data.fields nor fields_array and others also. As no fields are fetched, its giving error at line 9 (according to your screenshot).
Error at line 9: org.mozilla.javascript.EvaluatorException: GlideRecord.getElement() - invalid table name: incident (sp_widget.418270c1db0623005f151fc768961924.script;