Deleting Demand Records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2015 12:20 AM
One of the most frequently asked questions by customers in Demand Management is how to delete demand data — especially the demo data and some times demands which are past the Screening stage. In this article we are providing a script which can help you in deleting such demand records.
Out-of-box a demand can be deleted only when it is in a stage prior to the Screening stage. Upon entering the Screening stage, a demand gets associated with assessment metric entries. Also, if the demand has stakeholders attached to it, the assessments are sent to the stakeholders by email, and the email messages cannot be revoked. Therefore, a demand that is in the Screening or a subsequent stage is not allowed to be deleted. Also Demand users cannot delete a demand with associated resource plans, decisions or risks but demand manager can delete such records.The business rules "Delete Demand" and "Auto deletion rule for Assessments" abort deletion of such demand records.
If you want to delete a demand and its related records after the demand has entered the Screening or a subsequent stage, regardless of the ramifications, use background scripts (provided below). Note that if a stakeholder tries to open the assessment sent through email after the deletion of the demand, an error is displayed.
To delete a demand using background scripts:
- Navigate to System Definition > Scripts - Background.
- Copy the following script to the field labeled Run script (JavaScript executed on server).
//deleteDemandPreview('SYS_ID');
//deleteDemand('SYS_ID');
function deleteDemand(demandId) {
deleteRelatedRecords(demandId, false);
var demand = new GlideRecord('dmn_demand');
demand.get(demandId);
gs.log('Deleting demand of sys_id' + demand.sys_id);
demand.setWorkflow(false);
demand.deleteRecord();
}
function deleteDemandPreview(demandId) {
deleteRelatedRecords(demandId, true);
gs.log('Deleting demand of sys_id' + demandId);
}
function deleteRelatedRecords(demandId, preview) {
deleteAssessmentRecords(demandId, preview);
deleteRequirements(demandId, preview);
deleteDecisions(demandId, preview);
deleteResourcePlans(demandId, preview);
}
function deleteAssessmentRecords(demandId, preview) {
var results = new GlideRecord('asmt_metric_result');
results.addQuery('source_id', demandId);
results.addQuery('source_table', 'dmn_demand');
results.query();
gs.log(' ------------- Deleting Assessment Metric Results of demand with sys_id' + demandId + '-------------');
while (results.next()) {
gs.log('Deleting Metric result of sys_id - ' + results.sys_id);
if (!preview) {
results.deleteRecord();
}
}
var categoryResults = new GlideRecord('asmt_category_result');
categoryResults.addQuery('source_id', demandId);
categoryResults.addQuery('source_table', 'dmn_demand');
categoryResults.query();
gs.log(' ------------ Deleting Assessment Category Results of demand with sys_id' + demandId + '--------------');
while (categoryResults.next()) {
gs.log('Deleting Category result of sys_id - ' + categoryResults.sys_id);
if (!preview) {
categoryResults.deleteRecord();
}
}
var rec = new GlideRecord('asmt_assessable_record');
rec.addQuery('source_id', demandId);
rec.addQuery('source_table', 'dmn_demand');
rec.query();
gs.log(' ------------ Deleting Assessable Records of demand with sys_id' + demandId + '--------------');
while (rec.next()) {
gs.log('Deleting Assessable record of sys_id - ' + rec.sys_id);
if (!preview) {
rec.deleteRecord();
}
}
}
function deleteRequirements(demandId, preview) {
var req = new GlideRecord('dmn_requirement');
req.addQuery('parent', demandId);
req.query();
gs.log(' ------------- Deleting Requirements of demand with sys_id' + demandId + '-------------');
while (req.next()) {
gs.log('Deleting demand requirement of sys_id - ' + req.sys_id);
if (!preview) {
req.deleteRecord();
}
}
}
function deleteDecisions(demandId, preview) {
var dec = new GlideRecord('dmn_decision');
dec.addQuery('parent', demandId);
dec.query();
gs.log(' ------------- Deleting Decisions of demand with sys_id' + demandId + '-------------');
while (dec.next()) {
gs.log('Deleting demand decision of sys_id - ' + dec.sys_id);
if (!preview) {
dec.deleteRecord();
}
}
}
function deleteResourcePlans(demandId, preview) {
if (pm.isActive('com.snc.resource_management')) {
var plans = new GlideRecord('resource_plan');
plans.addQuery('task', demandId);
plans.query();
gs.log(' -------- Deleting Resource Plans of demand with sys_id' + demandId + '----------');
while (plans.next()) {
gs.log('Deleting resource plan of sys_id - ' + plans.sys_id);
if (!preview) {
plans.deleteRecord();
}
}
}
}
- Substitute SYS_ID with the sys_id of the demand record that you want to delete.
- Uncomment the first line of the script by removing the // characters at the start of the first line in the script, and click Run Script.
The system will generate a preview of records, which will get deleted by the script. - If you decide to proceed, uncomment the second line of the script by removing the // characters at the start of the second line in the script, and click Run Script.
The specified demand and its related records will be deleted.
- 3,000 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2019 12:22 AM
Thank you its working fine
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2024 01:08 PM
Thank you @akankshaaggarwa - this worked for me as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2024 11:58 AM
This script worked perfectly! Thank you!