Update child record if cmdb relationship is deleted

Richard Thomas2
Tera Contributor

Hi Experts,

I need to update a child record if the relationship with parent is deleted on the cmdb_rel_ci table. I need to untick this box.

find_real_file.png

I have this script which checks for other relationships and then should untick the box but I cant get it firing. Can anyone advise?


    

 

Thanks as always

Richard

2 REPLIES 2

Mathew Hillyard
Mega Sage

Hi Richard,

Your screenshot contains what might potentially be customer-sensitive information, so you may want to obscure such references. In addition, code snippets would be easier to read if included in an indented format.

Firstly whilst the amount of code (which I assume is in a Business Rule) is relatively small it's something that is better called from a Script Include for the purposes of troubleshooting and code reusability. 

If you're running business logic where the trigger is the deletion of a relationship, then the Business Rule should be on the CI Relationship table with a When value of delete. You should also be able to shift some of the logic to either the BR Filter or Conditions to limit when it runs as much as possible, as the CI Relationship table could be massive. It is also recommended to run this BR with a when value of async as the current record does not need to wait for the script's completion.

Your pasted code as a few errors and potential improvements, e.g.

  • The Configuration Item table has a Class dictionary entry. Its table value is not "class" but "sys_class_name"
  • The "this" variable is not required when calling functions within a business rule (but will be in a script include, hint hint!)
  • You don't need to convert an array to a string in order to pass it into a GlideRecord query
  • If your child GR table contains a false (unticked) value for field u_supports_important_business, then filter out records that already have this as false before returning the results of the query, otherwise you're potentially returning a record that already has the right value, trying to change it to the same value, and updating the record (which won't get updated anyway as nothing has changed)
  • If you're looking for the presence of one or more records add a <glide_variable>.setLimit(1) to the GR query - way more efficient than returning all the records

I would also recommend using the Script Debugger rather than logging statements using gs.log() - same result but no need to delete all the log statements once tested or hard-code sys_ids 🙂

Take a look and let me know if you need any more information.

BR

Mat

Richard Thomas2
Tera Contributor

Thanks Mat - I'll have look. Thanks for taking the time to reply it's much appreciated.

Kind regards

 

Richard