Validate Configuration Item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2023 09:34 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2023 10:07 AM
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:
-
First, you need to retrieve the CI records from the CMDB table in ServiceNow. You can do this using GlideRecord queries.
-
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.
-
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
// ...
}
}
Regards,
Riya Verma