ServiceNow introduction to getReference | ServiceNow getreference with callback function
[Music] hi all and welcome back so in today's tutorial we will be crea we will be looking on one of the use case so one of my subscribers reach out to me with the problems stated and we'll try to resolve that problem statement out okay so basically it's it's referencing to my previous video on glide ejects okay so basically what they are trying to say that they have looked into the glider jacks video okay uh hi i'm it i like today glide ejects to auto populate fields i have similar request update field based on request for field okay source is a reference field so request for this reference feed and feed to be auto populated are also reference fields okay but the issue comes when it's it's an email field which is string feed okay i got the issue so basically the context is like there is a reference field called reference uh requested for okay we can consider any any reference field uh which is pointing to this user table and uh they want to fill on change of that particular field they want to populate a field called email field okay which is string type of field okay so basically my suggestion will be likey or why we want to go for glider's exposure okay why oh we can go with the get reference also now as soon as i say get reference a lot of controversies will happen that people will start commenting that get reference is not bad best practice and all the things okay so why it's not a best practice for g and g underscore form dot get reference just end up calling the client side light record api okay in my opinion client side glide record should be avoided as much as possible since it since it usually resent into a lot of date data transfer which is not necessary also okay but if you are going to use it out with a callback function okay then it's not a problem okay uh client say glide record can also take a callback uh query okay and then it's not blocking anything out for you okay so instead of glide dijks we will be using get reference okay uh to resolve this the reason for that is like it's much it's easier to implement okay and with the callback it's it's falls into a best practice category only okay so let's jump on to the service now instead so you have gone through the statement okay now a user reach out to me on linkedin okay so let's try on our favorite table which is incident okay so incident undo so let's try to see first of all we have a reference feed so instead of requested for okay requested for over here i will be using uh caller field because it's also a reference field to the uh to the usage user table okay and the second thing which we'll be using is email field we don't have email with the let's quickly create an email field okay so i will go quickly on configure table scroll down i okay or i will new and i'll give it a name so i will it's a string fill they want trends to string again string will give it a name as caller email color email color email okay and maximum i will take this name okay you underscore color email and maximum length i will keep it at 40 which is the default one okay that's it i think that should be enough and now we will modify the form to see it on the form so i will do show form or we will go with the design form a lot of things we have to do as a prerequisite in order to get it working so well the email field newly created email field i will create a caller email right by that failure oh it's already there or what okay here it is caller email i will put it below the caller only so that it looks good okay we'll save the form close it out okay and we will simply again go to show form okay to see whether the email field is visible to or not so perfectly so we have caller email feed and now on the change of caller we want to fill the caller email field perfect sounds like a plan so we will go configure client script we'll be writing an unchanged client script okay uh because we want this field to be get changed or get populated whenever the caller is changing okay so we'll give it a name as like caller email get reference okay wrong okay let's say it's an incident table your type we will keep it as desktop type is on change on change of what field so we want on change of color okay inherit we don't want okay this script will populate the caller email based on caller okay just some reference okay and now we got the syntax for this okay so what we'll be doing over here as we decided we'll be writing a callback function so let's get started so what we'll do we will write a code over here so it will be pretty simple syntax so we'll go variable request a user or we can give caller user get reference and as you can see get reference takes two attributes one is field three and second is callback so we'll be using a callback function for this okay so it asks string film is what exactly you want to reference okay which exactly fill you want to reference out okay so i forgot to check that so we'll go incident dot to or i can i i don't want to do incident. and we will check what exactly the backend name for caller is it's caller underscore id okay perfect so we'll give first attribute as caller underscore id okay and the second attribute is ask for callback okay so we will give a function name for callback as callback only yeah sounds like a plan okay callback only now we will define this function now so what this callback will do it will it will work on a synchronous fashion okay it will not uh it it will not keep you on waiting okay till the execution happens okay it just makes it asynchronous okay so we'll go function we'll define this function now call sorry if you will [Music] call back okay what it will take it will take the variable which we have defined over here which is requested for okay requested we will requested a caller user so we'll give it as a input attribute and then we will write our logic so what we have to do g underscore form dot set value set value and we can give the name of the variable where we want to set the values and from caller we can now we have a caller object in the in caller user we have the entire object for caller okay so we will do caller user caller user dot what is this [Music] oh it's getting populated just a second if i select any user out for example able tutor so on selection available tutor i want this email right so it's emailed right so we'll do caller dot email the perfect so we'll do email okay and we can give some information also he has dot not she underscored form dot add info message that caller email got updated perfect i think that is enough for this tutorial okay this much of code so if you go to gliders you have to write a lot of code you have to write a script include on the server side then you have to write client side and all the things that's why i prefer get reference whenever it's possible okay in this kind of scenario so we'll simply save it out and let's try if it runs so we have saved it out okay we'll go to the create incident form we'll reload the form leave i don't want to see this and whenever you write why i reload the form people are a lot of people face this issue whenever you write anything on client side so it's better that you should reload the form so that it start effectively acting on the particular form okay which you have opened so we'll go on the caller and ideally it should work now so i'm changing the caller to able tutor okay and i didn't get the email id why i didn't get the email id let's check color user [Music] g underscore color used [Music] what just happened caller user equal to oh it's not caller user right oh it's not caller user what it was i forgot sorry sorry my bad my bad so it's not called a user it's you underscore caller underscore email you underscored caller underscore email email right i will save it out and if i reload the form now i was pretty surprised how can i make mistake in this simple script so now if we again select the apple tutor now you can see the email got populated and we got a info message also pretty easy right for this kind of scenario okay so i think it will resolve this problem statement okay it will help you have to resolve this particular problem statement okay this is all for today's tutorial hope you like the video okay and if you are also facing some these kind of issues okay you can probably reach out to me on linkedin or you can drop a comment on the youtube channel and i will try to create videos on the same as soon as possible okay so stay tuned happy and nice nice weekend you can say okay okay thank you bye you
https://www.youtube.com/watch?v=Figyw_NVOfY