Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Bulk SLA Creation and Linking to Contracts via Transform Maps

Hafeeza_Shaik
Tera Contributor

Use Case Overview

Automatically create and link Service Level Agreements (SLAs) to Contracts in ServiceNow by processing bulk data via a Transform Map. The process ensures that SLAs are created if they do not exist and are immediately visible in the SLAs related list of the Contract record.

Approach

  • Create a Transform Map to process imported SLA and Contract data from an import set.

  • In the onBefore Script, create the SLA if it does not already exist.
  • In the onAfter Script, link the SLA to the Contract if both exist.

  • Ensure all new and existing SLAs are linked to the respective Contracts.

  • SLAs will be visible in the Contract SLAs related list after the import.


Implementation Steps

  1. Prepare Import Data

    • The import data must contain:

      • u_service_level_agreement → SLA Name

      • u_contract_name → Contract Name

  2. Create the Transform Map

    • Source Table: Import table containing SLA and Contract details.

    • Target Table: Any relevant target table (logic in scripts).

    • Add the following scripts:


onBefore Script – Link SLA to Contract

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

    /* 1. Get SLA Name from the source data (Excel import) */
    var slaName = source.u_service_level_agreement;
    if (!slaName) {
        return "";
    }

    /* 2. Initialize SLA sys_id variable */
    var slaSysId = '';

    /* 3. Query contract_sla table by SLA name to check if SLA already exists */
    var slaGR = new GlideRecord('contract_sla');
    slaGR.addQuery('name', slaName);
    slaGR.query();
    gs.info("Entered SLA creation check in onBefore script");

    /* 4. If SLA does not exist, create a new SLA record */
    if (!slaGR.next()) {
        gs.info("SLA not found. Creating a new SLA record.");
        var newSla = new GlideRecord('contract_sla');
        newSla.initialize();
        newSla.name = slaName;
        newSla.duration = '01:00:00';
        newSla.type = 'SLA';
        newSla.target = 'response';
        newSla.collection = "sn_tech_product_support_case";

        /* 5. Set SLA schedule based on specific SLA names */
        if (slaName == "Ping 24 x 7 Maintenance Contract" || slaName == "Ping 24 x 7 Maintenance SLA") {
            gs.info("SLA Name: " + slaName + " - Setting schedule source to 'no_schedule'");
            newSla.schedule_source = 'no_schedule';
        } else {
            newSla.schedule = '8e8e45272bbcaa10073af7986e91bf17';
            gs.info("SLA schedule set to: " + newSla.schedule);
        }

        /* 6. Insert new SLA record */
        newSla.insert();
        gs.info("New SLA created with sys_id: " + newSla.sys_id);
    }

})(source, map, log, target);

onAfter Script – Linking SLA to Contract

(function executeRule(source, target, map, log, isUpdate) {
    /* -------------------------------------------------------------------
       PURPOSE:
       This onAfter script runs after the SLA record is created. It links
       the SLA to the correct Contract so that it is visible in the
       Contract’s related list.

       LOGIC:
       - Find the SLA by Short description from the source data.
       - Find the Contract record by a matching field (e.g., Contract number).
       - Insert a link in the relationship table to connect SLA to Contract.
    ------------------------------------------------------------------- */

    var slaGR = new GlideRecord("contract_sla");
    slaGR.addQuery("short_description", source.u_short_description);
    slaGR.query();

    if (slaGR.next()) {
        var contractGR = new GlideRecord("ast_contract");
        contractGR.addQuery("short_description", source.u_contract_name);
        contractGR.query();

        if (contractGR.next()) {
            var relGR = new GlideRecord("ast_contract_sla"); // Relationship table
            relGR.initialize();
            relGR.contract = contractGR.sys_id;
            relGR.sla = slaGR.sys_id;
            relGR.insert();
        }
    }
})(source, target, map, log, isUpdate);

3. Run the Import

  • Upload Excel file into the import set.

  • Execute the Transform Map.


4. Verify

  • SLA records appear in the contract_sla table.

  • SLAs are linked to Contracts in the SLAs related list.

Key Points

  • source.data comes from the imported Excel file.

  • Short description is used as the unique key to avoid duplicate SLA creation.

  • onBefore handles SLA creation.

  • onAfter establishes the relationship between SLA and Contract.

  • This ensures SLAs are automatically visible in the Contract’s Related Lists after import.


 

0 REPLIES 0