Client Scripts
Replace String within a String or find something within a String
alert (p.replace('dog', 'monkey')) ; <- This is not working in a UI Action
Lovely Dates in Client scripts
Example AJAX call
Option 1:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Populate the fiscal dates according to the dates in the Project Task or Project
if (newValue) {
var ga = new GlideAjax('BiHaBackFillProjectAJAX');
ga.addParam('sysparm_name', 'getPrjFiscalDates');
ga.addParam('sysparm_prsysid', newValue);
ga.getXML(setFiscalPeriods);
}
function setFiscalPeriods(response) {
var answer = response.responseXML.getElementsByTagName("answer");
if (answer) {
g_form.setValue('start_fiscal_period', answer[0].getAttribute("stdte"));
g_form.setValue('end_fiscal_period', answer[0].getAttribute("endte"));
}
}
}
-------- Server Script
getPrjFiscalDates: function() {
var ptSysId = this.getParameter("sysparm_prsysid"); // can be Project or Project Task
var result = this.newItem('answer');
var gr = new GlideRecord('planned_task');
gr.addQuery("sys_id", ptSysId);
gr.query();
if (gr.next()) {
var sdt = gr.start_date;
var edt = gr.end_date;
var ex = new ExpenseLinesUtil();
var stf = ex.calculateFiscalPeriodfromDate(sdt.slice(0, 10));
var etf = ex.calculateFiscalPeriodfromDate(edt.slice(0, 10));
result.setAttribute('stdte', stf);
result.setAttribute('endte', etf);
}
},
Option 2:
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } var userSelected = g_form.getValue('u_user'); var userDetails = new GlideAjax('UserDetailsUtil'); //script include UserDetailsUtil userDetails.addParam('sysparm_name', 'getUserDetails'); //calling function getUserDetails userDetails.addParam('sysparm_userSelect', userSelected); //passing the user detail as a parameter userDetails.getXMLAnswer(function(response){ var userObj = JSON.parse(response); //parsing the user object received frm getUserDetails fn g_form.setValue('u_contact_no', userObj.contactNo); //setting the value in necessary fields g_form.setValue('u_user_id', userObj.userId); }); }
Option 3: getReference - very nice
GlideForm | ServiceNow Developers
Types of alert
g_form
In a Client Script - Using g_form.getReference
g_scratchpad
g_scratchpad.testGlobalVar = "butCreatedFrom";
User Object cheat sheet
https://servicenowguru.com/scripting/user-object-cheat-sheet/
Get the users current language:
var userLanguage = g_lang;
Reference Field
Can I have a different display value in a Reference field to the one defined in the dictionary?
So in any table there can only be one display value.
But suppose I want to show another field ? e..g planned task - the display value is the number. But suppose instead of the number, I want to display the WBS?
STILL TODO
UI Action to go to a link
Make sure that client checkbox is selected (if not it means the code runs on the server)
function showBPCrechnung() {
var sapid = g_form.getValue("u_sap_id");
var url = g_scratchpad.sapProperties.SAPBaseURLhttps;
url = url + "/bc/archiveLink?SAP-ID=" + sapid;
url = url + "&sap-client=510&sap-language=DE";
g_navigation.open(url, '_blank');
}
Stop a catalog item from submitting based on a field value
// I added a hidden field called u_hidden_valuefunction onChange(control, oldValue, newValue, isLoading) {if (isLoading || newValue == '') {return;}// 1. newValue is from the sys_user_grmember table// 2. Check if the User has a date and Group in the sys_user "u_expirytime" & "u_expiry_remote_group" fields// 3. If they do then it means they have permissions already// 4. If they have permissions, inform them and stop the submittion//var inputValue = g_form.getValue(controlName);if (newValue) {var ga = new GlideAjax('UserDataUtils');ga.addParam('sysparm_name', 'doesUseralreadyHaveApptunnelReq');ga.addParam('sysparm_sysid', newValue);ga.getXML(userExistingAccess);}function userExistingAccess(response) {var answer = response.responseXML.getElementsByTagName("answer");if (answer) {var ans = answer[0].getAttribute("HASACCESS");if (ans == "YES") {g_form.setValue('u_hidden_value', answer[0].getAttribute("exttime"));g_form.showFieldMsg("u_supplier_user", "This User already has access until - " + answer[0].getAttribute("exttime"), 'error');} else {g_form.setValue('u_hidden_value', "");}}}}
onLoad Codefunction onSubmit() { var exval = g_form.getValue("u_hidden_value"); if (exval) { g_form.showFieldMsg("u_supplier_user", "This User already has access until - " + exval, 'error'); return false; }}
function onSubmit() {var exval = g_form.getValue("u_hidden_value");if (exval) {g_form.showFieldMsg("u_supplier_user", "This User already has access until - " + exval, 'error');return false;}}
OnChange catalog client script calling a function in onLoad
// You need to disable "Isolate script" on your onLoad script
function onLoad() {
//Type appropriate comment here, and begin script below
}
checkPrimaryRole = function () { // You must have this format for the function to work!
alert('in the function');
}function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
checkPrimaryRole();
}
Comments
Post a Comment