Background script/Fix script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2024 03:21 AM
Hi Community,
How to check that newly created variable is attached to the existing RITMS? If it's not attached the how to attach new variable to existing RITMS.
Thanks,
Manu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 09:26 PM
hi @Manu143
Here are general steps to check if a newly created variable is attached to existing RITMs:
1. Navigate to Catalog Definitions
- From the ServiceNow dashboard, navigate to Service Catalog > Catalog Definitions > Catalog Item.
2. Find the Catalog Item
- Find and open the catalog item that should be associated with the new variable.
3. Inspect the Variable List
- Once the catalog item is open, there should be a related list for variables. Look at that list to see if the new variable you created is there. If it’s not there, it’s not associated with that specific catalog item.
4. Check Variable Sets (if used)
- If the catalog item uses variable sets, you should check to make sure the new variable is part of these sets. Variable sets are a group of variables that can be shared between catalog items.
5. Review Existing RITMs
- If the variable is attached but you want to make sure it is referenced by existing RITMs, you would have to look at each RITM and ensure the variable is being used. This is often impractical due to volume and because RITMs are records of a transaction at a point in time, and historically they won’t be updated with new variable information.
If the variable is not attached, here’s how to attach it:
1. Access the Catalog Item
As before, navigate to Service Catalog > Catalog Definitions > Catalog Item, and open the specific catalog item you want to add the variable to.
2. Edit the Catalog Item
- Click on the ‘Edit’ option to modify the catalog item.
3. Add a Variable or Variable Set
- If the new variable is stand-alone, you can add it directly to the catalog item by clicking “New” in the variables related list. Fill in the required information for the variable.
- If the variable is part of a variable set, you can add the variable set to the catalog item by selecting the relevant set from the “Variable Sets” related list.
4. Save Your Changes
- Once you’ve added the variable or variable set, save your changes to ensure the variable will be included in any new RITMs created from that catalog item.
NOTE: Using a background script in ServiceNow to associate a variable with existing Request Items (RITMs) can be very challenging due to the way data is structured. Normally, variables are associated with Catalog Items and are instantiated as variable records for each generated RITM. Once an RITM is created, its variable data is typically considered static because it represents the state of user input at the time of RITM creation.
However, if you need to modify RITMs to include a new variable, and you fully understand the implications of making such a change in production data, you could use a background script. Keep in mind that my explanation is theoretical, as executing such a script would be a highly unusual scenario and is not recommended due to data integrity concerns.
Here is a pseudocode example of how a background script would look like:
// Identify the catalog item and the associated variable set (if applicable).
var catalogItemSysId = ‘SYS_ID_OF_CATALOG_ITEM’; // Sys_id of the relevant catalog item
var variableName = ‘new_variable_name’; // The name of your new variable
// Find all RITMs that were generated from the specific catalog item.
var ritmGR = new GlideRecord(‘sc_req_item’);
ritmGR.addQuery(‘cat_item’, catalogItemSysId);
ritmGR.query();
while (ritmGR.next()) {
// Check if the new variable already exists for this RITM.
var scItemVarGR = new GlideRecord(‘sc_item_option_mtom’);
scItemVarGR.addQuery(‘request_item’, ritmGR.sys_id);
scItemVarGR.addQuery(‘sc_item_option.item_option_new.name’, variableName);
scItemVarGR.query();
if (!scItemVarGR.hasNext()) {
// Create a new variable record for the RITM if it doesn’t exist.
var itemOptionNewGR = new GlideRecord(‘item_option_new’);
itemOptionNewGR.addQuery(‘name’, variableName);
itemOptionNewGR.query();
if (itemOptionNewGR.next()) {
var newOptionLink = new GlideRecord(‘sc_item_option_mtom’);
newOptionLink.initialize();
newOptionLink.request_item = ritmGR.sys_id;
newOptionLink.sc_item_option = itemOptionNewGR.sys_id;
newOptionLink.insert();
// Optionally set a value for the new variable if necessary.
} else {
gs.info(“Variable with name '” + variableName + “’ not found”);
}
}
}
gs.info(“Completed adding new variable to RITMs.”);
Please mark this as helpful or Correct if this solves your query.
Thanks & Regards
Deepak Sharma