Validate import set from Payload

tanz
Tera Expert

Hi,

 

I have an transform map which load data to device table when an excel is uploaded. I want to achieve the same functionality in json payload. 

There are two fields in device table, device name and serial number, where serial number is a coalesce and mandatory(this is done through onbefore script). The json should have a functionality if someone is attaching an empty serial number and pushes the  attachment data through postman it should throw an error that Serial number is mandatory. In my current functionality it is creating device with empty Serial number also when pushed through postman or rest api explorer.

"attachments": [
            {
                "payload": "", //base 64 format
                "file_name": "Device.xlsx",
                "content_type": "text/plain",
                "encoding": "base64"
            }
        ]
 
Is there any way to achieve this . Can you please help with script also?
 
 

 

 

3 REPLIES 3

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @tanz ,

 

Did you try DataPolicy ? Also you can use transform script to validate if the json pair has the value which is expected if not insert/update can be ignored with an error message. 

 

I cant think of any way were you can validate the value in Json itself ? I hope this help ...

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Tushar
Kilo Sage
Kilo Sage

Hi @tanz 

 

I think you can write a script for a Scripted REST API resource that validates the incoming JSON payload and handles the creation of device records accordingly.

However, if you want to implement validation directly within a transform script for a Transform Map, you can use a script like the following -

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
    // Check if the 'serial_number' field in the source data is empty
    var serialNumber = source.serial_number;
    if (serialNumber === '' || serialNumber === null) {
        // Log an error message
        log.error('Serial number is mandatory. Skipping record import.');

        // Return null to indicate that the record should be skipped
        return null;
    } else {
        // Return the target record to indicate that it should be created or updated
        return target;
    }
})(source, map, log, target);


Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

 

Ankur Bawiskar
Tera Patron
Tera Patron

@tanz 

you can use Scripted REST API for this.

Did you start on it? if yes then where are you stuck?

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