logo

NJP

NOWCommunity Live Stream - Code Decoded - Creating new records

Import · Jun 12, 2020 · video

[Music] good morning good afternoon good evening wherever you are and whenever you are welcome to the community live stream my name is Chuck Tomasi senior developer evangelist at ServiceNow and this is the show where I take your questions from the community turn them into content share them with you so that you get the insights the journey of discovery the reverse-engineering all that good stuff that goes into those answers and you can take that to your organization and be a more effective ServiceNow administrator and developer so welcome and thank you everyone for joining us this show is done live at 1:00 p.m. UTC 2:00 p.m. excuse me 2 p.m. UTC and you can do the math for your timezone there on Mondays and Thursdays this is Monday June 15th 2020 and today's topic will be a direct question from viewer John who has can you show us the several ways in which you create new records and I thought ok we had a little volley back and forth on the email saying what are you looking for and he said you know there's a couple of things I want to look at so I will dig deep into those happy to bring those to you and I recommend that you subscribe click the link you know what to do with the YouTube stuff you see right there we also do this on Twitch in case you want to watch that over there it's around for a couple of weeks so you'll probably find three or four episodes available then they roll off the end but it's always a nice backup plan in case something goes crazy with you - good morning abdicate Hodnett good morning because John is on John this is your question so pay attention this is this one's for you a lot Shan Paulo good to have everybody joining us today and I will get through the pre-roll as quickly as possible remember this is the community so if you've got questions go over to community dot service now calm that's what's behind me and that's where you can post hundreds of thousands of people there I know sometimes people ask me questions on LinkedIn or YouTube comments and want to get into a deeper discussion the community is really the best place for that because then other people can learn from your question other people can con contribute you're not just asking me one person who knows a small section of everything that's on service now this is the experts I am just a small representation of that all right so that's it for the community developer portal I invite you to go over to developer.samsung.com as always go get a free personal developer in cents another month or so maybe about six weeks we will have the Paris release in early access available for you on your personal developer instances so if you want to jump in learn start experimenting with Paris before it becomes generally available later in q3 this is the place to do it go get a free personal developer instance developer debt service now dot-com that's not all they have of course we've got the api's for scripting for rest api's the component libraries for building the new now experience components I know that's gonna be dated won't it somebody watching that's called new these are been around for years well as of today June 15 20 20 they are new so that's that's we've learning plans you can get a lot of free learning over at the developer site great stuff invite you to go over and take a look we also have a webinar coming up next episode of tech now I know it's been quiet for a while we didn't do anything in April or May because of the run-up to knowledge knowledge is now officially done six weeks so focus back on tech now episode 76 is going to be all about the integration hub and flow designer innovations we've come out within the 18 months since we did our last episode register at the link you see there were bringing a subject matter expert Michael slobotnik who did a session and knowledge on this topic actually he did he did more specific but we asked him to come in and broaden his horizons and educate us all good morning to Carolyn and RJ and Sandeep and everybody else who's joining us on the live stream really happy you could make it here today we also have the July episode which is being ramped up that's going to be about all of the Paris topics so Paris platform I should be more specific it's not going to be about CS MA char and ITSM and I Tom and sack ops all that good stuff well you can learn those from other sources my focus is on the platform and integrations and that's what we will have in the July episode I'll get you a date and I'll get you a little flashy screen like this as soon as I can but for now let's get back to the show I will be running a little bit of script to that this is an orange day code decoded is the theme so I'm going to be talking about code and some of the things you can do so you noticed well since April last couple of months we're on this color theme we've got a theme for various topics I'm going to be digging in deep to the code you can find the scripts that I've written for every episode dating back about two years I think it was mid July of 2018 that we started saving the code snippet somebody said hey can we do that I said yes we can so as soon as I stopped the cameras I commit to github and there it is over at the github excuse me that's the wrong screen this is the github link bitly /sn - CLS this is the one if you want to learn more about javascript i'm off script speaking of script none here this is a live show one-take unless the stream fails I'm not doing it again Oh all episodes all mistakes are captured for posterity just like that one even the corrected my voice so let's get on with what we're getting on with good morning to everybody and I need to just update my notes here make sure I covered everything yes no we're going to be talking about different ways you can create records in ServiceNow and you go well is it as simple as just going to a list like oh I don't know this one come on it's fine and clicking new yes it is and that does something specific and I want to reveal what goes on behind that because there's ways that you may want to do this in a script any workflow etc that you want to take advantage of if you look closely if you look closely the URL is very telling about what's going on here so I'm going to copy that into my blank editor and we're going to decipher that take it apart now I realize this has some encoding in it percent 3d as equals you get you get about two or three these in your head you figure them out real quick okay so let's take this apart obviously we have our instance ding-ding-ding we have our nav to do which is what puts it in that main content frame it says put it here don't fill up the whole screen just do this and then we have the URI that nav to do is going to use and it says slash that's a percent 2f and it does this so that doesn't confuse that like the equals here after URI with it equals out here in the URL so take to this chunk of it and says now where do you want me to go what do you want me to do with it I will translate that for you into more of a real Ryu RI URL ok I've got a table CCW workshop do that says take me to the forum if it said under list dot to do it would be taking me to a list and right here you see this part where it says sis ID whatever you double click now I guess that only works on a browser sis ID equals that's what percent 3d it's easy to remember there's a few years ago everybody had like 3d TV 3d glass of 3 everything everything was 3d well if you remember 3d is an equal sign that's the mnemonic I use like oh it's in 3d that's in equals sis ID equals minus 1 is a special way of getting those records says it is a special sis ID in fact there is a Doc's page dedicated to that it's called the unique record identifier sis ID equals minus 1 and it tells you all about the cool things you can do in there that is one way to do that ok let's go back to our instance looks good and then it says what do you want me to do once I've got that there's some other things in here that work really well I'm going to get to those in just a minute but I want to start even more basic with something you've probably seen before and that would be using Glide record initialize and glide record new they are scattered throughout server scripts so if I look for business rules it's on your system definition and say show me all the business rules where script contains initialize you will find all kinds of cool stuff in these scripts and many times if you look under advanced you can find look can write initialize right there so it's it's in the system I still have not found a good use for initialize over new record and you're saying we'll check what's the difference it looks like we're just sort of setting things up so that we can set fields later I don't know why the popularity of initialized somebody may be able to point me to a use case where I really want this instead of new records let me go into explore I don't use explore too much on this show I don't know if there's no bias or anything it's just I didn't have it installed on my PDI the last time I Z booted it which is one of the things you can do with your personal developer instance start over scratch wipe it fresh and I forget - that's a browser plugin isn't it no it's it's an update set it is and these little glasses up here bring up the explorer window this is a great add-on available from the developer portal going to connect share look for Explorer spelled this way XP lor a nice little add-on it's sort of like scripts background on super steroids so what I'm going to do is show you what happens when you run initialize how long going to do is do a glide record query and for that to avoid typing mistakes I have a script back here and I hope I didn't wipe out my other script okay I have new record and I've initialized these are exactly the same and I will paste that in exactly except for the initialized part it is doing a glide record on constant employee table you go Chuck what is that that's just a shorthand way so I don't have to type in X underscore 6 6 2 3 8 underscore CL s 3 2 3 underscore employee I put in this constant script include so that I've got my definitions sort of like my pound defined if you're a little C programmer or something of that nature it's those constants that go into lots of different places I could go in business rules in UI ActionScript includes and workflows and you scripted UI it all over the place just if you see that you know it's kind of an easy notation that I don't remember and I don't have to spell it and if I ever change the scope or the instance or the table name which I've done I've said oh this table used to point to say user now I've got to go around and chase down everywhere that I reference this user because now I extended this user and I call it customer that's ugly be really nice if I just referenced it as customer table or employee table and they just changed the definition of the constant script include easy to do so don't get wound around the axle on that it's just a shorthand way of pointing to another string now here is our old friend initialize and I use this to inspect the data in Explorer when I run that I get o constants not defined because I'm not in the right scope I'm glad I hit that run that and it says here is what you get out of line six you have yourself a scope glide record and in there notice here's the field band is empty date of birth is empty date of hire is empty state is empty everything is empty there's no default values it's an empty space and you may want that but I know for a fact this employee table has default values set up in the dictionary if I go to my employee table CLS employees and I will verify this to you to say configure Oh Dictionary our table today let's do dictionary let's do dictionary table is a nice format if I want to see more than just the dictionary entries but I don't look I've got a date as a default for the date of hire field do H I've got band is novice I've got border is 100 I've got some default values in here that I want so why would I do an initialize and then go and set these unless I want them something other than the defaults but you can do that anyway so again I haven't found a good excuse for using initialize over new record be great to see the script includes just to be sure I understand script includes for what give me a I went I might have missed what you were looking at when you wanted to see the script includes so these are not glide record it's not a script include that is a Java implemented API that's exposed through JavaScript okay so initialize doesn't have there's not no code you and I can look at it's not everything is not a script includes that you call within by instantiating it with a new argument so let's replace this with new record and watch what happens oh my Constance yes okay we can look at that let's look at Constance right after we get done with this look now that I use new record I've got band is set date of hire is set these are all the default values that I want which seems to make more sense in 99% of times I'm doing an insert on the record I want to do a new record set a couple of fields it was the first name last name this kind of thing and do the insert operation and I've got a new record so unless I'm really curious somebody out there has a good use case for using initialize / new record I haven't found it yet because every single use case that I do first thing I do after saying var blah blah blah equals glide record some table name or glide record secure as you should be using then I do a new record do I want default values or not and I haven't found an excuse yet to say yeah I don't think I need them this time unless it's one of those tables that just has like two fields but still I'll do a new record for consistency so anyway you want to see the constants sure I can do that studio collapse server script include con here is my constants file for this little app it is I don't even need this part prototype that can go there's no prototypes I don't instantiate it that way you're not seeing new constants table name it is just saying here is a value go use it you could declare functions this way too many people do I'm trying to get where you might have seen one of these Glide record or Glide filter dot something anyway this is very easy to do and it gives me one central place as I mentioned for all of those server-side constants that I'm going to be using so nothing fancy just constants dot employee table go and once it is defined I can go in head and use it again so if I wanted to I could say constants scope equals X under six six two three eight that's my personal developer instance three two three is the scope for this app and I could in fact say you know what instead of that let's do constants dot scope plus and do that for the rest of them as well it will work you can reference itself in itself very easy to do so no performance degradation that I found and I am beating it to death right now on another a project that I'm working on so very helpful part two of this is getting back to that society equals minus one thing that I was talking about and I have that over here now if you try this yourself and I encourage you try it on your PDI it doesn't hurt anything go pick a table name like let's get to this to do table okay paste that copy that and I am going to go to my personal developer instance slash table name under list do okay should bring up without the navigator because they didn't do the nav two dot do URI blah-blah-blah-blah-blah okay it brings up the list as I mentioned if you go to table name under list do that brings up a list if you leave off the list okay can i zoom in on that I can't zoom in on the URL I can really zoom in on the browser content that's annoying leave off the list apologize if that's a little hard to read it will bring you to a forum this is what we're doing when we go over here and say something under list or something dot list okay this is a shortcut to do the same thing in the nav page and if you leave off the list brings you to okay you get the idea where'd my page go here it is so I've got this it's going to bring up a blank form but that just navigates you there it doesn't get you started on creating a new record I can't simply say or can i let's do this let's put something on the employee table let's put a UI action sounds good so create server development UI action click click on oh it's highlighting on me wow that's being highly annoying create ok Studios being a little weird on my font size refresh I'm not sure what I did there create UI action click Lake I think it might be time to Z boot my PDI or turn off some plugins or something will go old old school on this go back here under system definition UI actions studio is not having fun it's working fine this morning let's go to new and we'll go create one call this create task on the employee table actually task points to project but that's ok we'll put the UI action on I'm sorry on the course table I'm working on something for another episode two out so let's do the course table because it has a reference if I remember right to the parrot course I'm going to let's make this primary color sounds good we'll make a button and take it off of insert so we save it first and do something very very simple and say just that info hello world you know what hello world has a comma in it and we use action dot well how it's my font size here action dot set redirect URL one of the most common things and if I leave it as it takes two types of argument this is an interesting concept you can either pass it a glide record like current and it will say stay on the page when you're done okay whatever you do put a message update some records and all this cool stuff stay here redirect to this page I'm going to submit that really wish it was in studio but we'll come back to that and then I go to my course table just right there at the top already hit go to this course and I have a create task up here now granted I have a pretest down there if I now I didn't actually create a task that's just the label I haven't implemented that functionality I'm building this up little by little I did not get a GS info oh because it went to the log I thought it was doing an ADD info message that was silly if we do go to the logs I'm sure I will see a hello world in the logs probably twice hello world hello world wasn't very necessary to do that it wasn't very evident but let's right-click that and do an edit UI action I don't do that too often but in this case I will and put something else in here what did we want to do we wanted to do the society thing so let's do GS add info message creating tasks da all right no the other type of argument besides a glide record and this doesn't have to be current you could go get go look up a specific record as long as you're passing in a glide record object you can pass that there and it will go there it will go to that record but I can also send it a string so let's go to X you know what let's do our constants thing constants dot did that put tasks table in the constants I don't remember we'll try it it'll freak out and die if it doesn't so it's going to go to task table and then society the equals minus one I'm going to go and check and make sure I have that constant setup script include should be one of the most recently modified there's constants and I have no I don't so we'll create that real quick come on copy paste task table and we will call this I think it was this called task was it not see now I don't have to remember all this crazy stuff once I define it once all I do is remember my constants and they're usually pretty easy to recall get yourself a naming standard let's find out if that in fact was the right table name paste that up there do it dot list I've got some tasks that's going to work back to the course table let's see if I can get my sis ID equals minus one to create me a new record go to the course create a task and there's my message and there's my new form they're saying well if you redirect it on the URL it went to a blank form what happens if you leave this society equals minus one off I think I said that right we're gonna try yeah go back to the UI action create task let's leave it off just to see what happens if we just go to the table cut that so I have it in the copy/paste buffer for later will it work go to the course record create task fascinating it doesn't put the minus one up there but it still goes up there that I didn't expect to work we're all learning here this is live unexpected result I would not do that because it doesn't it doesn't tell the system about that special sis ID so back to our UI action the proper way to do it of course is to put this in that but here's where the cool part comes in if you use the SIS perm query many of you remember sis farm query from lists so let's go to this list and that's not the list let's go to this list and say active equals true credits equals for you start building out this breadcrumb and you say copy query get down here whoops and it gives you I didn't work try that gives you this encoded query thing right you remember in coded queries if you don't you can go look them up they are an encoded way to give a set of criteria that you can feed back in in a number of places commonly used in add encoded query in a glide record query you can pass in one of these things you can substitute hard-coded values for variables whole video just on that trick so this is an encoded query string it represents this filter this is a human readable version of that it's the only difference and I can also take this let's go to the task table so I don't have task there anymore tasks and I want to make something up because I can't remember the fields if I say the course is that active is true and assigned to is that I have any coded query string up here let's copy that look at it in the editor because I want a record that starts with those watch what happens if I click new right here it will automatically apply all of these fields in the filter very very handy new a side to neva course is already set active is true granted I could have said activist false because active defaults to true but in this case it doesn't matter it's it's overriding the default true with another true because that's what the URI says that's what the filter says that's pretty darn cool and if you didn't look closely in the URL going to also copy and paste this out somewhere in here is see if I can get that from not scrolling around on you somewhere in here there it is it says sis parm query equals and then it has course equals some society which is what we want for a reference field active equals true assigned to is another Society we can use that to our advantage also so if I take that same URL thing copy the query and put it into my action now that wasn't it UI action create tasks come on I'm waiting there it is I can add that to this using proper formatting of a URI I already have a question mark so the next one is in ampersand and on it goes ooh this is encoded it's probably not gonna work all that well but we can remember some of our little tips and tricks oh you know what it'll be fine we need sis parm query sis arm under query I thought it was encoded it's not and it should be actually but we're gonna try it without this see what happens because the separators on the URI are actually this ampersand thing and since this is our last one we should have pretty good luck famous last words save this will create a task let's go to our course again create a task automatically assigned to that person automatically assigned to that course and there we are doing the same thing we did from the new button on the list we now have effectively a new button that auto assigns it there again I could go to that UI action and replace some of those hard-coded sis IDs because you know me hard code is this IDs bad idea someone's gonna change that user someone's gonna change that group have a way to look that up have a way to get that from a data table wherever this allows you to do that so this is another way to do the fields preset with sis perm query it's not just for getting a list of those records that match that criteria it's for way of setting those fields so our template field works also so on the field by field basis you can set an attribute to change that behavior creating a record with a value from the query not sure I quite understand Jace but yes you can you can order if you took out course course would no longer be set if you took out active active would go to its default so on and so forth so those are some of the things that we can set any field aside from obviously you're not gonna be able to set this updated on so it's created by those those six default fields Society no don't mess with those don't don't try and force those in when you're doing a society equals minus one if we look is there any other cool stuff in here there is another page that I reference in the show notes for initialized versus new and I thought I had one other page let me look that I found something at the very last minute about examples of navigating by URL that's what it was let me go back here so there is a page dedicated to the record identifier navigating by URL sample there it is examples stop scrolling Mouse navigating by URL Orlando done and it shows you some of these things look there's our friend sis perm query there's our friend Society equals minus 1 that's why I wanted to throw this page in when I post this you'll have the links to it moments after the show is done so there's lots and lots of other things you can learn by going into the URL and taking it apart view a list of incidents right there is a form there is a list you are a table equals on your list you have if you use underscore list you go to a list if you look at a list of attachments this attachment under lists I do some of you may already know this good on you there's there's a lot of people out there that are not that are sort of looking at these URLs going wow these are really obfuscated crazy things they don't make a lot of times when we take a URL shortener and shorten them up but there's important things in there that I want you to recognize when you go into when you go into the URL and start to look at it return a file whew you can put a processor on the end to return file we're not going to get into that at the end but you can say question mark xlsx and it will return that data in an Excel format but maybe another thing for another time explorer if you haven't looked at it very handy I just want to show you that for the new record versus initialize thing and obviously we've got our society equals minus 1 doing a lot of work for us and you can use that in a redirect you could use that in a business rule I've even used it in I leave it works in service portal I have to check I haven't done that recently but you may be able to do a new record in service portal that way can we go with a glide URI class to play with the URL Glide URI is available I don't use it too much I know there's a client-side version that's not as well-documented as a server-side document but you can take a look at it I usually use it to say hey somebody passed me a URL what are the parameters you can use your script to parse and get field value pairs out of the URL or out of the URI so you instantiate it like this there are a couple of things I believe there are a couple of methods missing it from here that I've seen in onboard scripts I've gone through this was like a Christmas exercise last year I said go through all of the outer box scripts and find somebody that mentions glad URI and what methods are they using and then I compared them against this page and went this isn't a one-for-one match so unfortunately I didn't get to finish that one there's also a lot of other methods associated with the action object we showed you set redirect URL there's also set return URL so if you go to a page you can affect how it comes back when somebody hits the back arrow there's a lot more to the action object that working on getting that documented as well but again that would because knowledge suddenly blew up in our faces and we had a lot of work that we didn't previously have had to stop that project but it is still on the back burner looking forward to getting back to that and improving the documentation as always helping the docs team build that documentation so that is going to it by that's all I was just trying to share yeah yep it does sometimes it does take a while to find this now you've got information John I hope that was helpful on the different ways you can do create new records both with a new record or initialize and what the differences there are and this aside e equals minus 1 so is there anything else that I may have missed we talked about all that good stuff nope I invite you to get a personal developer and says try this out yourself and if your studio is getting a little flaky like mine it might be time to Z boot which I'm afraid to do because now I have a couple of episodes already queued up with some data but it's in a scope dapsone all committed to source control and bring you back thank you very much for joining me I really appreciate it it has been a wonderful time let's get back on the right screen there we go thank you and if you learned something obviously share it be part of the community be kind to each other stay safe and I will see you again next time get that music going bye [Music]

View original source

https://www.youtube.com/watch?v=46ufK9jMO3Q