The CreatorCon Call for Content is officially open! Get started here.

Validate Configuration Item

Jake Adams
Tera Contributor

Hi,

 

I have created an Import API and I have a Configuration field mapping in it.

 

I want to do a validation on the Source CI value i.e.) The Source CI must be a valid CI from the CMDB table.

 

How to validate this?

1 REPLY 1

Riya Verma
Kilo Sage

Hi @Jake Adams 

Hope you are doing great.

 

To validate the Source CI value in your Import API configuration against the CMDB table, you can follow these steps:

  1. First, you need to retrieve the CI records from the CMDB table in ServiceNow. You can do this using GlideRecord queries.

  2.  When processing each record from your Import API, compare the Source CI value in the record with the CI records you retrieved from the CMDB. You can use GlideRecord queries to check if the Source CI exists in the CMDB table.

  3. If the Source CI value matches a valid CI in the CMDB, proceed with the import process. If not, log an error or take appropriate action based on your business requirements.

Reference script to implement validation in import API script :

// Loop through the Import API records
while (importApi.hasNext()) {
    var importRecord = importApi.next(); // Get the next record from the Import API

    // Get the Source CI value from the Import API record
    var sourceCi = importRecord.getValue('source_ci');

    // Check if the Source CI exists in the CMDB records
    var isValidSourceCi = false;
    while (cmdbRecords.next()) {
        var cmdbCi = cmdbRecords.getValue('ci_name'); // Adjust the field name as per your CMDB structure
        if (sourceCi == cmdbCi) {
            isValidSourceCi = true;
            break; // Found a match, no need to continue searching
        }
    }

    // Perform validation based on the result
    if (isValidSourceCi) {
        // Source CI is valid, proceed with import logic
        // ...
    } else {
        // Source CI is not valid, handle the error as per your business requirements
        // You can log an error, skip the import, or take other actions
        // ...
    }
}
 
 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma