How to make sure deleted record is cleared in other places where is it is referenced?

Ksnow
Tera Contributor

Hello,

@Amit Gujarathi @Anil Lande @Ankur Bawiskar @Community Alums 

 

How can we trace if data is not cleared even though record is deleted and it was referenced in other places?

Example: From company table "Pune" record is deleted and it is referenced in user table for many records (user profiles).

My requirement is to identify where "Pune" was used and after record deletion, was it cleared value from all the user profiles using script.

If it doesn't clear (left with sys_id), how to clear it using the script?

Can anyone please help me?

Appreciate your help!

 

4 REPLIES 4

Mayur2109
Kilo Sage
Kilo Sage

Hi @Ksnow ,

 

Please refer below community post.

https://www.servicenow.com/community/developer-forum/how-to-clear-the-sys-id/m-p/2035886/page/2 

 

Please check and Mark Helpful and Correct if it really helps you.

Regards,
Mayur Shardul

Amit Gujarathi
Giga Sage
Giga Sage

HI @Ksnow ,
I trust you are doing great.
Please find the script ouline fr the same.

// Step 1 & 2: Trace references before deletion
var companySysId = 'sys_id_of_Pune_record'; // Replace with actual sys_id
var userTable = new GlideRecord('user'); // Assuming 'user' is the table name
userTable.addQuery('company', companySysId); // Replace 'company' with the actual reference field name
userTable.query();

while(userTable.next()) {
    gs.info('User record with reference: ' + userTable.sys_id);
    // Add more logging or actions as needed
}

// Step 3: Delete the record
// This can be done through the standard delete UI or via a script

// Step 4 & 5: Verify and clear references post-deletion
userTable = new GlideRecord('user');
userTable.addQuery('company', companySysId);
userTable.query();

while(userTable.next()) {
    userTable.company = ''; // Clear the reference
    userTable.update();
    gs.info('Cleared reference in user record: ' + userTable.sys_id);
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Thanks @Amit Gujarathi 

 

Just to confirm if we are not sure in which table we have referenced the company, could have used in many tables.

In that case above script works? anything to be modified.

Thanks

Ksnow
Tera Contributor

@Amit Gujarathi 

Hope you're doing well!

Could you please let me know how to find where (in tables) company was referenced ?

Or do we have to find out manually and then glide the table then delete the record?

Please advise.

Appreciate your time and efforts!

Thanks