Vous êtes sur la page 1sur 6

var results = [];

var gr = new GlideRecord('u_check_in');


gr.addQuery('u_guest.name', 'CONTAINS', 'Alice');
gr.setLimit(2);
gr.orderByDesc('sys_created_on');
gr.query();
while(gr.next()) {
results.push(gr.sys_created_on + '');
}
gs.log(results.join(', '));

============================================================
getting single record
var gr = new GlideRecord('u_check_in');
gr.get('u_guest.name', 'Alice Richards');
gs.log(gr.sys_created_on + '');
=============================================================
GLIDEAGGREGATE Function
var count = new GlideAggregate('u_check_in');
count.addQuery('sys_created_on', '>', gs.beginningOfYesterday());
count.addAggregate('count');
count.query();
var result = 0;
if (count.next())
result = count.getAggregate('COUNT');
gs.log('Result: ' + result);
====================================================================
(current.u_number.changes() || current.u_floor.changes()) && current.u_floor.isN
il() && current.u_number.toString().length > 1
current.u_floor = current.u_number.substring(0, 1);
========================================================================
stopn duploicate checkins
cond: current.u_room.changes() || current.u_date.changes() || current.operation(
) == 'insert'
script:
var dup = new GlideRecord('u_check_in');
dup.addQuery('u_room', current.u_room);
dup.addQuery('u_date', current.u_date);
dup.setLimit(1);
dup.query();
if (dup.next()) {
gs.addErrorMessage('This room is already checked in on this date.');
current.setAbortAction(true);
}
================================================================================
=================================================
After Business Rule
NB: Always use update() to make changes
Condition:
current.u_lead.changesTo(true) ||(current.u_lead && current.action() == 'insert'
)
Script
var lead = new GlideRecord('u_m2m_guests_reservations');
lead.addQuery('u_reservation', current.u_reservation);
lead.addQuery('u_lead', true);
lead.addQuery('sys_id', '!=', current.sys_id);
lead.query();
while(lead.next()) {
lead.u_lead = false;
lead.update();
}
================================================================================
======
Script Include
var SimpleAdd = Class.create();
SimpleAdd.prototype = {
initialize: function (n) { //prototype consists of two function initialize and
add
gs.log('Creating new object');
this.number = (n - 0) || 0; // this is used if string is passed, it will tak
e 0
},
increment: function () {
this.number++;
gs.log(this.number);
return this;
},
type: 'SimpleAdd'
};
Calling:
var sa = new SimpleAdd();
sa.increment().increment();// this will call increment twice.
gs.log('Accessing the variable number ' + sa.number)

Extends Object in script include:


var SimpleSubtract = Class.create();
SimpleSubtract.prototype = Object.extendsObject(SimpleAdd, {
decrement: function () {
this.number--;
gs.log(this.number);
return this;
},
type: 'SimpleSubtract'
});
var ss = new SimpleSubtract(2);
ss.decrement().increment();
================================================================================
=========
Client side script
guest field
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (!isLoading) {
g_form.hideFieldMsg('u_guest');
}
if (newValue == '') {
return;
}
g_form.getReference('u_guest', function(guest) {
if (guest.vip == 'true') {
g_form.showFieldMsg('u_guest', 'Guest is a VIP!');
}
});
}
================================================================================
===========
Validate date
Let's improve the situation with a few undocumented functions. To do this, navig
ate to System Definition > Validation Scripts and click on New. Then, use the fo
llowing values:
Type: Date
Validator:
function validate(value) {
if (!value) {
return true;
}
if (getDateFromFormat(value, g_user_date_format) != 0)
return true;
return "Invalid date format. Please use " + g_user_date_format;
}
================================================================================
====================
Using GlideAjax
SI:
var QuickQuery = Class.create();
QuickQuery.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getField : function() {
var table = this.getParameter('sysparm_table');
var sys_id = this.getParameter('sysparm_sys_id');
var field = this.getParameter('sysparm_field');
var gr = new GlideRecordSecure(table);
gr.setWorkflow(false);
gr.get(sys_id);
if (gr.isValidRecord()) return gr[field];
else return null;
},
type: "QuickQuery"
});
CS(Client Script)
var ga = new GlideAjax('QuickQuery');
ga.addParam('sysparm_name', 'getField');
ga.addParam('sysparm_table', 'u_guest');
ga.addParam('sysparm_sys_id', newValue);
ga.addParam('sysparm_field', 'vip');
ga.getXMLAnswer(function(answer) {
if (answer == 'true') {
g_form.showFieldMsg('u_guest', 'Guest is a VIP!');
}
});

=========================================================================
Business Rule:
Name: Get VIP flag
Table: Check-in [u_check_in]
Advanced: <ticked>
When: display
Script: g_scratchpad.vip = current.u_guest.vip
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var showVIP = function() {
g_form.showFieldMsg('u_guest', 'Guest is a VIP!');
};
if (isLoading) {
if (g_scratchpad.vip) {
showVIP();
}
return;
} else {
g_form.hideFieldMsg('u_guest');
}
if (newValue == '') {
return;
}
var ga = new GlideAjax('QuickQuery');
ga.addParam('sysparm_name', 'getField');
ga.addParam('sysparm_table', 'u_guest');
ga.addParam('sysparm_sys_id', newValue);
ga.addParam('sysparm_field', 'vip');
ga.getXMLAnswer(function(answer) {
if (answer == 'true') {
showVIP();
}
});
}
================================================================================
==========
List action UI Contextmenu
In list title:
window.open(window.location.href);
In list row:
window.open('/' + g_list.getTableName() + '.do?sys_id=' + g_sysId);
================================================================================
=========
One way is to create an onLoad client script that sets up these events manually.
The following code uses some undocumented functions in the ServiceNow platform
to run script when the mouse moves over the Departure field on the Reservation f
orm. You can create a new Client Script on the Reservation table to try this out
:
function onLoad() {
var control = g_form.getControl('u_departure');
Event.observe(control, 'mouseover', function() {
g_form.hideFieldMsg(control);
g_form.showFieldMsg(control, 'in');
});
Event.observe(control, 'mouseout', function() {
g_form.hideFieldMsg(control);
g_form.showFieldMsg(control, 'out');
});
}
we can create our own custom events
CustomEvent.observe('sayHello', function() {
alert('Hello, world!')
});
CustomEvent.fire('sayHello');
==========================
Not known by others new users
After running the below code on a form with javascript executor and close the ja
vascript executor. This will increase the font size of the data
$$('input').invoke('observe', 'mouseover', function(event) {
this.setStyle({fontSize: '30px'});
});
This code is quite impenetrable to someone who isn't familiar with Prototype or
other JavaScript libraries. First, the code searches for all input fields throug
h the $$ shortcut. It then runs a function called observe on each one. It passes
through the mouseover event name and a function that will be called when that e
vent occurs. This function then increases the size of the font of the element, s
o you will get a supersized text when the mouse hovers over it. This code could
easily be made part of a Client Script.
=============================================================================
Now, pretend to be a malicious user. In the address bar of your browser, enter t
he following online script and press enter
javascript:g_form.setMandatory('u_comments', false);

================================================================================
===
At midday, a Scheduled Job looks for any reservations that are ending today
For each one, the room.reservation_end event is fired
A Script Action will be called, which creates a new Maintenance task
The Maintenance task is assigned, through a template, to the Housekeeping group.
But how does Housekeeping know that this task has been created? Let's send them
an e-mail!
1. Register event -----maintenance.assigned
2. Add the event to eventqueue === Maintenance assignment events with Business R
ule
3. Schedule Job at 12 noon --- Clean on end of reservation
4. creating event ----room.reservation_end
5. create template for maintenance -- End of reservation room cleaning
6. create script action for creating task -- Produce maintenance tasks
7.
================================================================================
=======

Vous aimerez peut-être aussi