Workspace new record creation querying Script Include
Release:
Australia
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi everyone,
I'm looking for some guidance from anyone who has worked with Workspace banners (CSM/FSM) and Script Includes.
We have a Script Include (code shared below) which determines whether to display one of the following banners based on an account opt-out flag:
- "Data for this customer must not be processed outside of the UK."
- "Data for this customer can be processed outside of the UK."
The Script Include is mapped to several tables, including sn_customerservice_case, and evaluates fields such as:
account.u_data_outside_eu_opt_out
The issue I'm seeing is only in Workspace.
var WVNTBannerUtils = Class.create();
WVNTBannerUtils.prototype = {
initialize: function(current) {
this._validRecord = false;
//gs.info("WVNTBannerUtils.initialize()");
var tu = new TableUtils(current.sys_class_name);
this._basetable = tu.getAbsoluteBase();
gs.info("WVNTBannerUtils: " + current.sys_class_name + " " + current.sys_id + " basetable:" + this._basetable);
var rec = new GlideRecord(current.sys_class_name);
if (rec.get(current.sys_id)) {
this._table = current.sys_class_name;
this._sys_id = current.sys_id;
this._validRecord = true;
this._current = rec;
}
},
// Encapsulate with some general function to get multiple banners
getBannersForTableRecord: function(){
if (!this._validRecord) {
return result;
}
return this.getOptOutForTableRecord();
},
// Output: {show: true/false, message: "string"}
getOptOutForTableRecord: function() {
var result = {
show: false,
type : "",
header : "",
message : "",
image : "",
content: ""
};
// Need a mapping for how we identify the opt in/out flag from an Account from each table
// these can be arrays if we need to check multiple methods
var optOutMap = {
"sn_customerservice_case": ["account.u_data_outside_eu_opt_out"],
"sn_customerservice_task": ["account.u_data_outside_eu_opt_out", "parent_case.account.u_data_outside_eu_opt_out"],
"customer_account": ["u_data_outside_eu_opt_out", "account_parent.u_data_outside_eu_opt_out", "account_parent.account_parent.u_data_outside_eu_opt_out"],
"customer_contact": "account.u_data_outside_eu_opt_out",
//"incident": "company.ref_customer_account.u_data_outside_eu_opt_out",
//"change_request": "company.ref_customer_account.u_data_outside_eu_opt_out",
"sc_req_item": ["company.u_data_outside_eu_opt_out","request.company.u_data_outside_eu_opt_out", "company.ref_customer_account.u_data_outside_eu_opt_out","request.ref_customer_account.company.u_data_outside_eu_opt_out"],
"task": ["company.ref_customer_account.u_data_outside_eu_opt_out","change_request.company.ref_customer_account.u_data_outside_eu_opt_out"],
"sn_ind_tmt_orm_order": ["account.u_data_outside_eu_opt_out","contact.account.u_data_outside_eu_opt_out"],
"sn_ind_tmt_orm_order_line_item": ["account.u_data_outside_eu_opt_out","parent_order.account.u_data_outside_eu_opt_out"],
"interaction": ["account.u_data_outside_eu_opt_out","contact.account.u_data_outside_eu_opt_out"]
};
if (!this._validRecord) {
return result;
}
var optOutFields = optOutMap[this._table] || optOutMap[this._basetable];
if(!optOutFields) return result; // if no mapping exists, exit early
if (!Array.isArray(optOutFields)) optOutFields = [optOutFields]; // If the map value isn't an array, turn it into one with a single element to use in the for loop below.
var optOut = true;
for (i = 0; i < optOutFields.length; i++) {
if (this._current.getDisplayValue(optOutFields[i]) == "true") {
optOut = true;
break;
} else {
optOut = false;
}
}
if (optOut) {
result.show = true;
result.content = '<div class="alert alert-danger m-0 p-2">' +
'<strong>Important: </strong>Data for this customer <strong>must not</strong> be processed outside of the UK.' +
'<span style="padding-left:20px"><img src="uk-flag.png" height="25px"></span>' +
'</div>',
result.type = "critical",
result.header = "Important:",
result.message = "Data for this customer must not be processed outside of the UK.",
result.image = "uk-flag.png";
} else {
result.show = true;
result.content = '<div class="alert alert-info m-0 p-2">' +
'Data for this customer can be processed outside of the UK.' +
'<span style="padding-left:20px"><img src="world-icon.png" height="25px"></span>' +
'</div>',
result.type = "info",
result.header = "",
result.message = "Data for this customer can be processed outside of the UK.",
result.image = "world-icon.png";
}
return result;
},
type: 'WVNTBannerUtils'
};Behaviour
When creating a new Customer Service Case in Workspace:
- Open a new Case.
- Before saving the record, the UK restriction banner is already displayed (as in the below image).
- This can occur even when the Account field is empty, or event when selected account allows it data to be preocessed out of the UK (this happens before the record is saved).
In Classic UI, this behaviour is not observed.
To me it seems like the Script Include is being instantiated and called while the form is loading in Workspace. I am still even not sure how is this Script Include called (whether by BR, Client Script, etc)
At the point the banner appears, the record may not yet have a saved account reference.
Any insight into this matter would be greatly appreciated.
Thanks in advance,
Kamva
1 REPLY 1
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
40m ago
update your script as this
var WVNTBannerUtils = Class.create();
WVNTBannerUtils.prototype = {
initialize: function(current) {
this._validRecord = false;
this._current = null;
this._table = '';
this._basetable = '';
if (!current || current.isNewRecord() || !current.getValue('sys_id'))
return; // unsaved record -> no banner
this._table = current.getValue('sys_class_name') || current.getTableName();
this._basetable = new TableUtils(this._table).getAbsoluteBase();
var rec = new GlideRecord(this._table);
if (rec.get(current.getUniqueValue())) {
this._current = rec;
this._sys_id = rec.getUniqueValue();
this._validRecord = true;
}
},
_emptyResult: function() {
return { show: false, type: '', header: '', message: '', image: '', content: '' };
},
getBannersForTableRecord: function() {
if (!this._validRecord)
return this._emptyResult();
return this.getOptOutForTableRecord();
},
// Returns 'true', 'false', or '' (unknown / no account / field empty)
_flagValue: function(path) {
try {
var el = this._current.getElement(path);
if (!el || el.nil()) return '';
var v = String(el).toLowerCase();
if (v === 'true' || v === '1') return 'true';
if (v === 'false' || v === '0') return 'false';
} catch (e) {
gs.warn('WVNTBannerUtils: cannot resolve ' + path + ' : ' + e);
}
return '';
},
getOptOutForTableRecord: function() {
var result = this._emptyResult();
if (!this._validRecord) return result;
var optOutMap = {
"sn_customerservice_case": ["account.u_data_outside_eu_opt_out"],
"sn_customerservice_task": ["account.u_data_outside_eu_opt_out", "parent_case.account.u_data_outside_eu_opt_out"],
"customer_account": ["u_data_outside_eu_opt_out", "account_parent.u_data_outside_eu_opt_out", "account_parent.account_parent.u_data_outside_eu_opt_out"],
"customer_contact": ["account.u_data_outside_eu_opt_out"],
"sc_req_item": ["company.u_data_outside_eu_opt_out", "request.company.u_data_outside_eu_opt_out", "company.ref_customer_account.u_data_outside_eu_opt_out", "request.ref_customer_account.company.u_data_outside_eu_opt_out"],
"task": ["company.ref_customer_account.u_data_outside_eu_opt_out", "change_request.company.ref_customer_account.u_data_outside_eu_opt_out"],
"sn_ind_tmt_orm_order": ["account.u_data_outside_eu_opt_out", "contact.account.u_data_outside_eu_opt_out"],
"sn_ind_tmt_orm_order_line_item": ["account.u_data_outside_eu_opt_out", "parent_order.account.u_data_outside_eu_opt_out"],
"interaction": ["account.u_data_outside_eu_opt_out", "contact.account.u_data_outside_eu_opt_out"]
};
var fields = optOutMap[this._table] || optOutMap[this._basetable];
if (!fields) return result;
if (!Array.isArray(fields)) fields = [fields];
var optOut = false, resolved = false;
for (var i = 0; i < fields.length; i++) {
var val = this._flagValue(fields[i]);
if (val === 'true') { optOut = true; resolved = true; break; }
if (val === 'false') resolved = true;
}
if (!resolved) return result; // no account / no data -> no banner
result.show = true;
if (optOut) {
result.content = '<div class="alert alert-danger m-0 p-2">' +
'<strong>Important: </strong>Data for this customer <strong>must not</strong> be processed outside of the UK.' +
'<span style="padding-left:20px"><img src="uk-flag.png" height="25px"></span></div>';
result.type = 'critical';
result.header = 'Important:';
result.message = 'Data for this customer must not be processed outside of the UK.';
result.image = 'uk-flag.png';
} else {
result.content = '<div class="alert alert-info m-0 p-2">' +
'Data for this customer can be processed outside of the UK.' +
'<span style="padding-left:20px"><img src="world-icon.png" height="25px"></span></div>';
result.type = 'info';
result.header = '';
result.message = 'Data for this customer can be processed outside of the UK.';
result.image = 'world-icon.png';
}
return result;
},
type: 'WVNTBannerUtils'
};
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏