Preventing deletion of attachments in ServiceNow
this video is going to look at attachments in service now and how to prevent their deletion uh so a common problem or a problem I've run into is a user comes to me and says uh somebody deleted my attachment or sometimes they'll even admit that they accidentally deleted their attachment and they want to know how can they get that back and I have to give them the sad news that uh out of the box once an attachment is deleted service now it's it's gone forever and there's no getting it back I mean I guess you could ask for a backup uh and depending on how important that attachment is uh you might actually go through with that um but it would be nice if we uh had a way to not delete attachments to begin with so that you could get them back um which honestly in my opinion I mean should be the out- of thebox way that it works uh because everything else is logged and stored and uh and uh and be able to be retrieved uh but attachments for some reason um you can just get rid of them and uh nobody nobody knows about it so let's take a look at the outof the boox behavior I am in an incident here I have an attachment field uh if I go ahead and add an attachment to this field okay we see the attachment is in there and I go to the notes or to the activity formatter and I can see the attachment here great uh if I go to the CIS attachment table I can see this uh expert training programs legal terms blah blah blah and the table name here is ZZ YY incident the ZZ YY is because it's in a in a Detachment field and not anywhere um in the actual uh attachment in the attachment section okay so we go back here if I oh and I guess one last thing to show you is that if we go look at the XML we see that let's see this thing is called utest or I think I even called it U file attachment I think that was the uh I must have done that in form designer and so I have the ID of the attachment perfect now I delete the attachment out of here and let me just refresh this well okay it deletes from there um before hitting refresh it's still here but I think it's actually oh no you can still get to it um once I let's go over to the CIS attachments okay it is gone out of here and if I go back to the incident uh it is gone out of here as well so no trace of it ever being here however amazingly if I go to the XML and I go to my attachment field what did I say that was yeah U file attachment boom here's the CIS ID still there a kind of ghost CIS ID because there's no actual um CIS ID in the CIS attachment table for this it's it's gone um similarly if I stick an attachment up here see did that go all the way I think so yep so we see our attachment there we'll reload the form um right the attachment is there and here we can see the record of me uploading it but um then I go and remove it and I refresh and same deal no trace of it actually being there so this is the behavior that we are trying to um avoid we want to be able to still have a record of this file being uploaded uh in the activity formatter so I can go back and retrieve it if I need to um so I have on my website I have an article on this prevent deletion of attachments and service now so we create a business rule on the sis attach attachment table we uh put it for delete and then we can give it some conditions to only fire on certain tables and we'll add this to the script so let's highlight this oops okay we'll go back here and let's go back to the assist attachments we'll do business rules let call this keep a deleted attachments this will be Advanced uh this is a before it's on delete and we're going to say when the table name um is incident or the table name is ZZ YY incident and what we're going to do is fire this stuff and let's just leave it at that we'll submit that oops I'm in the Human Resources core scope no I want to be in the uh Global scope uh okay and we also have a problem here so line 35 column one right I got a problem with this maybe I'm missing that okay I got that to save but I'm in the wrong scope so let's do an in uh first of all let's change our scope to Global okay we'll do an insert and stay okay perfect now we'll go back to all of our business rules s by updated let's get rid of the one in Human Resources core okay now let's go try it out so we'll go back to our incident okay we'll add an attachment okay and we see it here like normal and now I will delete this attachment and we see the um field modification signal here a little icon uh because part of what that script does is goes and cleans up the actual field here test so it gets that CIS ID out of there let's refresh and we can still see the attachment here even though it's gone out of here and if I go to the XML my field keep forgetting what I called it here it is you file attachment one so the CIS ID is gone out up there so that's why the field shows up as uh empty and actually it shows up as empty also in the other case the out of the box case where the CIS ID is there but it doesn't exist um so it's empty as it should be uh but it's still down here and if we go to the CIS attachments oops we go to the CIS attachments t we're going to see that here it is it's still here with zzy y y incident so there is basically no well the only way to delete the attachment uh would probably be have a CIS admin come in here and up here and yeah and delete it this way okay now let's look at the other situ situation where we attach a file into the regular attachments so I have this file called profile PDF now I put that up there so that's here I go here and I remove it and I'll even let's refresh the screen and it's still here and I can still get to it so and if we look at it in the CIS attachments I can see it's here profile PDF and so what the script does is it changes the table name to ZZ y y so that it won't be visible in the attachment section on the form uh but it'll still be associated with the incident table with that CIS ID so let's just take a look at that business rule whoops that's the one I deleted okay um so essentially we have two cases here right uh one is where the table name is starts with ZZ y y which means it's a field attached M and the other one is it's a regular attachment so if it's a field attachment we do this remove attachment which we'll look at in a second if it's just a a regular attachment then we just rename the table field like we just saw and we stick a zz YY in front of it and we update the um C attachment record and then we delete uh I'm sorry we abort the delete action so that it actually doesn't delete from the table um it looks like I do it here again which don't probably don't need that twice okay so then let's look at the first case where it is a in an attachment field so it runs this function remove attachment and this will go get the table name um does a Glide record on the table name uh on the uh gets the record in a Glide record and then it's going to go to CIS dictionary and look up all of the file attachment Fields um on that table then it's going to go through all of those fields and check to see if the CIS ID that is being deleted is the value of any of those fields if it is it removes it um and remember we saw that uh kind of heartbeat symbol there about the the field got updated so it goes and cleans the CID out of the field attachment out of the attachment field um and then it uh it saves the record and then it aborts the delete so it uh the the attachment is still in the CIS attachment table as we saw uh but it's just removed the CIS ID is removed out of that attachment field um so that's how it works uh pretty simple solution uh but can save you a lot of headaches and explaining to users that their files are completely gone uh because they can always go back to the activity uh log and go go find it and download it
https://www.youtube.com/watch?v=7fNuttDVtWc