Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

Error when fetching data on server using a function

Leejcharlto
Tera Contributor

Im getting this error: "ErrorConditon 'Condition: current.problem_id.changes() || current.operation() == 'delete'' in business rule 'Update Problem Incident Count' on incident: INC0008111 evaluated to null; skipping business rule" when i wrap the fetch in a function in the server script. It doesnt happen on the original fetch but only when I call  the server script to update the db. If i just let the script run down to the fetch without wrapping it in a function I dont have this issue.


Heres my server code:

(function() {
  var gr = new GlideRecordSecure('incident');
    var updateSuccessful = false;

    if (input && input.action == "saveUpdate" && input.recordSysId) {
        if (gr.get(input.recordSysId)) {
            var records = input.records;
            for (var i = 0; i < records.length; i++) {
                var record = records[i];
                if (record.recordId == input.recordSysId) {
                    gr.setValue('short_description', record.shortDescription);
                    updateSuccessful = gr.update() != null;
                    break;
                }
            }
        }
        data.updateSuccessful = updateSuccessful
        data.action = null
        data.recordSysId = null
        return
    }

  function fetchData() {
        data.records = [];
        gr.addEncodedQuery('caller_idDYNAMIC90d1921e5f510100a9ad2572f2b477fe');
        gr.query();
        while(gr.next()) {
            data.records.push({
                recordId: gr.getValue("sys_id"),
                number: gr.getValue("number"), 
                shortDescription: gr.getValue("short_description"),
                priority: gr.getValue("priority"),
                updated: gr.getValue("sys_updated_on")
            });
        }
    }
    fetchData()
})();

 

8 REPLIES 8

Ankur Bawiskar
Tera Patron

@Leejcharlto 

my thoughts

-> Wrapping the fetch in a function is not the cause

-> Fix the BR condition to null‑check problem_id

Use (current.problem_id && current.problem_id.changes()) || current.operation() == 'delete'

-> Verify the BR is on incident and not running on operations where current isn’t a full record

-> ensure your script only updates valid incident records (gr.get(...) and table check)

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

I didnt think wrapping it in a function could cause the issue but its the only time i get the error. Very strange.

Also, the BR is OOB on the incidents table.

@Leejcharlto 

where is this script written?

share screenshots

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

The original script I posted was server script. Heres the server and client scripts. 

If I remove the function wrapper around the data fetch then the problem disappears.

Leejcharlto_0-1787052026809.png