#2 5 Use Cases of Business Rules | ServiceNow Practical Use Cases
[Music] in order to subscribe to my channel please click here or click here please share comment and like my videos and channel hello guys welcome to sas with servicenow this is our second session of servicenow practical use cases in this session we will talk about five use cases of business rules scenario one when a story record is created then it should automatically create four different scrum tasks rackets of development testing analysis and documentation so let's see this requirement in my personal developer instance so you can go to agile application where you can see the stories module now here you will see the list of stories so the requirement is whenever i create a new story so if i click on this new button and just type test here and click on save so whenever a new story is created and if you go little bit bottom here you will see scrum task related list so as of now it is not creating any instagram task automatically but that's a requirement so if you click on this new button that's a manual creation of scrum task but you can see here we have four types of scrum task now this is something you have to create automatically whenever story is created at the same time while creating these scrum tasks you also need to populate short description as the same type you have here like analysis documentation coding and testing so that's something you have to populate here now in order to fulfill this requirement because the condition is that whenever story is inserted in the story table then it should create these four scrum tasks in that case we will write a business rule on story table and we will create these four scrum tasks automatically so let's go to stories from here i will go to business rules and i will click on new button so that i can create a new business rule here is the form for new business rule and now i will make it a little bit large here i will provide a name so name can be create scrum tasks and i will check this box it will open the advanced option here where we can write the script and i will go here condition i can select after insert so this is the condition we can select and now i will go to advanced and here i will make it full screen and i will make it little bit large so that you can see the script properly and we will start writing the script now before we before we start anything of creation of those chrome tasks first you need to create a variable where you can you can store those four values how we can how can we do that for that i will create an array so for that i will do where type now we will follow the basically sequence we have how exactly i can see the sequence for that i need to go to same stories and you also need to know know the table name so i will open it in new tab so let me open this one and if i go little bit bottom here we have scrum task i will click on create new and i will just try to go to this field dictionary i will click on configure dictionary here i have dictionary and if i go a little bit bottom here you can see we have all these choices starting with analysis coding documentation testing the backend value we have for these choices are one two three four which is something we have to use in our script so let's start first we have to populate these values capture these values in an array analysis coding documentation testing so if i go to my script here i will start with analysis comma coding comma documentation comma and then we have testing let's just verify yep that's what we have that's it now what exactly we have to do because we have to create four records so we can't just create one record because we have to use loop them loop in that case we will use loop now for that we will use for loop so i will do i equal to zero semicolon i less than type dot l e n g th length and then i can do i plus plus now we are going to loop because we will create four records so we will write a script which will create one bracket and then with the help of this loop it will create four rackets and how exactly we will create it so we have to insert it for that we will do var we will create a glide record variable so we will do new glide record now here we have to mention the table name in which we have to create the record and that is scrum task table so i will go to dictionary here um and we have to see i think this is not the real table because this is inheriting those choices so let's go to our table so i will go to stories again so here we have stories and if i go to any story and i go little bit bottom here and i will just click on create new so that i can just see the table name the table name here you can see it says rm underscore scrum underscore task so now i will go to my script again and i will put the table name rm underscore scrum underscore task so this is the table name now you have to initialize basically this variable so we will do gr dot initialize semicolon now we will populate the fields we have we will basically push the value now we have to push short description so what we will do short underscore description now that should be basically the first array object here the one value we have here so in that case what we will do we will just type type and i can just do i so in this case it will automatically push the value and then it will keep on looping it so that means first value will become here analysis then coding documentation testing now you can see we have written these basically we have created these array in sequence that means the sequence we have in choice so in this case we have one two three four now i cannot use this i here in order to push that value if i will push i it will push zero which we don't have that value in our choice field here at the bottom you don't have 0 here so in that case how exactly you can achieve that what you can do you have to create another variable j and i will do equal 2 and i can do i plus 1. now what i need to do i'll just we'll do gr dot type equal to and i will make it j because it will do zero because i is zero it will add it and then it will push it here but what we will do we will also add a string here so that it it should not uh i think create any kind of problem so it will directly push that string value so i will just do semicolon here so it is done i think we have uh given the choice we have given the short description and now i will just do gr dot here we have insert so i will do semicolon i think our script is ready and i will click on save so our business rule is created it is saved now we will do testing in order to do testing i will go to stories module again so we have stories and if i click on this new button it will create a new story racket but at the same time it should create those four rackets as well so i will do test for scrum task task i will click on save and if i go bottom it has not created it what is the reason behind it the reason is scrum tasks are created but you have not populated which story you have to assign those current tasks so what you can do you just go to business rule now you have to go to your business rule and that's another field you have to populate sometimes developers do these kind of mistakes so you have to remember because you have to tag that particular value the current story which you are going to create which you are creating basically so in this case i will do gr dot and then we have a field if i go to here with this field is story now this is the field i'm talking about and if i go here and i do gr dot story equal to i just need to do current dot society that's it well that's a reference field we can just populate the society we can push the society and i will save it so let me just make it a little bit zoom first so that you can see the script so we have just added this particular line in the code and now i am just saving it so it is saved now this time it will definitely create those four scrum tasks so let's do the testing so i will go to stories i will just create new and i will give test for scrum tasks i will click on save now you can see it is taking a little bit more time because it is first reading it has a business rule which it has to process not that much time but it took some time that means your business rule ran successfully so if you'll go a little bit bottom here and you can see that all the four scrum tasks have been created without any issue there was some just one line you have to add because so that you can relate you can refer that particular story and how exactly it has referred so if i open this system task you will see you your story is referred here the test force from task so that means this scum task is related to this particular story and you also have short description as same as type so this is how you can achieve this kind of requirement not only for your stories table not only for your agile application but for any tables this kind of requirement if you get where you have to create multiple records you can use loop where you have to create record in a different table this is how you can initialize it and then create some rackets in the system scenario two there should be a field on problem form which should show the number of incident brackets associated with the same problem let me show you this requirement first in my personal developer instance so here's my personal developer instance now if you will go to problem form let me open any existing problem form so maybe i will open this one now here we don't have any fields so there should be a field which should show that how many incident records are associated with this problem that's count you have to always update now how exactly you can achieve this the high level i would say solution would be that whenever incident record will have any problem attached it should automatically update the count in this new field that's kind of logic you have to implement how can we achieve this with the help of business rule again and for before that before writing that business rule first we will create that particular field on problem form now let me just tell you one thing it might happen that there are some fields already available there are some functionalities already available but that does not mean that i'm duplicating it i am just giving the demo i'm just showing you the examples that how can you achieve the practical use cases how can you see the practical use cases for these kind of requirements so it might happen the similar requirement might be available in service now out of the box maybe so before that let me do one thing before writing that business rule i will create a new uh new field on problem form and that will be form layout here i will create and maybe i will do associated associated incidents that's a field name we have now we will make it integer it's a count it's a count we have to show so we will make it integer and i will do add it'll be here and now i will make it above problem owner field and i will click on save so this field will be added click on close so you can see it has this field associated incidents now we have to write business rule now but on what table incident table or problem table so the answer is you have to write that business rule on incident table because that's what you're going to update you are going to refer a problem record with incident so what i will do i will go to incident so i will just open a random list i will go to business rules and then i will create a new business rule so i will click here i will provide a name count of incident on problem we will check this box advanced now when exactly you will run this what condition would be the triggering for this business rule so the triggering would be after after insert or update so whenever you are going to insert any new incident or you are going to update but there is a check and the check that that condition is problem field so we have a problem field where you actually associate any problem bracket with any incident so in that case problem is not empty that is the first condition and i think it should be r i think you can just keep and problem changes that means this field is getting changed and it is not empty we will go to advanced tab now here we have a script so we will make it big screen so we have it's full screen now and i will zoom it again so that you can see the script now the requirement is that whenever i am associating any new problem it should automatically update the count so in that case what i will do i will write here where gr equal to new glide racket uh i think the table we have for problem is problem and i will do gr dot now here we have to find that particular problem which is associated with the incident so what for that we will do gr dot add query and we can do cis underscore id the society of the problem and the field we have on incident table that is current dot problem id so this is the problem racket reference field so that means it will search for that particular problem and then gr dot query and i can do if next so if it found the problem same problem record it will do gr dot next and then i can do gr dot so we have to put that new field so if i go to the problem let me go to the problem form and just get the name of that field now here's the field so if i i think i have to make it a little bit smaller and if i right click here we have this field u associated incidence so i will do show and i will copy this cancel this one come here make it little large and now i will do gr dot associated incidence and i will do one thing here now you have to add a count now if you will add a count directly if you will put for example one but in this case every time whenever you will add a problem it will always show one one but you have to add it with the existing one the existing value you have in that particular field so what we will do we will do whatever we have in that particular field so we will do like u we will just copy this one and i will do just plus one because whenever whenever incident is created or updated with that particular problem then it should automatically update it now you will think that if it basically and and the the main point is that it should change that problem field as well then only it should update the count now it might happen that user might update uh the same incident it might happen but in that case that's check you can also put but as of now we are just doing like this so i'm just doing you associating plus one and i think it should be good but for that i will just do gr dot update so that it can update the problem racket and now i will click on save so new business rule is created to count the incidents in that case i will go here i will go to let's say we will come here and we have this particular incident so i'm opening it and i will go to related records now this is the field which i was talking about so if i add a problem record here let's see this one and i click on save so i'm updating it so it will run the business rule the reason behind it because we are changing the value and it is not even empty and if you will open this particular problem record so it did not updates associated incidents did not update new glide record add query current dot problem id i think this is something we did mistake here so this should be gr.next not new record so yeah these are sometimes i think we do syntax mistakes so i will save it and i'm sure this time it should definitely update the count so maybe i will open this one this time that's okay i will come here and i will add a problem record maybe this one and if i click on save so i have associated that problem racket with this incident and if i go to this problem record absolutely so you can see that it has added one here now this is the problem so let me copy this and let me go to any other incident so that i can show you how exactly it is increasing the count so maybe i will open this one i will go little bit bottom i think it has already that problem maybe let me just update this one but that's what we want so i'm going to change this i will save it save this particular form this is saved and now i will go to this problem bracket awesome you can see that now the count is two because you have added this problem to another incident as well and if you will go a little bit bottom you can see we have two incident brackets associated with this problem you don't have to go down basically at the bottom to see the number of records you can just directly see here so this is how you can fulfill the requirement if you get this kind of requirement from your customer or clients scenario three if all the catalog task requests are closed completed then request item should automatically be closed so if i go to my personal developer instance and if i go to service catalog here we have items and here we have catalog task so if i open just item list we have this request items and if i go at the bottom now if i see this one i think this is already closed complete but let's say i'm making it work in progress so and if i go to request item again and if i go here and open that catalog task and i just make it close complete so whatever task we have for a request item whenever those catalog tasks are getting close complete it should automatically update the value of state field on request item as closed complete so this is the requirement the scenario three we are talking about so how exactly you can achieve this requirement in order to achieve that you have to write a business rule on catalog tasks table so what i will do i will go here and just go to directly to business rules for catalog task that's what we have catalog task here i will click on new i will give it a name so maybe update state of ritm when closed now when to run when exactly you want to do that now this is something you have to do when you are updating it so i'm just checking this box advanced and when we are doing after so after update and state equal to close complete now this should not happen for one because in one request item you might get you might have multiple catalog tasks as well so you cannot do it just for one record you have to make sure when all the tasks are completed closed completed not check available all the tasks are closed completed or not so what exactly you will do so whenever a catalog task will be changed to close completed that's what you are doing here so whenever state is or maybe you can do changes to i think you i think it should be fine we are just updating it and state is closed complete so in that case what i will do i'll do where gr equal to new glide record now we have to check first in scrum and this catalog task table so for that we will do sc underscore task and we will do gr dot add query now what exactly you want to check now wherever that request item is available which is basically on catalog task form as well this one and the name of the field is request underscore item so what i will do here i will do request underscore item that means that the basically the field we have to check that is on a catalog task and we will do comma current dot request item so this is the field we have you have to match that value and then i will do gr dot now here we have to check the state as well so for that we will do gr dot add query and i can do state now it sha state should be 3 that means that is something we have to check do we have any record where state is 3 and so every time you make it close complete and here we will do gr dot query so state is three that means closed complete and it has same request item which is part of this catalog task where we are writing the story that is the current catalog task which is getting updated with close complete now i'm going to do a query and now here i will do if gr dot next now if i do not have any racket so in that case i have to do not here that means there's no record so if i'm not getting any record so if state is three and i have request item so if all the records are are like that if i'm not getting any record here so but but if i'm not getting any record i think you should make it not equal to then only you can do that because if you will select state is three that means all are closed complete so in that case if you will have any record where you will have close complete then you don't have to update that racket so in that case if we don't have any so what i will do i will do not equal to and i will do comma so we will make comma here so basically uh state is not equal to 3 and then if i have next or maybe i think you can you can also do one thing that if you have all the records completed so if you have even a single record but in this case you shouldn't get even a single record that means all the catalog tasks are closed completed so in not available update that particular request item how exactly i will do that i can just directly go to vr g gr item and i can do g r item it's another variable we are creating uh we will do gr item and i can do new glide record and we will go to request item table now what is that table we can check it from here i will open the record and if i will go here sc underscore request underscore item so i can do sc underscore req underscore item and then i can do gr item dot we have to find the same request item so i will do says id here we will do comma and then i can put gr dot see maybe i will do this current that's something we can do we can do current dot request item if i have same request item i can do gr item dot query if i'm able to find that then i will do gr item dot i think i have not made it full screen and i have not made it bigger so i will do that now and then here i will do next next so that means if it found the racket it would update it because we do not have any record where state is not completed close completed so in that case i will do if here i can just mention gr item dot state equal to and i can just make it three that's it i think this is the right script i think let's just validate if everything is good so we are a glide record we are doing glide record on sc task table we are acquiring it request item if it's the same request item or not we are checking if all the tasks that means all the tasks where we have uh this request item are those all closed completed so if not it means if state is not true that means there's no there's no record where state is not three that means if it is there's that means we're not getting any record that there's no record available then only i have to update it if there is a racket then it should not update it because that means there is still task which is still open so i will make it safe so i am saving this business rule now and we will test it so in that case i will go here and we can just open the same task and let's see what exactly it does as of now it's closed completed so maybe i will just make it open it's open now nothing happened on this particular form as well and then i will go to task here and i will make it close complete now whenever i will do that you that business rule will run i'm saving it and let's see if it is updating so if i reload it it is not pushing the value here and i think if i will come here the reason it is not doing that because we are not putting a command to update it because that's a different table so in that case what you have to do you have to do gr item dot update now this will update the racket so i will save this this is done and if i go here i will make it open again i will save this if i come here reload it nothing happened you can see state is still open and now i'm coming here and if i make it close complete and i'm trying to save it if i'm doing that and if i go to my file yes you can see that state of request item got changed that means it did not have any other catalog task where state was not closed complete and that's the reason it just make it close completed automatically now in order to make sure that it is also working with uh basically two one or more tasks in that case you can create one more catalog task so what i can do i can just make it work in progress let's save this and i can i think i'm not able to create a new task here it is not allowing me to do that but in that case let me check if i can i can just do something to create it let me reload it if i'm reloading it it isn't working progress i don't have i think for that we have to create a new request so what i can do maybe i will just go to any any form catalog form basically so we will go to service catalog and if i go to maintain items we have i think multiple requests here and maybe i have this sample item but i don't know how many how many rackets it will create maybe let's let's see something else um we have this uh acrobat let's see if i can just create it and how many how many rackets it will create like brackets means catalog task and items if i go to try it and i just do order now so it is created i will directly go to request item here we have request item and here we have catalog task now what i will do i will create a new catalog task as well so i will do short description and i will submit it now in this case i will open these catalog tasks in new tabs both one and i will just close the old one so we have this request item and we have these two catalog tasks let me just first close complete this one if i do close complete this one does it close it if i go here no it is not doing any activity here because there's still one catalog task open and if i go here and if i make it close complete now and click on save you will see the magic if i come here you can see it says close complete that means both the tasks are closed and i have this close complete state updated now this is something happening because of some workflow in the back end for this particular catalog item but overall our aim was to update this particular state field so and it is updating it is making close complete whenever all the catalog tasks are getting close completed so this is how you can achieve this kind of requirement which we mentioned in scenario three scenario four users should not be able to resolve the incident if change record mentioned in change request field is not closed let's see this requirement in my personal developer instance so the requirement is if i go to incident form and if i open just open any record let's say this one and if i go little bit bottom now here you will see this field called change request now if i add any change request here maybe this one and if i save this and if i try to open this particular record in a new tab let's see the state of this change now state is still new it is not closed now this is the change request now as if i talk about idle perspective process perspective if you have you're associating a change request that means in order to resolve this incident you are going to create this change and implement it and this incident is still open and if i'm trying to resolve it so let's say i'm going to resolve it and i'm clicking on save it says following mandatory fields okay i will fill the mandatory fields i will make it mandatory now and change is still there and if i save this i'm saving it you can see state is updated with resolve state even if the change was not implemented and if i go here and reload it state is still new basically it is not implemented yet so in that case the thing is you have to make sure that state of the change is closed captioning not properly so if available go to choice of choice of state field of change request table and if i go here and i have this change request i will make it show matching i have this closed here you can see so basically the three the value is three so the state value is three then only you will you should be able to resolve that incident so what i will do i will go to my incident and here we will create a business rule so i will directly go to business rule and i will click on new and we will just mention here about resolution resolution if change is not closed in that case let's see if we have to write the script for this now here before updating so that is basically the triggering condition of this business rule so we are going to do before updating and change request is not empty that means change request is not empty and we are also doing state state as maybe we can do change to resolve so if i'm making it resolved and in that case i have to check whether the record associated with this particular incident record is closed the change record which is associated so in that case do we have to write the script if i check here action i think i i can directly check it as well uh if i will update it and change request is not empty let's say if i if i can do it directly with this condition so maybe i will do change request can i do show yes we can do show related records and if i do state so maybe change change request we'll go to change request field here i think in this case we don't have to write a write a script and if i go to state here we have state and i can just mention here closed is not closed is not closed and if it is changing this result and it's not closed and change request is not empty in that case i will go to action here and i will do abort action if you want to add message i will do that associated associated change is not closed yet and if i'm clicking on save let's see if it works so i will go to maybe any incident record maybe this one i think it's already resolved but we have to go to any not resolved one so maybe this one and if i come here and if i just select maybe any change i hope it is not closed it should not here because i think you shouldn't i think you should not be able to attach any uh i think close change but it depends uh so in that case i will just save it first it shouldn't do anything and now i'm going to resolve it so if i click on save okay it is asking me to put these fields and now i am click on save it says okay action about it it says invalid update associated change is not closed yet and if i reload it you can see the state went back to the same state which it was earlier that means the previous value that means this change record is not closed yet so you have to make it close completed then only you can do that now you can see that we were able to achieve this functionality without writing script but if you want to achieve this kind of functionality you just need to go to advanced tab you have to enable it you have to write a script you have to basically do glide record you have to search for that particular change racket and then you have to check whether the value is closed completed three or not if the value is not three in that case you can just abort the action with the script as well so this is how you can achieve this kind of functionality if you will get this kind of requirement from your customer or clients so this was scenario four scenario 5 if users updates work notes of problem task then it should update same work notes in parent problem racket as well with highlighting problem task number so let's take a look what exactly this requirement is and how can you fulfill this requirement with the help of business rule so this is my personal developer instance so the requirement is if i go to problem and i just open any problem record let's say this one in this case if i will go to problem task so we have these problem tasks in the related list and if i create any problem task so i'm just creating a problem task and i'm just submitting it now the requirement is if i open this problem task and i have this work notes whenever type i any work notes here and i save it i post it then this work note should automatically be populated in the work notes of parent problem racket that means here as of now you can see that it's not doing like that but you have to put it over there how can you do that so for that what we need to do we need to go to problem task table and we need to create a business rule on problem task table not on problem bracket so i will click on create new so we are going to create new business rule this is the new form of business rule so i will give it a name so maybe update work notes to parent i will make this advanced checkbox active and now when exactly you should do that we should do that after once i am done with the updation so i will do update and this thing should happen so i will put the condition that when exactly this business rule should trigger so i will do that work notes changes so whenever i'm adding any work notes it should trigger this business rule when i'm updating anything so in that case i will go to advanced now how exactly i can i can push those work notes on problem racket so what i can do let me make it fuller screen and a little bit bigger so we'll write our code now i will start with because i have to get the work notes first so whenever it is changing so what i can do just create a variable so i will do work notes maybe i will do in caps work notes and i will do equal to here i will do current dot now here you have to first take work notes but whatever the last work note you have that is something you have to push to parent problem racket in that case i will do work notes and i will do get journal entry so i don't think we have that particular element api here so i will do journal entry now here i will do if you will do -1 you will be able to fetch all the work notes you have in in that particular racket but i just need the last one so for that i will do one here and then i will do a glide racket so i will do v v r g r equal to new glide record here i will put the table name of problem and i will do gr dot add query now this is something we have to query the same problem record which is on the problem task now i'm sure let's check the name of that field the backend name so if i go to the problem here if i open this one i have this problem task yeah here's the field so if i i think i have to a little bit make it smaller and now i can take it so uh okay it's problem so i will go to my script then in this case i will put that problem and here you can do current dot problem but here you have done problem it should be society not problem you have to search for that cis id and that society is in that reference field so we have current dot problem now the problem is the field of this problem task which we are working on so then we will have gr dot query and then we will do if so if that problem record is available so we will do gr.next if that is available then i will do gr dot work notes work underscore notes equal to now you have to update it so if you will do gr dot work notes equal to and i can just do work notes we are going to get this particular option here this so it will store the latest work notes and capture it in this particular variable and then it will be populated here so it will push that value to the problem bracket work notes and i will do gr dot update you have to update that as well so i think this is the script so let me just save it and let's do a testing so business tool is created and if i come here and let me just reload the form and if i type here test work notes and if i click on post so it is posted and if i go to my problem record yep we can see that you can see it says test work nodes it updated automatically but there was one more requirement the requirement was it should also populate because in problem rocket you might have you can have basically multiple problem tasks so you have to know that from which problem task this is has been updated so what i will do i will go to my business rule now i will make it full screen now here what you have to do when you will do that gr.work notes what you can do you can do this work note is updated in and here you can do current dot number that's a problem task number so i will do this work note is updated in current dot number and i can just give maybe i will do slash n so that i can make it in the next line and i will give um yeah i think this should be good so this work node is updated in we will get the number and then we will have our work notes so if i will save this so this is saved and if i come here and i go to the problem task again test work notes with number work notes with number if i click on post you can see we have this here but now we will see whether it it is updated in our problem record or not and yes you can see that it says this work node is updated and it shows the p task number and also it it is showing you uh the work notes so the whole work node is here now if you want you can definitely cut these values you have to use some javascript so that you can just cut these values but as of now this is the main and you can also see the timestamp ideally timestamp would be quite same because you you are doing it right away so if you want to show it it's up to you but if you want to just remove these things because this is also showing you who is updating it and i think this will also update it right here as well so i think you can remove that so as of now you can see with the help of business rule you are able to update parent racket as well with work notes so that's how you can achieve this kind of requirement which is mentioned in scenario five so i hope you like my video please like share comment and share my channel i will keep spreading the learnings have a great day
https://www.youtube.com/watch?v=CPCRyT10zRM