Cannot convert null to an object - Request from Catalog Item

AndreasRitter
Mega Guru

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:

 

(function executeRule(current, previous /*null when async*/ ) {

    var test = "Value ist Null"

    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 (key in grRitm.variables) {
            var v = grRitm.variables[key];

            gs.info('Checkpoint Variablen: ' + v.getGlideObject().getQuestion().getLabel() + ': ' + v.getDisplayValue() );

            //  Exclude headers and other formating types from text generation
            switch (v.getGlideObject().getType()) {
                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
                    // Do not include all the above from wn text
                    break;
                default:
                    wn += v.getGlideObject().getQuestion().getLabel() + ': ' + v.getDisplayValue() + '\n';
                    gs.info('Checkpoint Variablen: ' + v.getGlideObject().getQuestion().getLabel() + ': ' + v.getDisplayValue() );
            }
        }
       
        // 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
        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();


        // finally, if my own Short Description is not yet the same, update current request again
        // so that short descriptions of case and request are in sync
        if (current.getValue("short_description") != shortDesc) {
            current.setValue("short_description", shortDesc);
            gs.info("need to update request short desc");
            current.update();
        }
    }

})(current, previous);
 
Thanks for your Support 😉
Best Regards
Andreas
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@AndreasRitter 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@AndreasRitter 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

Thank you for your quick reply, what do you mean by MRVS?

@AndreasRitter 

MRVS -> multi row variable set

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Robert H
Mega Sage

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