logo

NJP

Attachments in ServiceNow Record Producer Mapped Attachment Variable Duplicated in Activity Log

Import · Nov 10, 2023 · video

this video we'll look at the attachment variable on a record producer is duplicating attachments when submitting a request and this problem is described in this uh knowledge article with a very interesting spelling of attachments so first let's let's look at the problem um let's fire up so I have an instance of uh employee Center here and I have a record producer with two attachment variables these attachment variables are mapped to a field or to attachment fields on a Case record so I'm going to go ahead and upload my attachments document one document two and when I submit I see it happening twice here the upload right document one and then here's document one again and in a different order inter interestingly document two document two again so this is very confusing for users um and you you probably never notice this if you're working an itsm or CSM but if you're in hrsd which tends to be very document intensive um this is undoubtedly uh not uh passed without notice the notice of your of some of your users um so sort of annoyingly uh they doesn't give you a workaround it just says this is expected Behavior Uh um and let's look at why this is the expected Behavior so what's actually happening when you upload those attachments are if we go back to our table here and we go to the question answer table this is where the record producer variables are stored uh so we can see that the uh two docu here's the answers to the questions right and then the answer is in the value field here maybe open one of these up so uh this is the question right this is the uh record producer variable document two and table CIS ID uh I'm sorry no uh table CIS ID this is the uh talent management case table and this is the CIS ID on that and then this is the CIS ID of the attachment okay uh so that's 7 C just let's remember that 7 C and if we go over to the CIS attachment table um oh I should I did this a few times before I should probably have deleted some of these but um let's look for 7c this should be document two uh this one here so this one is 7c and you see this is another document two over here and just ignore these other ones these are tests I was doing uh before so uh it's uh this one here is the real one this is uh 7c here we can do this copy S ID and do aad kind of thing so there's 7 C and then we have our other one let's grab that CIS ID uh that's 004 so if we go to the actual case and I have that fired up over here here's my case Okay uh here's document two and actually we could well see if I copy the link address put that in here here right we can see this is the 004 so this is the other one so what it's doing is it's first uploading it to the well it's uploading to the attachment table and then putting the CIS ID of the first attachment into the variable value and then it's copying the attachment and putting the second uh the copied attachments CIS ID into the uh field value okay so now the workaround to stop this from happening um so basically I have two business rules and here I am so I'm on the question answer table and I have two business rules here first one is clean maap attachments uh so this is running on insert it's a before business rule and basically it's saying uh the condition is uh when it's a map to when the field is mapped or when the variable is mapped I should say and and when the variable type is attachment that's 33 then run the script and the script is just getting the value of the which is the CIS ID of the attachment and it's going to the attachment table and deleting it simple as that so we'll turn that on and I have the code for this I'll put a link in my I wrote an article on it I have the code in there you can copy it uh then the second business rule is an asynchronous business rule replace mapped attachment CIS ID uh so what we want to do is we want to uh replace the CIS ID of the and this one is sort of optional uh but we want to replace the CIS ID of the uh copied attachment of the attachment that's going to remain and not the deleted attachment into the variable value uh so that if you want to display that on the form or for whatever reason it's the data is is a little cleaner that way it's maps to an actual attachment and not an attachment that's been deleted so this one is a asynchronous business rule uh also on insert and same conditions right it's only runs when it's a uh variable that's mapped and it's an attachment variable and so here we are going to the parent table or the the table that this record is being created on and we're going and grabbing um the actual CIS ID of the uh right so it goes to the field to go find that value of the of the actual uh attachment that's remaining so then it's going to just take that attachment and that CIS ID and copy it into the or write it into the uh variable value field now uh because this is an asynchronous business rule and we're doing a current update which is sort of a no no not really supposed to do that because you could trigger the workflows and you could just uh you could trigger you know the business rule again and then you just end up in a loop uh but I do have set workflow to false here so that uh so that doesn't happen the reason why this has to happen in an asynchronous business rule is I found when I did it in the same business rule as before as the one we're cleaning it uh the attachment had not been copied into the into the record producer yet um I'm sorry into the field yet so this wasn't working maybe if you made that first uh business rule and after business rule uh that might do it um I'm not sure I didn't I didn't test that but that's that's possibility it could make the whole thing a little bit cleaner so anyway let's turn this on uh so this is what replaces the value Okay so so now let's uh I tell you what let's go back to our assis attachments let's delete these things so when we do our second run here we won't get confused okay [Music] and let's even go back to our question answer table let's get rid of these two delete that okay so now let's run it again document two we do submit and so now I only see it once in this table and if we go over here back to our question answer table uh let's look at say document one is now A7 right and let's go look at our assist attachment table and here's document one this ID and there it is A7 uh so that's it uh just running having those two business rules will clear up that problem and your users will will be a lot less confused

View original source

https://www.youtube.com/watch?v=l5zHo5sIwrw