- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2018 07:46 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2018 11:59 AM
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
});
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2018 07:49 AM
Hi Filipe,
What log statements are printing?
Also what are you getting in console statements.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2018 07:59 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2018 09:18 AM
Hi Filipe,
you have a small typo with severe consequences so it seems 😉
ga.addParam('sysparam_name', 'getData'); should be ga.addParam('sysparm_name', 'getData');
Stijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2018 09:33 AM