logo

NJP

#10 Calculation with Business Rules in ServiceNow | Learn Application Development | LMS

Import · Nov 10, 2020 · video

[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 the next feature we have to add is calculate leaves so as of now we were just doing some changes on the front hand side that means we were populating some data we will calculating the duration we will populating the leaves from the bucket but now we have to work upon the back end side that means what exactly uh when when user will submit the request how the leaves calculation will happen how it will update the leave bucket and when exactly it will calculate so basically if i talk about the process perspective when when somebody approves the leave then only you will consider that hey now i have to deduct the leaves i have to mention something in taken and i have to update something in balance as well that's what you will do so in that case what i will do i will go to my personal developer instance so this is the basically form which user will submit and here we have a status so let's say i have submitted this this particular request and if it's get approved that means i i got the approval now i can go for leave so maybe duration was three days over here that means it should deduct the three days that in taken in our leave bucket that means if i go over here here taken should be updated as three and balance should also show after uh basically uh subtracting it from this uh accrued and i would say i think it should show it from balance that's that is something you can mention so whatever as of now you can see we have balance 24 and if i will take three leaves that means i should see here in in in the balance i should see 21 that is what i should see so that's called leave calculation so how would i do that so i will create a business rule for this so in order to fulfill this requirement we will create a business rule on leave request table so i will go over here and i will just close these tabs i don't need it for now what i will do i will quickly go to create application file i will go here server development and i will double click business rule because we will create a business rule for this for achieving this functionality and i will provide a name over here and name i will put maybe calculate i will do leaves and i will select the table it is checked and now i will if i will just maybe make a little bit small screen uh here basically we will do a async we will make it asynchronous because we don't need this data right away so i can wait for it so i will make it async and i will do it update now when exactly that condition would be uh i will make it for example i will do approval is approved and status is submitted if i will do that that means if these conditions will match then only it should basically update the leave bucket so what i will do i will go to the advanced and here i will write a script so i will make it a little bit bigger full screen and i will write the script over here so what exactly we have to do we have to basically uh glide racket the leave bucket and we have to check that which request has been submitted which first of all which type of lead uh which type of leave and which particular user has requested it so in that case i can do where gr equal to new glide record and i will put the table name over here and table name is leave bucket one so i will just copy this table copied and i will paste it over here semicolon and here i will do gr dot add query and i have to basically check the employee and if i just do comma now here i have to check the record requested by so if i have current dot requested i think that's a field maybe let's check it quickly if that's the field because field name should be correct in order to uh so yes we do have requested by and if i come over here requested underscore by and i will just do semicolon so this will check for the current records requested by and then i will do gr dot add query now i will select check the type as well leave type so i will do single quotation and i will put here leave underscore type and i will do comma and that is current dot leave underscore type and i can do semicolon and here i can just do gr dot query semicolon i just have to check the one racket that's it i have to find that particular record so if it is matching that means if it goes to the next record and if i'm able to find the record then i will update it simple it's quite straightforward so i will do gr dot balance equal to so what exactly i have to do i have to basically subtract from balance i have to subtract the duration which i will get it from the request so i will do gr dot balance so i have to populate that how much balance would be there so i will do gr dot balance minus whatever i i have value in duration so i will do gr dot current dot duration and here i will do gr.taken so whatever leaves i have taken that will be just duration so and that will be gr dot taken so how exactly i will i will populate the taken now it might happen that in tekken you might have zero or maybe two as well so you cannot directly put uh the duration because you have to calculate it so whatever view value you have currently you have to add with the duration and then you have to update the gr dot taken so that it updates the right record right value so i will do current dot duration and semicolon and i will just do gr dot update because that's a table we have to update that's it so if i i think everything should be good at query just checking the syntax uh requested by leave underscore type current uh we have comma seems to be good gr.update okay i think all seems to be good i will just click on save over here now as of now i will just approve the request manually so i have admin access i will just approve it to test it so we have created this business rule now we will test this business rule let's see if it is working or not so i will go over here and i will go to maybe request so i don't have any requests right now and i will just create a new request so i have this for system administrator i will select the casual leave it will show me how many leaves i have i have 24 i will select the dates now maybe i will do it from 6th of october till 9th of october i have 4 days and i will do to go somewhere well just an example i'm just putting over here um i'm not touching this what would happen idly when you will submit this but as of now it is not doing it automatically but we will make it automatically so i'm just selecting submit myself because i have admin access just to show you the demo but we will make it automatically here we have approval i'm not approving it right now so i'm just creating the racket so you can see it is created but i'm not doing anything right now uh it says okay it is also updating these rackets why exactly it is doing that i think it should it should be doing this maybe because of some business rule we have written let's see how many business rules we have written that's the reason so you have to make sure whenever you are getting something like this it might be because of the existing business rule you are you are you have written so i don't have any current dot update if i go to business rules i have this calculate leaves i have this calculate bucket uh create leave bucket maybe this one um i think it is for user i think we don't have any any um i think it is just showing let's see system okay it is just showing the same user has modify modified it but that's fine for now now what i will do i will approve this so that's the condition of that business rule so if i will save this you will see that it will update the bucket maybe we will open the bucket in a different window and i can see the value changing so i will come over here so you can see we have these rackets and these data these values i will come over here and i will save this let's see what happens it is approved so this particular request is approved and you can see here this taken is updated and it is also showing the balance as 20. a crude is something that how much you have basically uh uh basically uh basically you got in your bucket like total total number of leaves you got till now that's what the accrued is and then i have just recently requested for a leave and that also got approved and that's the reason it is showing here is four and it is showing balance as 20 and it's showing the data correctly perfectly and i will also i will also show you one one more thing if i will come over here and let's say i'm opening uh opening a new request for the same casual type of leave if i select casual you will see the change now see over here i had a crude 24 uh taken i have four now and i have balance as 20. that means are all the scripts are running perfectly but the one thing as you can see here that these these are not getting updated automatically so we will also update them with the help of some kind of maybe business rule maybe or we will see maybe with the help of flow designer we can update them as well approval will definitely go to someone which as per your requirement but but we will we will also automate this rather than putting it as manually so that's how you create uh business rules and as per the requirement we just had to calculate we have to update another table but that's how you write business rule and that is also async and that's the important piece over here so if if you don't have any kind of dependency that you have to show that data right away to the user in that case you can always you can always create the async business tool

View original source

https://www.youtube.com/watch?v=_s9qYqFYB8M