Deletion should be aborted when there are related records

Pranavi07
Tera Expert

Hi all,

usually we have OOB functionality "Service_offering_commitment" where we related service_offering with service_commitment.

we have designed a similar set up "Business_service_commitment" where we related Business service cmdb_ci_service with service_commitment.

 

Requirement is that we need a removal message when we delete/remove record from business_service_commitment table but the additional requirement here is... When we open business service and try removing service commitments from it, it should check If there are any service offerings attached to this business service and then in these service offerings, there are service commitments (the same commitments which we are trying to remove from business service), it should show error message showing the following commitments are associated to service offerings of this business service. Delete them first and then delete the commitments from business service.

If there are no such dependencies, user should be able to remove commitments from business service and get message like following commitments removed from BS.

 

I tried with some script, but error message is showing up many times with in the loop and couldn't progress here. Please help me with the script

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @Pranavi07 ,
I trust you are ddoing great.
Please find the below updated code for the same.

var hasMatchingCommitments = false;
var matchingCommitments = [];

var serOff = new GlideRecord("service_offering");
serOff.addQuery('parent', current.u_business_service);
serOff.query();

while (serOff.next()) {
    var OffCom = new GlideRecord("service_offering_commitment");
    OffCom.addQuery('service_offering', serOff.getUniqueValue());
    OffCom.addQuery('service_commitment', current.u_service_commitment);
    OffCom.query();

    if (OffCom.next()) {
        hasMatchingCommitments = true;
        matchingCommitments.push(serOff.name + " (" + current.u_service_commitment.name + ")");
    }
}

if (hasMatchingCommitments) {
    var errorMessage = "There are Service Commitments in the following Service Offerings: " + matchingCommitments.join(", ") + ". Delete those first to delete the Service Commitments from this Business Service.";
    gs.addErrorMessage(errorMessage);
    current.setAbortAction(true);
} else {
    gs.addInfoMessage("The commitment has been removed from the Business Service.");
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

3 REPLIES 3

Tony Chatfield1
Kilo Patron

Hi, unfortunately with no clear details of the script you tried, or where you tried it, the community is not in a position to provide feedback regarding your configuration. Perhaps you could update this thread with details of your code so that it can be reviewed.

Based on your post I would think a simple glide query of the related table, with an if next() condition that contains your warning message and the abort method.

Hi @Tony Chatfield1 ,
Sorry I missed adding script. Please find below

  var serOff = new GlideRecord("service_offering");
    serOff.addQuery('parent', current.u_business_service);
    serOff.query();
    while (serOff.next()) {
        var OffCom = new GlideRecord("service_offering_commitment");
        OffCom.addQuery('service_offering', serOff.getUniqueValue());
        OffCom.addQuery('service_commitment', current.u_service_commitment);
        OffCom.query();
        if (OffCom.next()) {
            gs.addErrorMessage("There are Service Commitments with name" + current.u_service_commitment.name + " in the Service Offering " + serOff.name + " Delete those first to delete the Service Commitments from this BS");
            current.setAbortAction(true);
        } else {
            gs.addInfoMessage("The commitment has been deleted from Business service");
        }

    }

This is a before delete BR on the business_service_commitment (the M2m table for business service and service commitments)

Amit Gujarathi
Giga Sage
Giga Sage

HI @Pranavi07 ,
I trust you are ddoing great.
Please find the below updated code for the same.

var hasMatchingCommitments = false;
var matchingCommitments = [];

var serOff = new GlideRecord("service_offering");
serOff.addQuery('parent', current.u_business_service);
serOff.query();

while (serOff.next()) {
    var OffCom = new GlideRecord("service_offering_commitment");
    OffCom.addQuery('service_offering', serOff.getUniqueValue());
    OffCom.addQuery('service_commitment', current.u_service_commitment);
    OffCom.query();

    if (OffCom.next()) {
        hasMatchingCommitments = true;
        matchingCommitments.push(serOff.name + " (" + current.u_service_commitment.name + ")");
    }
}

if (hasMatchingCommitments) {
    var errorMessage = "There are Service Commitments in the following Service Offerings: " + matchingCommitments.join(", ") + ". Delete those first to delete the Service Commitments from this Business Service.";
    gs.addErrorMessage(errorMessage);
    current.setAbortAction(true);
} else {
    gs.addInfoMessage("The commitment has been removed from the Business Service.");
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi