
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
As ServiceNow administrators, we occasionally encounter situations where data needs to be adjusted due to mistakes or systemic cleanup efforts. One such challenge arises when modifying the contract model of an active contract in the ast_contract table. Normally, this field is locked from editing once a contract is active, even for administrators.
Why This Happens
The contract model (contract_model field) links a contract to its model definition in the cmdb_contract_product_model table. When a contract becomes active, ServiceNow enforces restrictions to preserve data integrity. While this is an excellent safeguard, it can become a roadblock if:
- The wrong contract model was selected during data entry.
- A data cleanup effort requires realigning contract models.
- Standardizing contract model assignments across multiple records is needed for reporting or operational consistency.
In such scenarios, manual editing through the UI isn't an option—even for admins. The only way to resolve this is by using a fix script.
The Solution: A Fix Script
Here’s a simple yet powerful script to help you update contract models in bulk. This script takes an array of contract number and contract model name pairs, validates the existence of each, and updates the contract_model field accordingly.
/**
* Script to update contract models in the ast_contract table.
* Use this script when active contracts need their contract_model updated.
*/
// Input: Array of contract number and contract model pairs
var contractPairs = [
{ contractNumber: "CONTRACT12345", contractModelName: "Model Name Example 1" },
{ contractNumber: "CONTRACT67890", contractModelName: "Model Name Example 2" }
];
// Validate the input array
if (!contractPairs || contractPairs.length === 0) {
gs.error("No contract pairs provided. Please provide at least one contract number and model name pair.");
} else {
contractPairs.forEach(function(pair) {
var contractNumber = pair.contractNumber;
var contractModelName = pair.contractModelName;
if (!contractNumber || !contractModelName) {
gs.error("Missing contract number or contract model name for one of the pairs.");
return;
}
var isContractFound = false; // Flag to check if a matching contract exists
var isContractModelFound = false; // Flag to check if a matching contract model exists
var contractGR = new GlideRecord('ast_contract');
var contractModelGR = new GlideRecord('cmdb_contract_product_model');
var contractModelSysId = null;
// Find the contract in the ast_contract table
contractGR.addQuery('number', contractNumber);
contractGR.query();
if (contractGR.next()) {
isContractFound = true; // Contract exists
} else {
gs.error("No contract found with the number: " + contractNumber);
}
// Find the contract model in the cmdb_contract_product_model table
contractModelGR.addQuery('name', contractModelName);
contractModelGR.query();
if (contractModelGR.next()) {
isContractModelFound = true; // Contract model exists
contractModelSysId = contractModelGR.getValue('sys_id');
} else {
gs.error("No contract model found with the name: " + contractModelName);
}
// Proceed only if both the contract and contract model are found
if (isContractFound && isContractModelFound) {
contractGR.setValue('contract_model', contractModelSysId);
contractGR.update();
gs.info("Contract updated successfully for contract number: " + contractNumber);
} else {
gs.error("Update aborted for contract number: " + contractNumber + ". Either the contract or the contract model was not found.");
}
});
}
How It Works
- Input: You provide an array of objects, each containing a contractNumber and contractModelName.
- Validation: The script checks if the contract exists in the ast_contract table and if the contract model exists in the cmdb_contract_product_model table.
- Update: If both are found, it updates the contract_model field of the contract record to reference the correct contract model.
When to Use This Script
- A cleanup effort is required to realign or standardize contract models.
- An error occurred during contract setup, and the wrong model was chosen.
- Contracts need to be updated programmatically as part of a larger data transformation.
Final Thoughts
As ServiceNow admins, we often encounter scenarios where systemic safeguards, while necessary, can make certain changes cumbersome. Fix scripts, like the one above, are a powerful tool in our toolkit to address these edge cases. Just remember to test thoroughly in a development instance before deploying to production and to document your changes for future reference.
Happy scripting! Let me know how this worked for you in the comments below or share your own fixes and hacks! 😊
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.