#4 Create Business Rule | Learn Application Development in ServiceNow | Leave Management System
[Music] please subscribe to my channel and click on the bell icon to get the regular updates of my channel and do not forget to like comment and share create leave bucket for each employee so the requirement is whenever new employee is inserted into service now it should create the leave bucket for that particular employee so how exactly it will do the calculation so you have to make sure that as per hr perspective you are putting the calculation logic in the calculation table that is leave calculation as per the country you want to configure and with that configuration every time a new employee will be inserted it will basically allocate the leaves as per the calculation as per the logic table which we have created which will be filled by hr so what we will do now we will create this logic so if i go to my personal developer instance now how exactly i will create it so basically you have to create a business rule because that's what you can do because what you have to do whenever a new user is created in this user table that's a user table then you have to make sure that it is creating the bucket as per the calculation from leave calculator so if i go to maybe let's say i i come over here and i go to our application and i go to leave calculators now here i will create a single racket because i will i just i'm just creating this racket so that you understand that why exactly we are doing this uh calculation why exactly we are creating this business rule so let's say as an hr maybe we have a hr person he's uh he's he or she creating this record so maybe i'm just putting yearly for now uh starting month i can just do uh jan that means we want to calculate uh we want to calculate the month of uh starting month from jan to december now every organization basically calculates the lease depend on their like process they have some companies like they they select financial year like maybe uh from from april uh to march some companies they do like from jan to december so it's quite configurable so you can just mention that which which kind of month you want to mention over here so we have this starting month gen and then we have this december so in that case i will select the country so what i can do i have this india country so i have selected india here now for example for india we want to dedicate some leaves so let's say we have 24 leaves that means per month 2 leaves and we are just calculating as 24 leaves and that is yearly that means it should assign the leaves from the beginning that's just a basic i would say the process you have in your organization just kind of a demo perspective it's just kind of an example now here you can see i have the cs so i have to correct this so maybe i will uh before [Music] changing this let me go to leave calculator so i can just fix that value and i have this leave type over here and i just need to change the default value it should be cl not cs so it's casual leave so it says cl that is casual leave and i can just save it if i would save it and i will just automatically select here casual leaf so this is for casual leaves as as hr person i'm just putting that for india uh the basically leaves should be calculated from jan to december it should be assigned leave assignment should be yearly and then for casual leave so in that case what i will do i will save this record so i have saved this racket over here and now if i will come over here i have this and i have this leave calculator table what i will do i will also open maybe leave bucket as well that's what we have to create i have to insert the racket in this particular table so the requirement is and i will go here i will go to server development if you will go to server development you can see this business rule so that is what we are going to create so i will just double click on it so it will open the new form of business rule and now i will provide a name over here so maybe i will put create leave bucket that's a name of the business rule i have given in which in which table you want to create this i will do it in this user table so if i select this one so you can see i have selected this table over here and then i have this advanced checkbox so i will click it and it says basically when to run so you can do it before you can do it after it's totally up to you but ideally we will do it async so i will do it async and we will do it whenever new racket will be created so that's what you can do so maybe i will do async insert so uh and and for every user will be created the important point here that you can put some conditions because in servicenow you can create uh different users as well even like local admin accounts or maybe local accounts as well in that case you can put some conditions that you want to uh do you want to trigger this business rule only for employees not for general service now account so in that case what i can do i can just select here employee number is not empty so so i can select this one so in that case basically this has a condition now that employee number is not empty then only it should trigger this particular business rule so that case i will go to advanced i don't have to do anything in actions because i have to write the script i will make it big screen that is full screen we have and i will make it a little bit large and and i can just do like this so that you can see the script and now i will write this script now the thing is in order to create the racket that means in order to create the racket in leave bucket you have it's it's very important that you have to check the country as well in your leave calculator okay that's how the logic should run it should run on the basis of the country so what i will do i will start with where gr equal to new glide record and here i will put this calculator table i will copy this and put it over here because i have to glide record this particular table and then i will do gr dot i will do gr dot add query now here we have to basically uh select the country so because it has to match the country of user so how can i how exactly uh you can do that but before that i think we have to check uh the name as well so i will go to leave bucket and if i select uh if i check the country uh that's a calculator not the bucket one so you can see we have this country and let me check the name as well so i have selected the name i will click on ok so the name of the column is if i go here it's country so i can select the country but it's a reference field so what exactly you have to do basically the record which is being inserted for the user that country and this country should match that means it should match with this country in the calculator so in that case our logic would be country dot because that's a reference field so i will do name that means dot name i have to check the name of the country i will do comma and here i will do current okay that's the racket being inserted and i will do dot here we have location and if i will take you to the user table so let me show you quickly so that we understand that what exactly we need to take as part of the dot walking so let's say we have this user i open this user and do i have location over here i don't have location but let me quickly maybe just capture it we have this country code but i have to take this location and then i can just click on ok and i will get the country over here so these are the locations but the thing is we have to match uh either either with the country ideally it it has to be a country or either with the locations it's up to you that what kind of logic you want to implement but in this case what we will do we will assign some countries in this particular field uh to to some of the new users maybe so that our logic should run but it's up to you what kind of logic you want to uh populate so in that case now if i talk about this location or every location i take you to over here can you see we have this country and that's what i have to match because the whatever country i will select and whatever location we have already selected for a user that should be that country that means dot by dot walking it should have country over here so that it matches that record in our leave calculator so in that case i will close this and here i will do dot i will do country because i have to match the country location can be any anything so if i talk about india perspective it can be location can be delhi location can be bangalow location can be pune any any different locations but if i dot walk from location uh for that location that what which which country uh we have for that particular location then we can see that so that's how that's what i'm doing this dot walking location dot country and then i will do semicolon and what i will do i will just do query that's it so and then i will do while and gr dot next i think you can also do if because you don't have to i would say you don't have to run the loop as well so you can just do if but maybe i would say multiple brackets are created i think it will definitely uh trigger the business rule so you can also do if gr dot next i think gi dot next is also okay so now i have to write the script over here as per the condition we have in that uh leave assignment field the reason behind it because we can assign the assign create the buckets we can create the leaves as per the logic which we have in leave calculator so we have two different logics that is leave assignment that is yearly and monthly so in that case what i will do if i go to leave calculator and if i see let's see the leave assignment i will just see the name so we have this leave assignment and if i talk about the choices uh we have two choices so we have m and y so in that case i will come over here so i will start with if now i have to put the condition so if gr dot leave underscore assignment that's a field name we have equal to equal to yearly that's a back-end value of yearly if that is early then the logic will be different the creation uh process will be different if it is monthly then creation process will be different so in that case what i have to do i have to create the racket so in that case i will do where i would say we will just create over here like leave bucket so that's a variable i'm going to give leave bucket equal to new glide racket and here i have to give the table name of leave bucket which is this one so i will copy this and and i will just give it over here then i will do leave bucket dot initialize that's how you create the racket in any any table so you have to initialize it and you have to put semicolon and then i will do leave bucket dot now we have populate the fields so the fields we have if i talk about leave bucket over here so maybe we have leave type and i'm sure this is leave underscore type and if i go to leave calculator we have leave type here as well so i would go to my business rule and i will just do leave underscore type equal to what exactly uh the type would be basically i have to pick the type from calculator and that will be this one so we will do gr dot leave underscore type then we will go for accrued so basically we have to populate the accrued value that means how many leaves i have accrued so for example for yearly so if you are basically providing leaves that means for yearly you have 24 leaves but let's say for example this user work was created in uh maybe in april that's that's basically fourth month of of the year in that case you cannot assign 24 leaves so your logic should be in that way that it will auto calculate the rest of the months of the year and then assign the lease then put in the bucket how exactly you can do that for that you have to do little bit calculation and that is also with a script and what exactly you have to do you have to basically calculate the current month that in which month that user was created and then you have to divide it basically those number of leaves you have to divide it from 12 and then in that case you can just assign those leaves you can just multiply it and then you can assign it so you have to make sure so for example you have 24 leaves and i have to divide it from 12 and the remaining ones maybe are like eight so in that case i will assign eight into two and that is 16 leaves so how will i calculate it so in that case what i will do i have to do it above this this particular piece the reason behind it because i have to calculate it that's how i will populate here the accrued leaves and all the balance and other other details so i would what exactly i will do now i have to work on the basically a month basically date and time so how will i calculate it basically i have to check the current month how can you do that so for that i think servicenow already provide you the api that is a glide date time api and it has some methods with the help of which you can basically get the data related to current month current day uh what what date it is that's something you can get so how exactly we will do maybe we'll just create um i think just a variable gdt which is this glide date time a variable i have taken and it's up to you what variable you want to take i will just give it new glide date and i will just mention time and i will just parenthesis and then i will just do uh semi semicolon so now what i have to do i will just now i have to get the current month how can you store the current month i will create a variable and here i can do g d d dot you will see that automatically i will see get it should be get month here is the one it says gets the month stored by glide date time object expressed in current users time zone i will just press tab so we will have this current basically month and now why i am storing this current month because that's what the user got created so i can just get the current one that's it that's what i can do and then that's how i can do the calculation and now i have to do that how many months are left now how exactly will calculate that's something you can do uh maybe i will just give a variable days left and then i can do here 12 minus what exactly i will do here current month now this way i will be able to calculate that how many months are okay i have given days left that's wrong so i had to do month left not days left so i will just mention that how many uh months are left so that's how it automatically will calculate for eight months left we'll store that how many months are left now i have to calculate that how many leaves are there per month how will i calculate that so maybe i will just store in another variable that will be per month equal to now here i will check the field in my calculator over here because if you will go here we have this leave i will check this leave and then i will divide it by 12 that's how i will get the monthly leave so if i check this it is leaves and i will come over here go to my business rule and here i will do gr dot leaves that's it that's a field we have and i can just divide it by 12. if i will do that i will get the monthly leader how many leaves are there monthly to basically be assigned to an employee to for for that particular country so that's that's up to achar what process they have at policy they have for a specific country it can be 36 as well then you will have um i would say leave 36 divided by 12 that will be three three leaves per month so now so overall we have months left over here eight i'm just talking about the example we have and then we have uh monthly leave is about two and now i have to calculate that how many leaves are there because now i have to populate the leaves that's something i have to store so i will put over here uh total leaves and what exactly is that i have to basically multiply per month into that's basically my per month leaves and i will put months left i will copy this one and semicolon so this basically variable will store that how many leaves will be assigned for this particular user who have which which is just created so in that case i will go to accrued now so i will populate a value in accrued and if i go to bucket let me check the field we have accrued so i'm sure i can put the right um value so i can just remove this so it is accrued and if i do equal to i just have to copy this one so this is the first time creation when we are creating the bucket for for that employee and if i come over here and this time now i will do leave bucket and i can do dot now i have to put the balance and if i come over here we have this balance and i can just do now i have not taken in leaves yet so how many how many leaves will be basically show in balance it will be same there will be total leaves so i can copy this one paste it over here semicolon and the next is if i come over here then we have uh i think balance taken now as of now because it's a new user so i'm sure that this user must have must have not taken any leaves so in that case i will just do leave bucket dot and i can just do taken equal to and you can just mention zero that's what this user has taken so no leaves as of now and then we have now because it's very important how we will recognize the employee and that is this field employee so i will copy this come over here and i will do leave bucket dot employee equal to and i will do so that's basically our current racket so you can use that's a reference field so i can just do current dot society i think this would definitely work this would definitely work that current dot society and at last i can do leave bucket dot um we will do dot insert that's it so you're done that means this will create the racket in this particular table as per the calculation we have in this leave calculator table now what i will do i will as of now like you can see we have just created for this month uh yearly but now we will do it for monthly so let's go for a monthly and monthly if i let me check here till here i will do like this and i will do maybe else if and i will copy this one i'll do copy put it over here and here i will put m that's a monthly and i will do like this now we will we will have to create similar kind of racket but you can't use the same i would say same variables because otherwise it will give you an error so in that case i will just copy this one and put it here over here but you will definitely get the error it says leave bucket is already defined but that's okay we will change it so i will put m here that means it's leave bucket monthly that's what the logic we are going to put here so we have glide record and i will change all of these as well so maybe i will just copy this and change our variables over here and like this so i will just change all the variables because you cannot use uh same variables in the in the same script and that is also in the same function you cannot use that so if those are different functions then you can definitely use then you won't find any kind of issues now i have this uh else if and now how exactly we will assign it uh per month now per month is basically that every time in every one month uh basically uh if i talk about divided by 12 so whatever leaves you assign over there for example 24 it it will divide it so that will be divided by 12 and then it will assign it monthly so in that case and it will also keep on adding it so if i talk about over here so let's say i will do where i will just capture the value of monthly leave and that i can do per month and i will do have i used this per month yes we have used it so maybe i will just put it per month m so i will do like this and here i can do gr dot leaves that how many leaves i am putting over there and i can just do leaves so whatever leaves i have i can just mention over here divided by 12 that's how i can calculate the monthly leave and i can do like this now for example a user got created in maybe in january in that case in january i will just have two leaves that means because that's how it will calculate so it will uh if i talk about the bucket perspective it has to assign only two leaves so in that case if i come over here and accrued i will just copy paste it it will not be total leaves it will be per month and if i talk about balance i will again make it as per month that's it taken is again zero and employee will be as it is and i can insert it but there's still one thing that how it will add basically next leave for example i joined in april and now in may it will add two more leaves how exactly it will do that that calculation you can do at this level we will see that logic as well that how can i increase the leaves every two months as per the accrued so maybe in a crude we have two and i have not taken any leave because that's what the calculation we will we will uh do uh basically in later scripting because we have to uh perform the whole calculation that everything every data should be populated as per the calculation so here i have a crude two for example in the first month second month it should be four and that's what the calculation you will do so we have to write some kind of scheduler uh so that it will automatically add the leaves but that is also as per this calculator it should not extend but if i talk about i think we still need to check that how many months are left that's how it will calculate and keep on adding two leaves or whatever leaves as per the calculation every month so that this data this bucket will keep on increasing but you have you also need to make sure that how many leaves employee is taking and you have to keep on updating this bucket so i think we are done with the this particular script so i can save this now and i will show you because we will do a test as well whether this is working or not so what i will do um i will quickly give a check that i hope everything is fine i don't have any syntax error as well um all the field names everything seems to be good i have a variables write variables okay so i will just click on save now so this will create business rule which will create leave bucket racket now we will do a test so i will come over here we already have this 24 we have this for india and we have this yearly gen 2 december what i will do i will go to sys underscore user dot and i will do list now i have to check that location uh where country is india so maybe i will uh i will add country code um this one okay not this one country code and we will see if we have country code india i think its country code is empty in out of the box instance we have in personal developer instance but i can still do one thing let me check if i go to maybe relate show related yes i can do show related if i go to location i can go to location fields and if i go to country and here i select uh maybe yeah starts with india and i just run this let's see i'm not getting any uh location over here uh so let's let's create it so for that maybe i will go to locations i will open it in a new tab so in order to do the testing you have to make sure that the country is populated and if i just pull the country and we can just populate to any any particular location i think that's not a big problem or maybe i will create a location let's do that it's it's much better so i will do maybe delhi and country i will do india and i will just submit it so new location is created so in that case i will go to users table i will click on new now here do i have opportunity to provide location no but we will see that so this is i would put delhi delhi.user dot demo first name is delhi user demo and i will just click on save so i have created this user but i have not basically assigned any location i have not updated any location and that is something i can do i will maybe go back over here and i will do says user dot list and i will search for the user that's this one i will just bring location so we already have here i will go in the right hand side you can see location is empty i will double click and i will select delhi yep we are getting delhi i will save this and you can see there was okay location was not updated but will it create the racket i think that's something we have to make sure that while creating it we we should we can fill the location so maybe because this will not update the bucket let me see if bucket is updated if i because we already have inserted the racket if i go to bucket so you can see we do not have any record over here so the reason behind it because we also didn't put the employee number that's a i would say a main condition of creating the bucket so what i will do and i think this time i will make sure i'm creating the user uh properly uh so maybe i will do one thing i will put the location i think employee number i think it's not there i can't see employee number that's okay i will put them on the form both employee number and location so that i can populate it now it is saying because i'm working in leave management scope now leave management system scope so i cannot edit this if i really want to edit this i can i have to basically edit in the global global application i have to change the scope so in that case i will close this i will change the scope of the application from developer view and i will go to global i will come over here and i will do says underscore user dot list i will open this particular record and i will go to form layout and now you can see i can edit the form layout so what i will do i will look for location so we have this location over here i will put it in this particular selection over here so maybe i will put it like this [Music] department i will put location and i can also put employee number i don't see employee number and i go over here yep we have employee number and i will also put employee number at the top and maybe i will put it just below location so that we can just edit it but it's up to you how you want to change the layout uh idly if i talk about the user creation like most the organizations they uh create the users and service now with the help of ldap or maybe kind of different kind of transfer maps and imports so you won't do it manually but your business rule rule will definitely run if they have the employee number so in that case i have created this and i will now i will just close this one as well and i will go to sis user dot list and this time we will give employee number and we will also select the location so let's say delhi dot user dot demo one and i can just do delhi uh user demo one and i can select the location we can see that will be delhi and i will give the employee number maybe employee zero zero okay it says employee zero one that's a number or maybe i will just keep it zero one and or maybe just keep one for now that's okay and if i click on save let's see what happens and if i come over here will i get any record absolutely you can see that the record was created and if i select over here but you can still see i think the calculation is not correct so i have this for a casual leaf and i have the location i think uh that is what you mentioned in okay let me check the leave calculator if i go to calculators uh it's india i think so it should work because we do have this uh india country and i have this 24 leaves and i have it for casual and this is for yearly so maybe there should be some issue with our calculation so let's go to our script over here and if i go to our business rule because i'm sure there should be some kind of issue if i check this like gdt new glide date time this is totally perfect i'm getting them get month so i'm getting the current month this is also okay how many months are left okay i forgot it guys yes we only have four months left and it's working perfectly so this incident is created in the month of september october and it automatically calculating october november uh uh september october november december i think it's it's not uh basically uh i think dividing it properly maybe so let me check so idly for okay so we have two months left because we are in october right now and we have november we have december that means two months and if you divide the per month leaves it is two and two into two is fours that's what it is saying that if a person is joining in october that means if that user is created in october then that month is not being i would say no included and if you really want to include it you just have to do one thing i will i can tell you here in the current month like whatever current month you will get whatever current month maybe you can just do i think plus one now this way you will you will divide basically uh subtract uh so for example here we got eight uh here we got uh ten that's a tenth month and this will do ten plus uh eleven i think it should do minus sorry it should not be plus it should be minus so it will calculate 9 then and then 12 minus 9 is 3 that means current month will also be included let's let's test it i think that's much better i think if we are changing the script we have to test it so that you can make sure that your script is working fine so if i come over here and i just put 2 and i just put 2 and i just put employee number 2 and i just insert and stay let's see what happens created so second user is created now and if i go over here will i get it yes i will absolutely and this time you can see we have six leaves so because if somebody is joining in october so it's totally up to you i think i i'm just creating just an example but if you're really creating this kind of application the the kind of variables the kind of calculation the kind of logic it's totally up to you and the requirement you get from your customers so this is how you create a business rule to to fulfill the functionalities so i hope you learned that how you create business rules and how you can also test it at the same time whether it is working or not so we are done with the first functionality that is creation of leave bucket so let's go to our next functionality
https://www.youtube.com/watch?v=ZktM9fLE4rY