How to send data from Client controller to Server side?

Nikita28
Mega Guru

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!

1 ACCEPTED SOLUTION

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;
	}

find_real_file.png

View solution in original post

14 REPLIES 14

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;
}

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;
}

 

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.

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;
	}

find_real_file.png

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;