Inbound API capture to Asset table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2024 12:38 PM
I get a JSON payload sent to us by one of our hardware vendors. I was wondering what the best approach here would be, to generate Asset records from this data.
We get a Purchase Order number, Model number, quantity and Asset tags. I need to create a new Asset for each tag.
Am I best off using Flow Designer? Or are there other methods that you would recommend?
Has anyone successfully implemented something similar?
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2024 03:26 PM
HI,
Scripted Approach: If you need more complex logic or have specific requirements that Flow Designer cannot handle, you might consider writing a script using ServiceNow's Table API. You can create a Scripted REST API or a scheduled job that processes the JSON payload. This approach involves writing server-side JavaScript to handle data parsing and record creation. The script would parse the JSON data to extract the Purchase Order number, Model number, quantity, and Asset tags, then iterate over the Asset tags to create new Asset records programmatically.
To create a Scripted REST API in ServiceNow that processes a JSON payload to generate Asset records, follow these steps:
1. Define the Scripted REST API
Navigate to: System Web Services > Scripted REST APIs
Click New to create a new Scripted REST API.
Name: e.g., "Asset Creation API"
API ID: e.g., "asset_creation_api"
API Version: e.g., "v1"
Active: Checked
Save the record.
2. Create the Resource
Navigate to: System Web Services > Scripted REST APIs and select the API you just created.
Scroll down to the "Resources" tab and click New to add a resource.
Name: e.g., "Create Assets"
HTTP Method: POST
Relative Path: /create
Save the record.
3. Write the Script
Navigate to: System Web Services > Scripted REST APIs and select the resource you just created.
Scroll down to the "Script" tab and click Edit to add your server-side script.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// Get the request body
var requestBody = request.body.data;
// Parse the JSON payload
var payload = JSON.parse(requestBody);
var poNumber = payload.PurchaseOrderNumber;
var modelNumber = payload.ModelNumber;
var quantity = payload.Quantity;
var assetTags = payload.AssetTags; // Assume this is an array of tags
// Validate data
if (!poNumber || !modelNumber || !quantity || !assetTags || !Array.isArray(assetTags)) {
response.setStatus(400);
response.setBody({ error: "Invalid input data" });
return;
}
// Create Asset records
var assetGr = new GlideRecord('alm_asset');
for (var i = 0; i < assetTags.length; i++) {
var assetTag = assetTags[i];
assetGr.initialize();
assetGr.purchase_order = poNumber;
assetGr.model_number = modelNumber;
assetGr.asset_tag = assetTag;
assetGr.insert();
}
// Respond with a success message
response.setStatus(201);
response.setBody({ message: "Assets created successfully" });
})(request, response);
Test the Scripted REST API
Navigate to: System Web Services > Scripted REST APIs
Select your API and go to the "Resources" tab.
Click on "Test" next to your resource or use the ServiceNow API Explorer.
Please mark this response as correct or helpful if it assisted you with your question.
Thanks and Regards
Shreedevi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2024 10:42 PM - edited 07-23-2024 07:27 AM
Thank you for your reply! I also looked over this documentation
As it is more closely related to what we need, in terms of generating Assets off of a Purchase Order payload.
Then I realised that this type of import really isn't possible without the full Hardware Asset Managament plugin, there are transforms that are part of that plugin, that happen once the payload is received, generating first the PO, then the assets.
I do appreciate your time, at this moment we don't have the full Hardware Asset Managament plugin, so I think that should be the next step. Do you have any thoughts to that?