The Now Platform® Washington DC release is live. Watch now!
on ‎08-02-2020 10:19 PM
Hi there,
A frequently asked question concerning Virtual Agent: How to upload any type of file within a chat with the Virtual Agent. So far, this was limited to using the User Input Image Picker… with the Paris-release, Eureka: A new User Input File Picker has been added which replaces the former Image Picker.
Though, how to use this new User Input File Picker?
User Input File Picker
From the ServiceNow Docs:
"Replaces the former Image Picker control in the Virtual Agent Designer palette. You can specify the type of file that your users can upload, either image files or all file types. An anti-virus scan runs automatically on all files uploaded."
Behavior User Imput File Picker
When simply applying the User Input File Picker, you don't really see anything happening after selecting a file. Though, because we know files attached to records are actually stored as attachments, let's have a look at the Attachments [sys_attachment] table.
Filtering on the most recent attachments, notice that a record has been created on the Table name 'sys_cs_conversation_task' and the File name exactly is the file which you just selected with the User Input File Picker.
Relating the File to a (new) record
So the User Input File Picker does work. Though, the file is only added to the Attachments table. So what actually does the vaInputs give us? Might that be useful? When applying gs.info(vaInputs.file_picker.toString()), this provides us with something like:
https://instance-name.service-now.com/api/now/v2/cs/media/axdxPNJDEQomEheiCRdcimWIJdmqAYkbOVdGevPqloRBcrpjAhpmydqjVYMGqRVS
Searching the ServiceNow Docs for Virtual Agent Script, there's a useful vaSystem property mentioned: vaSystem.attachToRecord().
vaSystem.attachToRecord(String mediaId, String tableName, String sysId)
String tableName, String sysId, both look logical. mediaId, would that be what we just wrote to the System Log? Indeed it is 🙂
Solution
Out basic test Topic for the User Input File Picker looks like:
In this case, we are just creating an Incident. Obviously, attaching the file to an existing record would also work. The Utility Script Action contains:
(function execute() {
vaSystem.attachToRecord(vaInputs.file_picker.getValue(), 'incident', vaInputs.create_incident);
})()
vaInputs.file: file_picker being the Variable Name for User Input File Picker
vaInputs.create_incident: create_incident being the Variable Name for Utility Action
Result
If we open the just created Incident, notice that the file from the User Input File Picker indeed has been attached to the Incident:
---
And that's it actually. Hope you like it. If any questions or remarks, let me know!
If this post helped you in any way, I would appreciate it if you hit bookmark or mark it as helpful. Interested in more articles, blogs, videos, and Share projects on Virtual Agent I published? - Virtual Agent |
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
Hallelujah - they finally updated this to not default to images...
(FYI all, up until Paris, the Image Picker does support non-image files despite its name. In Windows, on the file explorer popup, you just need to change the file type from Images to All. And if you select a non-image, VA will return a broken image icon to the user, but it will save and attach the file just fine.)
Hi mark,
Could you please tell me how can I add attachment to service request as I have created it using script action and not just utility action?
For some reason, the attachment is not being added to the Incident. Prior to the file picker, I ask the users if they need to add an attachment and if they select yes, then it asks them to upload the file. Once they upload, the Incident creates and gives them the link to the record but it never attaches the file. What am I doing wrong?
Hi Laura,
I used this in script action after create incident. This might help you.
That's pretty much what I have in my script action - where "Add to record" is the variable name of my file picker
Do you see any errors in the system log? Or when using the Virtual Agent preview debugging? Do you also see if the attachments are added to the attachment table, and maybe just nog correctly attached to the ?incident?
Maybe this Topic Block will also be of help:
Virtual Agent Topic Block: File Picker
Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP
---
Thank you Mark! You're correct that the attachments were added to the attachment table but not to the Incident. I created the version of file picker in your article and made that a topic block and used it in our flow and it worked like a charm. I still don't understand the behavior of why the attachment wasn't getting put into the Incident table but alas, we found a work around 🙂 Thanks for your help.
Hi,
Very nicely explained.
I would like to know is it possible to get sys_id of attachment from vaInputs.file_picker.toString()?
Hi Mark,
Thanks for this brilliant Article. This has helped bring over the attachment form VA to my incident.
I have a custom conversation in which i am asking user the question if they would like to add further attachments after the attach the first. It just a loop which takes the conversation back into the file picker(add attachment) part.
I have created script action after the incident creation but it only picks up the very latest attachments that was sent to the file picker. How do i get all the attachments that were added by the user.
Thanks
What you are experiencing sounds logical to me. Because you are looping, without actually writing any attachments to the record? That's being done after "No" I assume? So yes, then you would only have the last attachment.
Kind regards,
Mark
Looping is very important for us because it allows our users to easily upload multiple attachments at once - a much-requested feature of ours.
Very easy to do - what you're missing is simply putting the file upload node and the script node (for attaching the file to the corresponding record) inside the loop. Take a look at my screenshot below for context.
If it helps, this is the question (boolean input) I have which initiates and/or bypasses the attachment loop:
Do you want to add one or more attachment(s) (i.e. screenshot with error message)?
(Note: I just have this flexible wording to keep it no-code. If you wanted it to be a bit more robust, you could easily use a script variable as a counter and an extra line of code so you can make the question more dynamic - i.e. the first time "Do you want to add one or more attachment(s)?" and then if it loops, the next time(s) it can say "Do you want to add another attachment?")
Hi Chris,
Please have a look at the scree shot below:
what I have done here is that after the creation of the incident i have created the loop for attachments with the script node. This works perfect , in the backend all the attachments are getting attached to the created incident.
When i look at your loop for attachments that is happening before there record creation, so how are you managing to attach those multiple files in the backend.
As Mark said doing looping for attachments and using the script before the record creation it will always overwrite the last attachment.
I would still love to find out if there is a way we can loop through attachments before record creation and then in the back end all attachments are pinned to the record created for the agent.
Thanks
So yes, you are absolutely right that if you want to loop to allow multiple attachments, you basically need to do it AFTER the incident is created.
I'm not saying there's absolutely no way to do it before the incident is created, but whatever clever solution can do it, it'd be a lot less straightforward and a lot more complex - and in my opinion, the minimal benefit is not worth the extra effort.
Thanks you Mark and Chris. If I manage to find a solution i will definitely link it here. If you ever come across anything like that please do so.
Regards
How does this attach the file to the end users open incident/request?
Hello,
How can I create a record from details uploaded in the File Picker?
Thank you.