The issue is that the clear companies script deletes rows from the ADH00100 table when the relevant database doesn’t exist. This will only be a problem for sites which have SmartList objects created with SmartList Designer, which explains why I haven;t seen the problem more often.
I posted the code which can be used to update the clear companies script, but if you don;t want to maintaina custom version of this script, there are two actions you can take.
If you know of this issue in advance of running the clear companies script, you can build into your process the runing of a script against the table to change all of the CMPANYID entries in the table to the number of a company which does exist:
/* Created by Ian Grieve of azurecurve|Ramblings of a Dynamics GP Consultant (http://www.azurecurve.co.uk) This code is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 2.0 UK: England & Wales (CC BY-NC-SA 2.0 UK). */ UPDATE ['User Smart List Master'] SET CMPANYID = (SELECT TOP 1 CMPANYID FROM SY01500 ORDER BY CMPANYID DESC) FROM ADH00100 AS ['User Smart List Master'] WHERE CMPANYID <> 0 AND (SELECT COUNT(database_id) FROM sys.databases AS ['System Databases'] WHERE ['User Smart List Master'].CMPANYID = ['System Databases'].database_id) = 0 GO
This script is configured to only update the CMPANYID field when the relevant database doesn;t exist or the field is set to 0.