Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Seperate the string using script

BharatiK
Tera Contributor

Hi,

 

I want to create an incident using script with Short Description: "ServiceNow Associate" and attach that incident to 2 child incidents.

Where 1st child Incident should contain short description : "ServiceNow" and 2nd Child Incident Should contain "Associate".

 

How we can achieve this?

 

 

 

Thank you..!!

 

4 REPLIES 4

Pooja2998
Mega Sage
@BharatiK try this script

var parentIncident = new GlideRecord('incident');
parentIncident.initialize();
parentIncident.short_description = 'ServiceNow Associate';
parentIncident.insert();

var childIncident1 = new GlideRecord('incident');
childIncident1.initialize();
childIncident1.short_description = 'ServiceNow';
childIncident1.parent = parentIncident.sys_id;  
childIncident1.insert();

var childIncident2 = new GlideRecord('incident');
childIncident2.initialize();
childIncident2.short_description = 'Associate';
childIncident2.parent = parentIncident.sys_id;  
childIncident2.insert();

gs.info('Parent Incident Sys ID: ' + parentIncident.sys_id);
gs.info('First Child Incident Sys ID: ' + childIncident1.sys_id);
gs.info('Second Child Incident Sys ID: ' + childIncident2.sys_id);
 
If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Pooja.

sunil maddheshi
Tera Guru

@BharatiK 

You can achieve this using a Script Include, Business Rule, or Background Script in ServiceNow. Below is a BG script:

 

// Create the Parent Incident
var parentIncident = new GlideRecord('incident');
parentIncident.initialize();
parentIncident.short_description = "ServiceNow Associate";
parentIncident.insert();

// Check if Parent Incident is created successfully
if (parentIncident.getUniqueValue()) {
    gs.info("Parent Incident Created: " + parentIncident.number);

    // Create the First Child Incident
    var childIncident1 = new GlideRecord('incident');
    childIncident1.initialize();
    childIncident1.short_description = "ServiceNow";
    childIncident1.parent = parentIncident.sys_id; // Set Parent Incident Reference
    childIncident1.insert();

    if (childIncident1.getUniqueValue()) {
        gs.info("Child Incident 1 Created: " + childIncident1.number);
    }

    // Create the Second Child Incident
    var childIncident2 = new GlideRecord('incident');
    childIncident2.initialize();
    childIncident2.short_description = "Associate";
    childIncident2.parent = parentIncident.sys_id; // Set Parent Incident Reference
    childIncident2.insert();

    if (childIncident2.getUniqueValue()) {
        gs.info("Child Incident 2 Created: " + childIncident2.number);
    }
} else {
    gs.info("Failed to create Parent Incident");
}

 

Please mark correct/Helpful, if this helps you

Viraj Hudlikar
Tera Sage
Tera Sage

Hello @BharatiK 

 

For this question you will get multiple answer but here is one of the ways for the same:

// Function to create an incident with a given short description and optional parent ID
function createIncident(shortDescription, parentId) {
    var incident = new GlideRecord('incident');
    incident.initialize();
    incident.short_description = shortDescription;
    if (parentId) {
        incident.parent_incident = parentId;
    }
    incident.insert();
    return incident.sys_id;
}

// Create the parent incident
var parentIncident = new GlideRecord('incident');
parentIncident.initialize();
parentIncident.short_description = 'ServiceNow Associate'; // You can change this to any short description
parentIncident.insert();

// Split the short description of the parent incident into words
var words = parentIncident.short_description.split(' ');

// Create child incidents based on the words from the parent incident's short description
for (var i = 0; i < words.length; i++) {
    createIncident(words[i], parentIncident.sys_id);
}

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Ankur Bawiskar
Tera Patron
Tera Patron

@BharatiK 

Is this a business requirement or some scripting you are trying to learn.

if it's the 2nd case then unless you start you won't learn.

if it's the 1st case then I don't think it's a valid business use-case

If my response helped please mark it correct and close the thread so that it benefits future readers.

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