- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2023 03:52 AM
Hi Techies,
Greetings. Please help me with the script to create multiple records in asset table.
regards..
Mani
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2023 11:28 PM
Hi @mgjk ,
Below is an example script that demonstrates how to create multiple records in the Asset [alm_asset] table using a GlideRecord in ServiceNow:
// Specify the number of records to create
var numRecordsToCreate = 5;
// Loop to create multiple records
for (var i = 0; i < numRecordsToCreate; i++) {
var asset = new GlideRecord('alm_asset');
// Set field values for the new record
asset.initialize();
// Set values for specific fields
asset.setValue('name', 'Asset ' + (i + 1));
asset.setValue('asset_tag', 'ASSET-' + (i + 1));
// Insert the record into the table
var assetSysID = asset.insert();
// Check if the record was created successfully
if (assetSysID) {
gs.info('Created asset record with sys_id: ' + assetSysID);
} else {
gs.error('Failed to create asset record');
}
}
Thanks,
Ratnakar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2023 11:28 PM
Hi @mgjk ,
Below is an example script that demonstrates how to create multiple records in the Asset [alm_asset] table using a GlideRecord in ServiceNow:
// Specify the number of records to create
var numRecordsToCreate = 5;
// Loop to create multiple records
for (var i = 0; i < numRecordsToCreate; i++) {
var asset = new GlideRecord('alm_asset');
// Set field values for the new record
asset.initialize();
// Set values for specific fields
asset.setValue('name', 'Asset ' + (i + 1));
asset.setValue('asset_tag', 'ASSET-' + (i + 1));
// Insert the record into the table
var assetSysID = asset.insert();
// Check if the record was created successfully
if (assetSysID) {
gs.info('Created asset record with sys_id: ' + assetSysID);
} else {
gs.error('Failed to create asset record');
}
}
Thanks,
Ratnakar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2023 12:47 PM
from sqlalchemy import create_engine, Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# Define the database connection
DATABASE_URL = 'sqlite:///assets.db'
engine = create_engine(DATABASE_URL, echo=True)
# Define the data model
Base = declarative_base()
class Asset(Base):
__tablename__ = 'assets'
id = Column(Integer, primary_key=True)
name = Column(String)
category = Column(String)
value = Column(Integer)
# Create the table if it doesn't exist
Base.metadata.create_all(engine)
# Create a session to interact with the database
Session = sessionmaker(bind=engine)
session = Session()
# Define the list of assets to insert
assets_to_insert = [
Asset(name='Laptop', category='Electronics', value=1200),
Asset(name='Desk', category='Furniture', value=300),
Asset(name='Printer', category='Office Supplies', value=150),
# Add more assets as needed
]
# Insert the assets into the database
session.add_all(assets_to_insert)
session.commit()
# Close the session
session.close()
print("Records inserted successfully!")
Before running the script, make sure to adjust the DATABASE_URL to match your database configuration. Also, modify the Asset class and the assets_to_insert list to match your actual asset table structure and the data you want to insert.