Skip to content

Commit 00572ba

Browse files
authored
Merge branch 'main' into Hacktober-2025-5th_code_branch
2 parents 4459df2 + af88ba1 commit 00572ba

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
When importing or processing Configuration Items (CIs), especially hardware assets, missing model or manufacturer data can cause CI creation failures or incomplete relationships.
2+
This script handles that automatically by:
3+
* Checking if a manufacturer already exists in the core_company table.
4+
* Checking if a model already exists in the cmdb_model hierarchy.
5+
* Creating the manufacturer and/or model records if they are missing.
6+
7+
How It Works
8+
9+
1. Extracts model and manufacturer names from the data source (source.u_model and source.u_manufacturer).
10+
2. Calls getOrCreateManufacturer():
11+
* Searches the core_company table by name.
12+
* Creates a new company record if not found.
13+
3. Calls getOrCreateModel():
14+
* Searches the cmdb_model table (including child tables).
15+
* If no match exists, inserts a new record in cmdb_hardware_product_model.
16+
4. Logs each action (found, created, or failed) for debugging and auditing.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var modelName = source.u_model;
2+
var manufacturerName = source.u_manufacturer;
3+
4+
function getOrCreateModel(modelName, manufacturerName) {
5+
try {
6+
var manufacturerSysId = getOrCreateManufacturer(manufacturerName);
7+
if (!manufacturerSysId) {
8+
gs.error("MODEL SCRIPT: Failed to find or create manufacturer: " + manufacturerName);
9+
return null;
10+
}
11+
12+
// Query cmdb_model to check if any existing model (including child tables) exists
13+
var modelGr = new GlideRecord('cmdb_model');
14+
modelGr.addQuery('name', modelName);
15+
modelGr.addQuery('manufacturer', manufacturerSysId);
16+
modelGr.query();
17+
18+
if (modelGr.next()) {
19+
gs.info("MODEL SCRIPT: Found existing model: " + modelGr.getUniqueValue());
20+
return modelGr.getUniqueValue();
21+
} else {
22+
// Create in child table: cmdb_hardware_product_model
23+
var newModel = new GlideRecord('cmdb_hardware_product_model');
24+
newModel.initialize();
25+
newModel.setValue('name', modelName);
26+
newModel.setValue('manufacturer', manufacturerSysId);
27+
var newModelSysId = newModel.insert();
28+
29+
if (newModelSysId) {
30+
gs.info("MODEL SCRIPT: Created new hardware model: " + newModelSysId);
31+
return newModelSysId;
32+
} else {
33+
gs.error("MODEL SCRIPT: Failed to insert new model for " + modelName);
34+
return null;
35+
}
36+
}
37+
} catch (e) {
38+
gs.error("MODEL SCRIPT: Error in getOrCreateModel(): " + (e.message || e));
39+
return null;
40+
}
41+
}
42+
43+
function getOrCreateManufacturer(name) {
44+
try {
45+
var companyGr = new GlideRecord('core_company');
46+
companyGr.addQuery('name', name);
47+
companyGr.query();
48+
49+
if (companyGr.next()) {
50+
gs.info("MODEL SCRIPT: Found manufacturer: " + companyGr.getUniqueValue());
51+
return companyGr.getUniqueValue();
52+
} else {
53+
companyGr.initialize();
54+
companyGr.setValue('name', name);
55+
//companyGr.setValue('manufacturer', true); // Ensure it’s marked as manufacturer
56+
var newMfrSysId = companyGr.insert();
57+
58+
if (newMfrSysId) {
59+
gs.info("MODEL SCRIPT: Created new manufacturer: " + newMfrSysId);
60+
return newMfrSysId;
61+
} else {
62+
gs.error("MODEL SCRIPT: Failed to insert new manufacturer: " + name);
63+
return null;
64+
}
65+
}
66+
} catch (e) {
67+
gs.error("MODEL SCRIPT: Error in getOrCreateManufacturer(): " + (e.message || e));
68+
return null;
69+
}
70+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The triggerDataSource() function eliminates the need for manually executing a Data Source from the UI.
It programmatically triggers the import of a predefined Data Source record and loads the associated data into an Import Set table.
2+
This function is typically used in:
3+
* Scheduled Script Executions
4+
* Flow Designer Actions.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
triggerDataSource: function() {
2+
3+
var dataSourceSysId = gs.getProperty('ds.tag.based.sys.id'); //Store the sysId of DataSource from system property
4+
5+
var grDataSource = new GlideRecord('sys_data_source');
6+
if (grDataSource.get(dataSourceSysId)) {
7+
var loader = new GlideImportSetLoader(); //OOB Method to load
8+
var importSetRec = loader.getImportSetGr(grDataSource);
9+
var ranload = loader.loadImportSetTable(importSetRec, grDataSource);
10+
importSetRec.state = "loaded";
11+
importSetRec.update();
12+
return importSetRec.getUniqueValue();
13+
}
14+
},
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Get KB Article Permalink
2+
Flow Action which will take KB Article Number as input and returns the latest version of Permalink. This URL will stay constant always even if the KB Article is updated with new version.
3+
4+
**Input** : KB Article Number (Type : String)
5+
6+
**Script Step** : Generates the Permalink URL for KB Article (see the script.js file in this folder)
7+
8+
**Output** : Permalink (Type : URL)
9+
10+
**Usage** : This can be used in multiple scenarios where the KB Article Link/URL is required. It can either be in notifications, scripts, Integrations, etc. Since this Permalink is always fixed and works even if the KB Article is updated to new version, there is no maintainance required.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(function execute(inputs, outputs) {
2+
3+
var instanceURL = gs.getProperty('glide.servlet.uri').toString();
4+
var permalink = instanceURL + 'kb?id=kb_article_view&sysparm_article=' + inputs.kb_number;
5+
outputs.kb_article_permalink = permalink;
6+
7+
})(inputs, outputs);

0 commit comments

Comments
 (0)