2

I'm very new to ServiceNow so sorry if this is an obvious question. But I've checked the docs and haven't found a way to do this.

In my ServerScript I query to get a record from a table, and then to send that to the ClientScript I get the record values (this is the problem) and then add that obj to the data obj.

Is there a way to get all of the records fields without listing them all? Similar to sql Select * ...?

    ...
    var userGR = new GlideRecord('sys_user');
    gs.info(gs.getUser());
    userGR.addQuery('user_name', gs.getUser().getName());
    userGR.query();
    while(userGR.next()){
        var userObj = {};
        $sp.getRecordValues(userObj, userGR, 'user_name');
        data.users.push(userObj);
    }
    ...

I want to do something like $sp.getRecordValues(userObj, userGR, '*'); in order to get all the columns in that table.

I'm thinking it's not natively possible, to try and prevent unnecessary data being sent. But just curious if there is a way?

John
  • 1,808
  • 7
  • 28
  • 57

3 Answers3

0

Well, typically this is not done the way you indend to do it. When getting data from the client side, this is done using GlideAjax, whis essentialy receives a datastructure (for example a JSON) from the server. The GlideAjax calls a client callable script include. Then you can infer the structure of the data from this data structure.

The code on the client side roughly loks like this:

var ga = new GlideAjax('YourScriptIncludeName'); 
ga.addParam('sysparm_name','yourFunctionName'); // Get the Account Mgr 
ga.addParam('sysparm_yourparameter',yourValue); // Set parameter sysidAccount 
ga.getXML(ParseStuff);  

function ParseStuff(response) 
{  
    "use strict";

    var answer = response.responseXML.documentElement.getAttribute("answer");

    // Decode answer
    var objAnswer = answer.evalJSON(true);

    // objAnswer is a JS Object, you may for example loop thorugh the properties        

}
0

As of now this API doesn't support such selections. The method only accepts "A comma-separated list of field names."

https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_GSPS-getRecordValues_O_GR_S

Rahul R
  • 111
  • 6
0

GlideRecord does always retrieve all the columns on the table, you just need to know their names to retrieve them from the GlideRecord instance. Fortunately, it provides an API to do exactly that: userGR.getElements. It returns a list of GlideElement instances which give you a bunch of useful metadata about the schema (name, type, etc.). Additionally, that $sp.getRecordValues method is really just a convenience method that essentially just does this:

function getRecordValues(data, gr, names) {
    for (name in split(names))
        data.put(n, data, gr.getValue(name));
}

So, you can just do this to get what you want:

var userGR = new GlideRecord('sys_user');
if (userGR.get(gs.getUserID())) {
    var userObj = {};
    for (element in userGR.getElements()) {
        userObj[element.getName()] = userGR.getValue(element.getName());
    }
    data.users.push(userObj);
}
Joey
  • 2,901
  • 21
  • 22