- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2025 02:25 AM
Hi all,
we have the problem that when collecting all used variables from a catalog item, a Cannot convert null to an object error occurs.
In the catalog iterm variable and variable set are used.
As long as the script part from “switch ‘ to ’break” is commented out, I at least get the variable values via gsinfo.
How can I find out which variable is causing the problem?
Here is our script:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2025 02:41 AM
things to check
1) your catalog item has MRVS in it?
If yes then it might be breaking your script
2) Also did you try with simple catalog item which doesn't have MRVS?
3) may be some variable is having null value or variable is undefined
try this once
(function executeRule(current, previous /*null when async*/ ) {
var wn = "Variablen (Fragen und Antworten) vom Kunden:\n-----\n";
var caseNumber = current.parent.getRefRecord();
var grRitm = new GlideRecord('sc_req_item');
grRitm.get('request', current.sys_id);
if (caseNumber.canWrite()) {
for (var key in grRitm.variables) {
var v = grRitm.variables[key];
if (!v) {
gs.info('Variable "' + key + '" is null or undefined');
continue;
}
var glideObj = (typeof v.getGlideObject === "function") ? v.getGlideObject() : null;
if (!glideObj) {
gs.info('Variable "' + key + '" has no GlideObject');
continue;
}
var question = (typeof glideObj.getQuestion === "function") ? glideObj.getQuestion() : null;
var label = (question && typeof question.getLabel === "function") ? question.getLabel() : '(No Label)';
var type = (typeof glideObj.getType === "function") ? glideObj.getType() : '(No Type)';
var displayValue = (typeof v.getDisplayValue === "function") ? v.getDisplayValue() : '(No Display Value)';
gs.info('Checkpoint Variable: key=' + key + ', label=' + label + ', type=' + type + ', value=' + displayValue);
// Exclude headers and other formatting types from text generation
switch (type) {
case 11: // Label
case 12: // Break
case 19: // Container Start
case 20: // Container End
case 24: // Container Split
case 32: // Rich Text Label
case 33: // Attachment
break;
default:
wn += label + ': ' + displayValue + '\n';
}
}
// Add price if existing
if (grRitm.price != 0) {
wn += "\n\n" + grRitm.price.getLabel() + ": " + grRitm.price.getDisplayValue() + "\n";
gs.info('Checkpoint Price: ' + grRitm.price);
}
caseNumber.setWorkflow(false);
// Append catalog item to case short description
var shortDesc = "";
if (caseNumber.getValue("short_description"))
shortDesc = caseNumber.getValue("short_description") + " (" + grRitm.getValue("short_description") + ")";
else
shortDesc = grRitm.getValue("short_description");
caseNumber.short_description = shortDesc;
gs.info('Checkpoint Case Shortdescription: ' + shortDesc);
// Append variable summary to case description
var text = caseNumber.description != '' ? caseNumber.description + '\n\n' + wn : wn;
caseNumber.description = text;
gs.info('Checkpoint Description: ' + text);
caseNumber.update();
// Sync request short description if needed
if (current.getValue("short_description") != shortDesc) {
current.setValue("short_description", shortDesc);
gs.info("need to update request short desc");
current.update();
}
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2025 02:41 AM
things to check
1) your catalog item has MRVS in it?
If yes then it might be breaking your script
2) Also did you try with simple catalog item which doesn't have MRVS?
3) may be some variable is having null value or variable is undefined
try this once
(function executeRule(current, previous /*null when async*/ ) {
var wn = "Variablen (Fragen und Antworten) vom Kunden:\n-----\n";
var caseNumber = current.parent.getRefRecord();
var grRitm = new GlideRecord('sc_req_item');
grRitm.get('request', current.sys_id);
if (caseNumber.canWrite()) {
for (var key in grRitm.variables) {
var v = grRitm.variables[key];
if (!v) {
gs.info('Variable "' + key + '" is null or undefined');
continue;
}
var glideObj = (typeof v.getGlideObject === "function") ? v.getGlideObject() : null;
if (!glideObj) {
gs.info('Variable "' + key + '" has no GlideObject');
continue;
}
var question = (typeof glideObj.getQuestion === "function") ? glideObj.getQuestion() : null;
var label = (question && typeof question.getLabel === "function") ? question.getLabel() : '(No Label)';
var type = (typeof glideObj.getType === "function") ? glideObj.getType() : '(No Type)';
var displayValue = (typeof v.getDisplayValue === "function") ? v.getDisplayValue() : '(No Display Value)';
gs.info('Checkpoint Variable: key=' + key + ', label=' + label + ', type=' + type + ', value=' + displayValue);
// Exclude headers and other formatting types from text generation
switch (type) {
case 11: // Label
case 12: // Break
case 19: // Container Start
case 20: // Container End
case 24: // Container Split
case 32: // Rich Text Label
case 33: // Attachment
break;
default:
wn += label + ': ' + displayValue + '\n';
}
}
// Add price if existing
if (grRitm.price != 0) {
wn += "\n\n" + grRitm.price.getLabel() + ": " + grRitm.price.getDisplayValue() + "\n";
gs.info('Checkpoint Price: ' + grRitm.price);
}
caseNumber.setWorkflow(false);
// Append catalog item to case short description
var shortDesc = "";
if (caseNumber.getValue("short_description"))
shortDesc = caseNumber.getValue("short_description") + " (" + grRitm.getValue("short_description") + ")";
else
shortDesc = grRitm.getValue("short_description");
caseNumber.short_description = shortDesc;
gs.info('Checkpoint Case Shortdescription: ' + shortDesc);
// Append variable summary to case description
var text = caseNumber.description != '' ? caseNumber.description + '\n\n' + wn : wn;
caseNumber.description = text;
gs.info('Checkpoint Description: ' + text);
caseNumber.update();
// Sync request short description if needed
if (current.getValue("short_description") != shortDesc) {
current.setValue("short_description", shortDesc);
gs.info("need to update request short desc");
current.update();
}
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2025 02:47 AM
Thank you for your quick reply, what do you mean by MRVS?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2025 02:51 AM
MRVS -> multi row variable set
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2025 04:12 AM - edited 05-28-2025 04:12 AM
Hello @AndreasRitter ,
It's hard to tell without knowing the details of your Catalog Item configuration, but here is a piece of code that will skip any invalid variables, thus preventing the issue:
for (key in grRitm.variables) {
var v = grRitm.variables[key];
if (!v.getGlideObject() || !v.getGlideObject().getQuestion()) {
continue;
}
// remaining code
}
Regards,
Robert