Skip to content

Instantly share code, notes, and snippets.

@DesignFront
Created March 4, 2021 10:08
Show Gist options
  • Save DesignFront/74a88f99df38ed29eb89d27ad6554019 to your computer and use it in GitHub Desktop.
Save DesignFront/74a88f99df38ed29eb89d27ad6554019 to your computer and use it in GitHub Desktop.
service now custom code
var _get;
window.SN = {
  get: function(scriptInclude, name, obj, cb) {
    return _get(scriptInclude, name, obj, cb, true);
  },
  getPlane: function(scriptInclude, name, obj, cb) {
    return _get(scriptInclude, name, obj, cb);
  }
};
_get = function(scriptInclude, name, obj, cb, bool) {
  var ga, key;
  if (!cb && obj) {
    cb = obj;
    obj = null;
  }
  ga = new GlideAjax(scriptInclude);
  ga.addParam('sysparm_name', name);
  for (key in obj) {
    if (key === 'name') {
      console.log('"name" cannot be used because ServiceNow uses it to know which function is to be executed server-side');
      continue;
    }
    ga.addParam("sysparm_" + key, obj[key]);
  }
  return ga.getXML(function(res) {
    var e, error;
    if (!_.isFunction(cb)) {
      return;
    }
    e = null;
    res = res.responseXML.documentElement.getAttribute("answer");
    if (res == null) {
      e = {
        debug: "Server didn't send back a response",
        message: "Incorrect request"
      };
    }
    if (bool && !e) {
      try {
        res = JSON.parse(res);
        if (res.error) {
          e = {
            debug: res.error,
            message: res.error
          };
          res = null;
        }
      } catch (error) {
        e = error;
        e = {
          debug: "Couldn't JSON.parse response from the server: " + res,
          message: res
        };
        res = null;
      }
    }
    return cb(e, res);
  });
};
Vervolgens kan je het volgende doen:
var params = {
  sys_id: 'j40skldhG40dJ2i3jnn4',
  search: 'i can not find my printer settings',
  type: 'ticket'
}
            "Script-Include",  "functie" 'object met data', 'callback'
SN.get('ITGuardian',       'ticket',    params,               function(e, data) {});
Je krijgt dan in je callback een eventuele error terug of data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment