SimonMorris
ServiceNow Employee
Options
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
‎10-06-2011
02:28 AM
I've done some work on some concept code this week, and have learned some interesting things about Glideform and Conditions
The code I'm prototyping would be used during the creation of a record, as the user fills in the form. It's an interesting problem to inspect the form during this state as there isn't a record in the database to query or get until the user hits submit.
The way to do it is to use GlideAjax - a great walkthrough on the wiki - to send the form to the server in it's unwritten state.
var ga = new GlideAjax('MyExampleCode');
ga.addParam('sysparm_name','functionName');
var fields = g_form.getEditableFields();
for (var x = 0; x < fields.length; x++) {
ga.addParam('sysparm_' + fields[x], g_form.getValue(fields[x]));
}
ga.getXML(CallbackFunction);
So g_form.getEditableFields() is a handy method to grab the fields on the form, which would include any customisations a customer will have made. Iterating through those fields I'm building the client side of GlideAjax up by adding sysparm_fieldname to the parameters.
EDIT: Hmm, of course getEditableFields() only gets editable fields, so this isn't so good for fields such as Priority which is typically read-only. I'll update this post when I get that fixed, for the moment I manually added ga.addParam('sysparm_priority', g_form.getValue('priority'));
g_form.getValue(fields[x]) is populating that parameter with the current value of the form.
The screenshot below shows Firebug with the Ajax client sending the values of the form to the server.
On the server side of the GlideAjax conversation I created an empty (or dummy) record and populated the object with the data from the form.
The main part of this snippet is GlideRecordUtil().getFields(incident). This returns a list of all the fields that exist on the form for that table, again including any customer tweaks.
incident = new GlideRecord('incident');
incident.newRecord();
fields = new GlideRecordUtil().getFields(incident);
for (var num = 0; num < fields.length; num++) {
var field_name = fields[num];
incident[field_name] = this.getParameter('sysparm_' + field_name);
}
Now that I have a representation of the record, should the user hit Submit, I can compare the record against filters.
There is a function - Packages.com.glide.script.Filter.checkRecord() that compares a record, in my case a dummy record, against a filter.
var condition = 'category=hardware^priority=2';
var matches = Packages.com.glide.script.Filter.checkRecord(incident, condition);
if (matches) {
// do something with the dummy record that is a priority 2 hardware incident
}
So in summary - how to compare a record against a filter, before it's even been saved!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.