Skip to content

Instantly share code, notes, and snippets.

@cmcdevitt
Last active March 10, 2023 14:55
Show Gist options
  • Select an option

  • Save cmcdevitt/638971a55f0d3e7b81696e998914342c to your computer and use it in GitHub Desktop.

Select an option

Save cmcdevitt/638971a55f0d3e7b81696e998914342c to your computer and use it in GitHub Desktop.
AJAX Simple Example
/*
**** Client Script ****
Name: CM Ajax Test
Table: Incident [incident]
UI Type: All
Type: On Load
Application: Global
Active, Inherited, Global: True (checked)
*/
//Script
function onLoad() {
var ga = new GlideAjax('global.GetDepartmentAJAX');//Name of Script Include
ga.addParam('sysparm_name','grpNameAvail');// Must be "sysparm_name". Name of function/method in the Script Include to call
ga.addParam('sysparm_one','foo'); //Paramete to pass. Must star with "sysparm_"
ga.addParam('sysparm_two','bar'); //Paramete to pass. Must star with "sysparm_"
ga.addParam('sysparm_sys_user_group', 'chris'); //Paramete to pass. Must star with "sysparm_"
ga.getXML(processIt); //Do it. "processIt" is the "callback" function below
}
function processIt(response){//Callback function
var answer = response.responseXML.documentElement.getAttribute("answer");//Must use this exact line
alert(answer);
}
/*
**** Script Include ****
Name: GetDepartmentAJAX
API Name: global.GetDepartmentAJAX
Application: Global
Asccessible from: "All application Scopes" <- IMPORTANT!
Client callable: True (checked) <- IMPORTANT!
Active: True (checked)
*/
//Script
var GetDepartmentAJAX = Class.create();
GetDepartmentAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {
grpNameAvail: function() {
/*
arguments: none
Parameters/Arguments:
Are passed via the 'sysparm_xxxx' varables from the Client Script
Are accessed via this.getParameter("sysparm_xxxx");
Return true/false
*/
var group_name = this.getParameter("sysparm_sys_user_group"); //Matches from Client Script above
//Starndard Script Include stuff...
var answer = true;
var grp = new GlideRecord('sys_user_group');
grp.addEncodedQuery('name=' + group_name);
grp.query();
while (grp.next()) {
answer = false;
}
//Don't return an object, Return a String
return answer;//Whatever you return ends up in the "answer" varable in the client script once the XML is parsed
},
type: 'GetDepartmentAJAX'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment