Skip to content

Instantly share code, notes, and snippets.

@wave-inguane
Last active Aug 16, 2021
Embed
What would you like to do?
Server APIs
//...........................................................................................................................
// Reference qualifier Script Include
//...........................................................................................................................
var noBlankFieldUsers = Class.create();
noBlankFieldUsers.prototype = {
initialize: function() {
},
// Usage - Reference qualifier
// javascript: new global.noBlankFieldUsers().getUsers();
getEpaUsers: function() {
var grUser = new GlideRecord('sys_user');
grUser.addEncodedQuery("active=true^emailISNOTEMPTY^user_nameISNOTEMPTY^first_nameISNOTEMPTY");
grUser.query();
var userList;
while(grUser.next()) {
userList += ","+grUser.sys_id;
}
return 'sys_idIN'+userList;
},
type: 'noBlankFieldUsers'
};
//...........................................................................................................................
// These are two simple examples using GlideRecordSecure.
//...........................................................................................................................
var att = new GlideRecordSecure ('sys_attachment');
att. get('$[sys_attachment.sys_id]');
var sm = GlideSecurityManager.get();
var checkMe = 'record/sys_attachment/delete';
var canDelete = sm.hasRightsTo(checkMe,att);
gs. log('canDelete: ' + canDelete);
canDelete;
//...........................................................................................................................
//
//...........................................................................................................................
var grs = new GlideRecordSecure('task_ci');
grs.addQuery();
grs.query();
var count = grs. getRowCount();
if (count > 0 ) {
var allocation = parseInt(10000/count) / 100;
while (grs.next()) {
grs.u_allocation = allocation;
grs.update();
}
}
//...........................................................................................................................
//
//...........................................................................................................................
Table: content_block_programmatic
Name: Custom google search
Category: General
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<script>
function u_search() {
var newwindow = window.open("http://www.google.com/cse?cx=e6960d264296ca6ec&amp;q="+document.getElementById('u_search_text').value,'name','width=800,height=600,left=100,top=100,resizable=yes,scrollbars=yes,status=yes,toolbar=no,menubar=yes,location=no');
if (window.focus) { newwindow.focus() }
}
</script>
<div style="text-align: center">
<input id='u_search_text' type="text" value="GlideRecord" style="width:400px;" />
<button onclick='u_search(); return false'>Search</button>
</div>
</j:jelly>
//...........................................................................................................................
// Email script
//...........................................................................................................................
Invoke:
${mail_script:ng_activity_mention_body}
//*****************************************************************************************************************************
// Business Rules
//*****************************************************************************************************************************
/*
Usage: SN Agile Development Module
Name: LinkToStory
Table: sys_update_set
When: before - insert
Naming convention :AVI-STRY0013430-short_desc.v1
Script
*/
(function executeRule(current, previous /*null when async*/) {
if( current.u_story == ""){
var name = current.name+"";
var end = 0;
var start = 0;
if(name.indexOf("STRY") > 0){
start = name.indexOf("STRY");
end = start+11;
}
var number = name.substring(start, end);
var grSTORY = new GlideRecord("rm_story");
if (grSTORY.get("number",number)) {
current.u_story = grSTORY.sys_id;
}
}
})(current, previous);
//*****************************************************************************************************************************
// Workflow
//*****************************************************************************************************************************
/*
Workflow Activity: Wait for condition
Stage: Fulfillment (1 Day )
Text: Wait for all catalog tasks to be closed
Then next stage is
Activity: X= Set Values
Stage: Completed
Text: Complete
Set these values:
Stage = Closed Complete
Stage = Complete
*/
// Set the variable 'answer' to true or false to indicate if the condition has been met or not.
chkTask();
function chkTask() {
var c = 0;
var chk = 0;
var ctk = new GlideRecord('sc_task');
ctk.addQuery('request_item', current.sys_id);
ctk.query();
c = ctk.getRowCount();
while (ctk.next()) {
if (ctk.state >= "3") // Here we are checking, whether the state of task is closed, so "3" is closed complete, 4 is closed incomplete and 7 is closed skipped.
{
chk = chk + 1;
}
}
if (chk == c) {
answer = true;
}
}
//*****************************************************************************************************************************
// UI Actions
//*****************************************************************************************************************************
Name: Cancel Request
Table: Request[sc_request]
Order: 100
Action name: state_model_move_to_canceled
Active: true
Show update: true
Client: true
Form button: true
Form: context menu: true
Hint: Progresses request to closed cancelled state
Onclick: loadConfirmDialog();
Condition: current.request_state == 'requested' || current.request_state == 'in_process'
Script:
var changeConfirmCancelDialog;
function loadConfirmDialog() {
var dialogClass = window.GlideModal ? GlideModal : GlideDialogWindow;
changeConfirmCancelDialog = new dialogClass("change_confirm_cancel", false, 648, 250);
changeConfirmCancelDialog.setTitle(new GwtMessage().getMessage("Cancel Request"));
changeConfirmCancelDialog.render();
}
function moveToCancel(notes) {
var ga = new GlideAjax('CatalogRequestClientUtil');
ga.addParam('sysparm_name', 'cancelRequestAndRITM');
ga.addParam('sysparm_req', g_form.getUniqueValue());
ga.addParam('sysparm_notes', notes);
ga.getXML(function(resp) {
var result = JSON.parse(resp.responseXML.documentElement.getAttribute("answer"));
if (result.error == 0) {
var currentURL = window.parent.location.href;
window.parent.location.href = currentURL;
} else {
g_form.addErrorMessage('Unable to update Request ' + g_form.getValue('number'));
}
});
}
if (typeof window == 'undefined')
setRedirect();
function setRedirect() {
current.update();
var grRITM = new GlideRecord("sc_req_item");
if (grRITM.get("request", current.sys_id)) {
var grTASK = new GlideRecord('sc_task');
grTASK.addQuery('request_item',grRITM.sys_id);
grTASK.query();
while(grTASK.next()){
if(grTASK.state != 3 || grTASK.state != 7){
grTASK.state = 4;//'closed_incomplete';
grTASK.update();
}
}
var workflow = new Workflow();
workflow.cancel(grRITM);
}
action.setRedirectURL(current);
}
--------------------------------------CatalogRequestClientUtil--------------------------------------------
Script Include:
Name: CatalogRequestClientUtil
API Name: global.CatalogRequestClientUtil
Client callable: true
Active: true
Script:
var CatalogRequestClientUtil = Class.create();
CatalogRequestClientUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
cancelRequestAndRITM: function() {
var returnValue = {
error: 0
};
var request = this.getParameter('sysparm_req');
var notes = this.getParameter('sysparm_notes');
var reqGr = new GlideRecord("sc_request");
if (reqGr.get(request)) {
var requestNumber = reqGr.getValue('number');
reqGr.setValue("request_state", 'closed_incomplete');
reqGr.setValue("stage", 'closed_incomplete');
reqGr.work_notes = notes;
if (reqGr.update()) {
var ritmGr = new GlideRecord('sc_req_item');
ritmGr.addQuery('request', request);
ritmGr.query();
if (ritmGr.next()) {
ritmGr.setValue('stage', 'closed_incomplete');
ritmGr.setValue('state', 4);
ritmGr.work_notes = ('Notes from ' + requestNumber + ': ' + notes);
if (!ritmGr.update()) {
returnValue.error = 1;
}
} else {
returnValue.error = 1;
}
}
else {
returnValue.error = 1;
}
}
else {
returnValue.error = 1;
}
return JSON.stringify(returnValue);
},
type: 'CatalogRequestClientUtil'
});
//*****************************************************************************************************************************
// Other Integrations
//*****************************************************************************************************************************
BY Email:
https://community.servicenow.com/community?id=community_blog&sys_id=908c26e1dbd0dbc01dcaf3231f9619c4
https://community.servicenow.com/community?id=community_blog&sys_id=908c26e1dbd0dbc01dcaf3231f9619c4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment