Automating Screen Captures with ServiceNow
foreign [Music] it's Justin thank you for watching and welcome to my house that's Justin's house in this video I'm gonna have a little fun and show you something a little different a little geeky and we are going to be doing some bash scripting and screenshotting of web pages and attaching it to a servicenow record using the attachment API now let me explain what the heck I'm up to and why I'm doing this you may not know but I have an application built on servicenow that crawls the servicenow store that keeps track of all the updates and occasionally applications go away from the servicenow store whatever reason I'm not here about why I just know that they're in my database and then I can no longer get to them so when I see them in my database they kind of show up like you're seeing here with this n a um and that's just because of the way that um if I can't find a particular attribute in the application it shows up as n a or not applicable meaning I couldn't find it right that's just a Justin convention there's nothing to do with servicenow you can see it there in the version as well now if we scroll down we see that it's Nana here too so at some point this URL appeared in the site map or appeared in my database but when you go to it it's no longer actually showing anything so I just clicked on it and you can see here the application that you were looking for was not found well there's two problems with this one is it's sitting in my database and two is that it's taking time every time I crawl the store to go look at this thing and to find something that's not there so I wanted to do two things I wanted to inactivate it which I accomplished by adding an active flag on the application and if this scenario happens I go ahead and inactivate it so it doesn't show anymore you'll see that here in a second the second thing I did is I said I want a screenshot of what the the crawler saw when it did that crawl so I had to get creative and I went back to using my um my bash command that actually does the crawling and pulls the content I said well I know there's an option or a switch on there to do an actual screenshot so here I've got my server up this is my mid server I've remoted into it and uh it's running in a mid container and I'm just going to copy and paste the you or the the the command that does the uh screenshot Command right so I'm just going to right click here and edit and we're going to paste and of course that doesn't work because you guys are watching let's try this it worked earlier when I pasted uh let's see control or command C over here I'm running on a Mac trying to get over the windows so it's kind of a fun little combination of things here there we go all right paste it over so let's dissect this a little bit it's what's running the Chromium browser you can see there at the top it's in headless mode that's important I am basically saying Hey I want you to take a screenshot using this application URL all right so that's the real simple simple piece of it we didn't actually need to put in the application URL so let's just backspace this layer a little bit and we want to come down here and we're just going to copy this URL from my application database I got the URL there and let's bring this back up and hopefully my Pace Works a little bit better this time edit and paste there's my URL so if I run this it's going to create a file called screenshot.png so if I hit enter um oh I didn't like that what did I do wrong what did I do wrong so I've got a screenshot and the URL oh it's the exclamation point so I've got to escape my exclamation point so let's try that again is it there nope all right so let's try that whole thing again I didn't have this problem earlier because I was taking a screenshot of my blog page and oohs that's what I wanted all right clear and we're going to do edit and paste still not right I think it's actually executing that because I included a character in there I shouldn't have so let's copy again should be an interesting edit video to edit and right click and we will paste man I'm having tons of luck as soon as I had the record button um things get wonky in here edit and paste there we go all right my application URL we're gonna grab again from my Apple my database so just going to copy that come back to my windows subsystem for Linux and right click edit paste I know I can right click in there but I want you guys to see exactly what it's doing now I need to escape that exclamation point because I am on a bash command line so if memory serves I think I just need to put a backslash in front of that exclamation point and I should be okay um and it's running okay so it's running in the background just ignore those errors for the life of me I have not been able to figure out those errors are and you can see there it wrote the file uh screenshot.png so in theory if I do a list command I should have a file here called screenshot.png and I do all right so that's number one that's what I want to do the second thing I needed to do was figure out how to attach this to a recorded service now so I'd like it attached to this record good right here and that's where it gets really fun so I got to discover the attachment API never used it before I'm going to copy and paste in I like we've been trying to do and hopefully I have better luck the full command I'm not going to walk you through it and I'm going to have to blur out things like my password and my Dev instance URL but let's walk through it first of all I'm using curl this is per the servicenow documentation it's navigating to my PDI so that's going to be blurred out but it's my PDI and it's going to my table name which is right here x underscore store update store application and then I need to plug in the sys ID of the record that I want to attach this to all right so I'm still sitting on that behind the screen so let's pull this up and we're going to right here copy sysid that's the record I want so let's go back to my Linux system here and we're just going to I might be shorter to go home and then Arrow my way over or kind of control tab no I can't so I just error my way over to application underscore sysid I did that so that I didn't come in here and you were seeing me actually put the sis IDs in and not it already being pasted in there because I wanted you to see where they came from unless they hit the insert button let's uh oh man I totally messed that up what the heck is going on okay Escape Escape I don't know what's going on uh clear uh control C there we go clear all right let's try that again we're just going to copy this over here and paste it into here edit paste all right and then again I need to get to my let's go home I need to get to my application underscore sysid I'll fast forward and post here and I'm going to be very careful to only hit the delete button I think there's just a display issue going on here and I'm going to trust that that actually deleted it now let's go grab my sysid from here copy sysid um and then come back in here and we're going to right click the header there edit and paste and I think well it definitely put an assist ID it just looks really bad from right here looks like I got an extra space too so I'm just going to get rid of that and let's see if I can backspace my oh this is looking better okay man that was so weird um but now actually I'm missing part of this this idea okay let's let's backspace all the way get rid of that let's paste one more time again right click edit and paste there we go as a full sys ID all right this full sys ID everything I want there so let's walk through the rest of it um this part right here where it's given the file name basically I'm telling bash to use the date and I'm telling it to use it in the 2023-07 17 format so basically year month day um because today is the 17th and then uh basically I'm setting up the headers that are going through that command and then I'm passing a user which I'll blur out in post but basically I set up a user and granted them access as a web service only and a user on this particular application that's it that's all the access they have and then for the data that I'm going to pass I use this little at symbol and then the name of the file that I just showed you screenshot.png and what it should do is it should attach the file to this record in service now so let's go ahead and hit the enter key and see what happens it's thinking about it it's making that that rest call maybe maybe click harder there we go there we go he made it uh and we got a response back from the servicenow not that's not important I really don't care about the response what I care about is that the attachment actually show here so I'm going to reload the form and we should see there it is there's my attachment 0718 um wow somehow it moved to uh the 18th that's weird and there you can see my screenshot the application that you're looking for is not found okay that's exactly what I wanted right so that's how it works let's see what this looks like when I plug it into a custom action in flow designer all right so here is the custom action retrieve application page screenshot my inputs on this is just gonna be my application my application record has the assist ID of the record I want to attach it to and it also has the URL of the store application and in my SSH step it's everything I just showed you except I'm going to do some shortcuts around uh passing the location using the record value so that's application record and then I have the low location which is you can see here a URL so make sure my head's not in the way here yeah it should be showing there my URL so I basically took this and dragged it and dropped it over into my script and another additional thing is in bash I use this Ampersand Ampersand to run two commands in a row rather than show you like I did where I ran in the terminal window and then in the second one I want them to run consecutively so I use the Ampersand Ampersand um and then the thing here for assist ID I also grabbed that from here so I just dropped this ID and dragged it over here and that put the assist ID in that particular one and everything else is good everything else is the same as I just showed you and there are no outputs right so that's the custom action and in that custom action it's in two subflows I'm using one subflow is when I manually call an application record crawl so I say basically hey go crawl this application and the second one is in my every Tuesday I do crawl of everything I have in the database and what's important to hear the reason I'm showing you this is I'm going to do a couple things I'm going to set the application to an active I'm going to then retrieve the screenshot and then I'm going to update a crawl count that I'm doing to see how many applications were not found so I'm just counting that for metrics and then in my subflow in my one page application crawler um I'm basically doing almost the same thing except there's no counts no crawler to update because I'm just doing one record um oops I highlighted the wrong thing let's scroll back up there it's right there so basically I update the store application to say it's inactive now I retrieved the screenshot attach it and I end the subflow because there's no sense in processing everything else so let's show you what this actually looks like I've got in here this is still active we know this doesn't go anywhere if I click on the URL that's what it looks like so I've got my app my button up here for crawl app crawl app is going to run this one crawl one application it's a call it's a UI action to a subflow so I'm going to click on that and we're just going to sit here and wait and what I'm expecting are two things one the active date will change and the last crawl date will change and that will keep me into hey it's done and then actually you can see it happen right there while I was talking so active is now inactive and our last crawl date is updated so now it's accurate and notice I don't see a screenshot yet because I just need to reload the record to see the screenshot so I'm going to right click I'm going to reload and I should have my screenshot there and I do pull that up and there is my screenshot of application I found so I thought this is really fun and really cool I wanted to share with you all I got to do some scripting I got to do some headless browser work and put that all into a custom action and incorporate it in my application and now I have a little bit more of efficient application than that I'm not crawling things that are no longer active in the store and I'm leaving myself evidence of what the crawler found the last time I was there so if I ever come across something where all the variables didn't line up and for some reason it stopped crawling it I'm going to see a screenshot of maybe something different maybe it'll be an error message maybe it'll be a page not found or 404 to show me that hey there's other situations where I might see this but anyways lots and lots of fun in flow designer scripting and a mid-server I hope you found this video helpful if you did please like Please Subscribe or share it with somebody who you think might be interested in doing strange things from your PDI into a mid-server to bring content like this to you on YouTube and until next time don't forget to always be learning thank you
https://www.youtube.com/watch?v=ImN1Ka9Qr3c