Seperate the string using script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 03:05 AM
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..!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 03:17 AM - edited 02-19-2025 03:18 AM
Regards,
Pooja.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 03:21 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 03:51 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 04:18 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader