How to restrict user to delete records when related lists is not empty ?

venugopal s
Tera Contributor

Hi Everyone,

I have written delete ACL on cmn_location where class is not Workplace Location, but I need to add condition when Associated tables have any records then deletion rule should not be applicable. 

Thanks

venugopals_0-1680851522273.png

 

@Ankur Bawiskar 

12 REPLIES 12

DUGGI
Giga Guru

To add a condition to your delete ACL on cmn_location that prevents deletion when associated tables have records, you can modify your existing script to include a check for associated records. Here is an example script that includes this check:

// Get the current record being deleted
var location = current;

// Check if the location class is not Workplace Location
if (location.getValue('sys_class_name') != 'cmn_location_workplace') {

  // Check if there are any associated records in the Location Association table
  var locationAssoc = new GlideRecord('cmn_location_association');
  locationAssoc.addQuery('location', location.sys_id);
  locationAssoc.query();
  if (locationAssoc.hasNext()) {
    // Cancel the delete if associated records exist
    gs.addErrorMessage('Associated records exist. Cannot delete location.');
    current.setAbortAction(true);
  }
}

In this script, we first get the current cmn_location record being deleted. We then check if the location class is not Workplace Location. If it is not, we then query the cmn_location_association table to check if there are any associated records for this location. If there are, we cancel the delete action and display an error message indicating that associated records exist.

 

You can modify this script to include additional checks for other associated tables as needed.

Hi 

@DUGGI ,
It's getting multiple error messages based on associated records. how to restrict to single error message. 
Thanks

Hi @DUGGI ,
How can we hide delete button instead of error message.
Thanks