How to Validate Mandatory JSON Fields in a ServiceNow Widget

JVINAY
Tera Contributor
Hi Team,
 
I have created a Service Portal widget to perform Intake readiness validation.
 
I have already completed validation of Business Application fields from the cmdb_ci_business_app table.
 
My next requirement is to validate JSON values stored in the Cloud Intake Task.
 
The JSON payload is available in the field:
 
u_namespace_request_schema
 
Example JSON:
 
{
"preferred_destination":"Cloud",
"request_type":"standard",
"application_name":"Carelon Research Snowflake",
"cir_request_id":"CIR0003499",
"car_intake_id":"CLOUD0006428",
"apm":"APM1082819",
"costcenter":"6037140100",
"dr-tier":"84"
}
 
In my widget server-side script, I am reading the JSON and parsing it using:
// Central Intake JSON Validation
 
var jsonReady = false;
var jsonMessages = [];
 
var taskGR = new GlideRecord('u_cloud_intake_task');
taskGR.addQuery('u_cloud_task_type', 'provision');
taskGR.orderByDesc('sys_created_on');
taskGR.query();
 
while (taskGR.next()) {
  var intakeGR = taskGR.u_intake.getRefRecord();
 
data.debugJson =
'Business App = ' + intakeGR.getValue('cmdb_ci_business_app') +
' | Input = ' + input.apmSysId;
data.debugApp =
    'Task=' + taskGR.number +
    ' | App=' + intakeGR.getDisplayValue('cmdb_ci_business_app') +
    ' | AppSysId=' + intakeGR.getValue('cmdb_ci_business_app') +
    ' | Input=' + input.apmSysId;
//if (intakeGR.getValue('cmdb_ci_business_app') != input.apmSysId)
    //continue;
 
var jsonText = taskGR.getValue('u_namespace_request_schema');
    if (!jsonText)
        continue;
 
   try {
 
    var jsonData = JSON.parse(jsonText);
 
    jsonMessages = [];
 
    if (!jsonData.apm_id)
        jsonMessages.push('APM ID missing');
 
    if (!jsonData.cir_request_id)
        jsonMessages.push('CIR Request ID missing');
 
    if (!jsonData.car_intake_id)
        jsonMessages.push('Cloud Intake ID missing');
 
    if (!jsonData.namespace_name)
        jsonMessages.push('Namespace Name missing');
 
    if (!jsonData.application_name)
        jsonMessages.push('Application Name missing');
 
    jsonReady = (jsonMessages.length === 0);
 
} catch (e) {
 
    jsonReady = false;
    jsonMessages.push('Parse Error: ' + e.message);
 
}
    break;
}
 
data.jsonResult = jsonReady ? 'READY' : 'NOT READY';
 
data.jsonSummary =
    'Validated key JSON attributes successfully.';
}
}
 
})();
Additional requirement:
 
Currently the JSON validation is working, but the result appears to be static because it is validating the latest Cloud Intake Task JSON instead of the JSON associated with the selected Business Application.
 
My requirement is that the validation should be dynamic.
 
Example:
 
Business Application A
→ Validate JSON from the Intake Task related to Business Application A
 
Business Application B
→ Validate JSON from the  Intake Task related to Business Application B
 
I do not want a generic/latest task validation. I want the JSON validation result to be based on the selected Business Application and its associated Intake Task.
 
Could you please suggest the recommended way to map a selected Business Application (cmdb_ci_business_app) to the correct Cloud Intake Task (u_cloud_intake_task) and then validate the JSON stored in u_namespace_request_schema?

 

JVINAY_1-1786694542201.png

 

2 REPLIES 2

yashkamde
Giga Sage

Hello @JVINAY ,

 

In this line :
var taskGR = new GlideRecord('u_cloud_intake_task');
...
taskGR.query();

 

> The more better than looping through all tasks and checking in script, the filter happens at the DB query level.

var taskGR = new GlideRecord('u_cloud_intake_task');
taskGR.addQuery('u_cloud_task_type', 'provision');
taskGR.addQuery('u_intake.cmdb_ci_business_app', input.apmSysId); // dot-walk filter
taskGR.orderByDesc('sys_created_on');
taskGR.setLimit(1); // only need the most recent matching task
taskGR.query();

 

> Also your validation checks:

if (!jsonData.apm_id) jsonMessages.push('APM ID missing');
if (!jsonData.namespace_name) jsonMessages.push('Namespace Name missing');

But your sample JSON has apm:APM1082819, not apm_id. And there's no namespace_name key at all in the example you shared.

 

Try this code :

// Central Intake JSON Validation
var jsonReady = false;
var jsonMessages = [];

var taskGR = new GlideRecord('u_cloud_intake_task');
taskGR.addQuery('u_cloud_task_type', 'provision');
taskGR.addQuery('u_intake.cmdb_ci_business_app', input.apmSysId);
taskGR.orderByDesc('sys_created_on');
taskGR.setLimit(1);
taskGR.query();

if (taskGR.next()) {
    var jsonText = taskGR.getValue('u_namespace_request_schema');

    if (!jsonText) {
        jsonMessages.push('No JSON payload found on Intake Task ' + taskGR.number);
    } else {
        try {
            var jsonData = JSON.parse(jsonText);

            if (!jsonData.apm)
                jsonMessages.push('APM ID missing');
            if (!jsonData.cir_request_id)
                jsonMessages.push('CIR Request ID missing');
            if (!jsonData.car_intake_id)
                jsonMessages.push('Cloud Intake ID missing');
            if (!jsonData.application_name)
                jsonMessages.push('Application Name missing');
            // Confirm whether namespace_name is a required key in the real schema
            // if (!jsonData.namespace_name)
            //     jsonMessages.push('Namespace Name missing');

            jsonReady = (jsonMessages.length === 0);
        } catch (e) {
            jsonReady = false;
            jsonMessages.push('Parse Error: ' + e.message);
        }
    }
} else {
    jsonMessages.push('No Intake Task found for selected Business Application');
}

data.jsonResult = jsonReady ? 'READY' : 'NOT READY';
data.jsonMessages = jsonMessages;
data.jsonSummary = jsonReady
    ? 'Validated key JSON attributes successfully.'
    : jsonMessages.join(', ');

 

If my response helped mark as helpful and accept the solution.

Ankur Bawiskar
Tera Patron

@JVINAY 

did you take help of any AI tool like co-pilot etc and check what suggestion it gave to fix this?

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