The CreatorCon Call for Content is officially open! Get started here.

AngularJS retrieve data from include script

Filipe Coucello
Kilo Contributor

Greetings,

I'm developing an application following the Learning Plans from the developer.servicenow.com website.

I was just introduced to AngularJS application. I just finished the module Introduction to AngularJS in ServiceNow.

All worked like a charm.

Now I'm trying to get some information from the database by the UI script of the "Client-side logic for Angular App" so I added this piece of code.

$scope.getIncidents = function() {
    var ga = new GlideAjax("FCDevInstDBcontext");		
	ga.addParam('sysparam_name', 'getData');
	console.log(ga);
	ga.getXMLAnswer(function(answer) {
		console.log("Awnser: " + answer);
		alert(answer);
	});
};

And then I created a Script Include for my app:

var FCDevInstDBcontext = Class.create();
FCDevInstDBcontext.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	getData: function() {
		gs.log(">-----------FCDevInstDBcontext-------------<");
		var records = [];
		var incidentsTbl = new GlideRecord('incident');
		incidentsTbl.query();
		while(incidentsTbl.next()) {
			records.push(incidentsTbl);			
		}
		gs.log(records);
		return records;
	},
});

It always returns null.

I found out that I can use GlideRecord on my angular UI Script using an asynchronous function, but that's not a good way to do it, calling it from the client side, Am I right?

Any help is appreciated.

Regards,

Filipe

1 ACCEPTED SOLUTION

To resolve this next issue instead of directly pushing each returned GlideRecord object into the array, a new object should be created and then pushed into the array. Then after that stringify the return and then encode/parse the callback response. 

For example in the script include:

getData: function() {
	gs.log(">-----------FCDevInstDBcontext-------------<");
	var records = [];
	var incidentsTbl = new GlideRecord('incident');
	incidentsTbl.query();
	while(incidentsTbl.next()) {
                var obj = {};
                obj.number = incidentsTbl.getValue('number');
                obj.description = incidentsTbl.getDisplayValue('description');
		records.push(obj);			
	}
	gs.log(records);
	return JSON.stringify(records);
}

Okay, that could be a pain to have to write out all the fields if you need all the fields returned. This could be an alternative:

getData: function() {
	gs.log(">-----------FCDevInstDBcontext-------------<");
	var records = [];
	var incidentsTbl = new GlideRecord('incident');
	incidentsTbl.query();
	while(incidentsTbl.next()) {
                var fields = Object.keys(incidentsTbl);
                var obj = {};
                fields.forEach(function assignValuesToObj(key){
                    obj[key] = incidentsTbl.getValue(key);
                    if(incidentsTbl[key].getDisplayValue())
                          obj[key + '_dv'] = incidentsTbl[key].getDisplayValue();
                });
                
		records.push(obj);			
	}
	
	return JSON.stringify(records);
}

Then in the glideAjax in the client script:

$scope.getIncidents = function() {
    var ga = new GlideAjax("FCDevInstDBcontext");		
	ga.addParam('sysparam_name', 'getData');
	//console.log(ga);
	ga.getXMLAnswer(function(answer) {
		console.log("Awnser: " + JSON.parse(answer));
		alert(answer); //not parsing the alert because it needs to be string here to display correctly in an alert
	});
};

View solution in original post

6 REPLIES 6

To resolve this next issue instead of directly pushing each returned GlideRecord object into the array, a new object should be created and then pushed into the array. Then after that stringify the return and then encode/parse the callback response. 

For example in the script include:

getData: function() {
	gs.log(">-----------FCDevInstDBcontext-------------<");
	var records = [];
	var incidentsTbl = new GlideRecord('incident');
	incidentsTbl.query();
	while(incidentsTbl.next()) {
                var obj = {};
                obj.number = incidentsTbl.getValue('number');
                obj.description = incidentsTbl.getDisplayValue('description');
		records.push(obj);			
	}
	gs.log(records);
	return JSON.stringify(records);
}

Okay, that could be a pain to have to write out all the fields if you need all the fields returned. This could be an alternative:

getData: function() {
	gs.log(">-----------FCDevInstDBcontext-------------<");
	var records = [];
	var incidentsTbl = new GlideRecord('incident');
	incidentsTbl.query();
	while(incidentsTbl.next()) {
                var fields = Object.keys(incidentsTbl);
                var obj = {};
                fields.forEach(function assignValuesToObj(key){
                    obj[key] = incidentsTbl.getValue(key);
                    if(incidentsTbl[key].getDisplayValue())
                          obj[key + '_dv'] = incidentsTbl[key].getDisplayValue();
                });
                
		records.push(obj);			
	}
	
	return JSON.stringify(records);
}

Then in the glideAjax in the client script:

$scope.getIncidents = function() {
    var ga = new GlideAjax("FCDevInstDBcontext");		
	ga.addParam('sysparam_name', 'getData');
	//console.log(ga);
	ga.getXMLAnswer(function(answer) {
		console.log("Awnser: " + JSON.parse(answer));
		alert(answer); //not parsing the alert because it needs to be string here to display correctly in an alert
	});
};

Thank you very much 😄 I knew something was wrong on that while. Thank you ChrisB