for catalog creation and dynamic data source learning purpose

keshav77
Tera Contributor

In ServiceNow some time we get project where we need to create a Service Catalog item in ServiceNow. This catalog item is designed to dynamically insert data into a target table—in my case, the API Endpoint table. is the target table.

The key requirement is that the data should be passed directly from the catalog item to the target table in real time. Since multiple users might submit requests simultaneously, using a single static data source could lead to conflicts or data integrity issues. To address this, I implemented a dynamic data source using Business Rules on the Requested Item (RITM) table.

Here's how the solution works:

  1. Catalog Item Setup:

    • I created a catalog item named "Catalog Your API Inventory Item".
    • This item is configured to trigger a Business Rule when a request is submitted.
  2. Dynamic Data Source Creation:

    • The Business Rule dynamically creates a Data Source and Import Set.
    • It then triggers the Import Set to move data from a staging table to the target table using a Transform Map.
    • The Transform Map ID is passed dynamically to ensure the correct mapping.
  3. Error Handling:

    • If certain fields (e.g., the API field) are empty or invalid, we prevent the data from being submitted.
    • This validation is handled using an onBefore Client Script.
    • A dedicated field in the staging table captures error messages for each row.
  4.  Pass the error message of ignored field in RITM variable 

 

Below is the code--

 

business rule on requested item table--

*condition on your item

keshav77_0-1752950650553.png

code you need to create a dynamic data source and import set.

keshav77_1-1752950783182.png

keshav77_3-1752950865603.pngkeshav77_4-1752950911285.pngkeshav77_5-1752950976937.png

 now time  for on before script--

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {


    var errors = [];
    var rowNum = source.getDisplayValue('sys_import_row') || 'unknown row';

    // Validation checks
    if (!source.u_api_version)
        errors.push("[" + rowNum + "] Missing required field: Version");

    if (!source.u_api_name)
        errors.push("[" + rowNum + "] Missing required field: API Name");

    // If any errors found, log and skip the row
    if (errors.length > 0) {
        var msg = errors.join("; ");
        gs.info('Validation Error: ' + msg);  // Log to system log

       //option store error in a custom field on the import row
        source.u_error = msg;

        ignore = true;  // Skip this row
        //error = true;   // Mark as error
        //error_message = msg;  // Set error message for the row
    }




})(source, map, log, target);
 
 
NOW AND NOW last step-
 
(function runTransformScript(source, map, log, target, import_set) {

    var importSetId = import_set.sys_id;
    gs.info('OnComplete: Processing import set ' + importSetId);

    var allErrorsByRitm = {};
    var rowGR = new GlideRecord('u_catalog_api');
    rowGR.addQuery('sys_import_set', importSetId);
    rowGR.addNotNullQuery('u_error');
    rowGR.addNotNullQuery('u_ritm');
    rowGR.query();

    while (rowGR.next()) {
        var ritmId = rowGR.u_ritm.toString();
        var errorMsg = rowGR.u_error.toString();

        if (!allErrorsByRitm[ritmId]) {
            allErrorsByRitm[ritmId] = [];
        }
        allErrorsByRitm[ritmId].push(errorMsg);
    }

    // Update each RITM
    for (var ritmId in allErrorsByRitm) {
        var ritmGR = new GlideRecord('sc_req_item');
        if (ritmGR.get(ritmId)) {
            ritmGR.description = 'testing is going good';
            ritmGR.variables.row_count = allErrorsByRitm[ritmId].join('\n');
 
            // ritmGR.work_notes = 'Import Errors:\n' + allErrorsByRitm[ritmId].join('\n');
            ritmGR.update();
            gs.info('Updated RITM: ' + ritmId);
        } else {
            gs.warn('RITM not found: ' + ritmId);
        }
    }
 
 
If like this content please mark helpful

 

 

 

 

 

0 REPLIES 0