logo

NJP

#2 What is Before Business Rule in ServiceNow | End to end Tutorial of Business Rules in ServiceNow

Import · Sep 15, 2020 · video

[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 sassword service now in this video you will learn about before business rule you will learn about what is before business rule and how it works what is before business rule before business rule is triggered when user submits the form however before any action is taken on the record in the database that means if you want to take some action before data gets saved in the database then before business rule is used if i give you a quick example the user opens up the form and fills the data of the in the fields you have on the form now when user clicks on submit and that particular data basically gets saved or pushed into the database now between those duration that means after clicking on submit before that data gets saved into the database if you want to take some action that is called before business rule major use cases of before business rule so let's talk about some major use cases of before business rule which will help you to understand when exactly you have to use before business rule abort update of racket if form does not have expected value if user fills some value in one of the field and you don't want to update that value in the racket as per the values of other fields then you can abort the action with before business rule another use case is populate assignment group before creation of incident record another one is users should not delete a racket if condition does not match and the last one we have is date should be updated before insert of a racket so if you want to populate something before insertion of a record before business rule is definitely used in those cases before business rule without a scripting let's create few business rules without scripting in my personal developer instance so our first scenario for before business rule is and that is also with insert so we will create different types of business rules with different operations we have like insert delete update or query so we will start with insert scenario that user should not be able to create new incident if category is inquiry or slash help and assignment group is not service desk so that means you have to restrict the user that if assignment group is not service desk and category is inquiry help the user should not be able to create that incident and how can you achieve it you can achieve it with the help of before business rule and that is also before insert so if i go to my personal developer instance and i go to incident i will just open list of incidents and i will open the list of brs now configure business rules here we had that previous business rule which we created so i will just make it inactive because it should not conflict with our use cases and i will create a new business rule so i'm going to give it a name as demo for before without scripting that is just with configuration so click on this now here we will keep selecting before that's what the practical example we have to show and here i will select insert this time so i will select insert now here i will select the condition and the condition is assignment group is is not service desk and category is inquiry help and i can just go to actions now i don't have to set any value i have to abort the action i have to stop user for creating this kind of incident so in that case i will just select this abort action you can see set values is gone and then i can just select this add message why we are doing this add message i will show you so you cannot create incident without assigning to service desk if category is inquiry slash help that's it and i'm going to save this so it is saved now i'm going to create a new incident so category is this and i will select the caller let's say this time i will select analyst and if i select the test for br and if i click on save what do you think let's see no it says invalid insert and also says that message you can see at the top it says you cannot create incident without assigning to service desk if category is inquiry slash help in this case user cannot create any incident if it matches these two conditions the condition which we mentioned that is assignment group should should be service desk if category is in query slash help so this is the first use case with configuration without scripting and that is also for before insert the next scenario we have for update before update and the scenario is change the state of incident to in progress when user updates work notes so if i come over here so the requirement is if i put anything in the work notes it should change the state to in progress that's a requirement so what i will do i will go to my business rule and i will just go over here and i will do this time update and i will close this i will just cancel these one and this time i will do work notes i will find the work notes fields and i will just mention changes that's it and i will go to actions we don't need to do any kind of scripting for this as well i will just remove these and then i will do state equal to in progress and we will also make incident state to in progress we will also make it in progress and i will just click on save you are done you're done with the second requirement you have with the help of configurable business rule so i'm just going over here let's say i will just reload it or i will just remove it i can still create it this time if i will create it you will see this time it will not give me any kind of error the reason behind it we have to change the condition in our business rule so that business rule is not applied anymore so because we have just override with our new values so now what i will do i will come over here i will just do test for state if i click on post let's see if something yep you can see over here it automatically change the state to in progress that means our second scenario is also accomplished the next scenario we have for delete operation so that means before delete so if state is new then user should not be able to delete the incident okay let's see that so if i come over here i come over here and i just maybe i have to go to conditions so it should be before first i have to change the value it should be before delete and i have to mention before state before delete if a state is new if it stays new i will go to actions i will close this one and i will just do abort action and i will do you cannot delete and i will save this so now our another business rule is created for delete operation before delete so if i will delete it so that basically the triggering point is before before deletion that means if i will try to delete this record and if state is new then i should not be able to do that if i click on save i will make it new for now and if i click on delete you will see that this incident will not be deleted so if i click on this it is just giving the warning this is out of the box warning this is not our business rule warning so if i click on delete let's see what happens it says you cannot delete it basically routed me to the business rule that's okay but i can just check with the help of open incident let's see and we can check whether our incident was deleted or not and yes you can see here this was our incident you can see the same incident where we uh mentioned the test for state as well and we tried to delete it but because of our business rule it aborted the action if i will disable it for example let's say i'm going to disable this business rule you will see that now i will be able to delete it so if i click on here click on here this time this will be deleted and i can show you let me just close these tabs and i go to open you can see that basically the incident which we created it's not there it has been deleted even i think if i go to deleted records if i go to deleted records i'm sure that you can see that see we have this record yep we have this record which was basically deleted that's how you can stop the operation for delete as well so this is how you can create a business rule for before delete before br with scripting so let's create few business rules with a scripting in my personal developer instance and we will again we will consider all the scenarios we have like insert operation delete operation query operation now without scripting we didn't use query operation but we will use it in this particular case that means in before br and we will use that query but that is also with scripting so starting with first scenario we have for insert we will use scripting so we have users should not be able to create new incident if there is an existing incident with same short description and with same color so we have to use scripting because you cannot achieve this functionality with just configuration because you have to check the existing incident with the same short description and with the same caller so in that case if i go to my instance so i will go to list of incidents so here we have list of open incidents i will create the business rule so i will go to list of business rule this one is already inactive so i will keep it inactive but i will just copy the name so that i can just rename it and i will mention here demo for before with scripting and i will check this advanced check box we will do before and we will check this insert no condition i will directly go come to advanced now the requirement is that if the short description and caller matches then it should not create the incident that means users should not be able to insert create new incident so in that case what i will do i have to search basically query the incident table for short description and color so in that case what i will do i will do where gr equal to i will just make it big screen so that you can see my script i will do where gr equal to new glide record here i will put incident that's a table in which we have to do query i will gr dot add query and i will put short underscore description and i will do comma and this time i will do current dot shot description that's what i have to match and then i will do gr dot and i will do add query and i can put caller id field i will do comma current dot caller id fields i will do semicolon and i will do gr dot query semicolon and i will do if because even one record is found i have to abort that action so i will do gr dot next now here i will do current now which one i have to abort the current one which is basically we are going to create so in that case i will do current dot set abort action and you can just put boolean value that is true and i will just do semicolon and i can also put gs dot add error message you cannot create new record as you already have existing one now you can put different conditions as well whether that particular user has open uh open incident or not that's something you can put over here but i'm just using the random just to show you the demo so this is done and i am going to save it so business tool is created now we will do a test but let me just make it small screen and i will just go to incident list and i will just create a new incident but before that we also need to check for the same kind of short description so if i come to incident list so let's say we have this test for br and i can just copy this and we also have this atom wrangle so we will use the same user so if i come over here and i just mention like this so we are copying the same short description and i'm keeping the same color and if i click on save let's see what happens absolutely you can see that it is not creating it says invalid insert you cannot create new record as you already have existing one and if i change this let's say i do like this and i click on save this time it will allow me because short description is not matching with the existing one so this is how you can achieve with before insert and that is also with a scripting another scenario we have is for update that means before update and the scenario is users should not be able to change the state of story to work in progress if there's no scrum task created in this story so if there is already opened or created story and if user is trying to change the state to work in progress but if there is no scrum task attached to that story then users should not be able to basically change the state to work in progress how can we do that so if i come over here and i just close these tabs now this time i have to work on story table so i'm going to deactivate this for now i will just deactivate it and i will go to stories table so we will go to stories so i will just type over here stories if i do stories i have these stories here now this is the list of stories i have and i go to configure business rules i will create a business rule demo for before and that is also with scripting and i will check this advanced check box we will do before and we will do update and i will just check this and you have to also put the condition now it's up to you you can put the condition in scripting as well you can also put the condition but as i mentioned wherever it is possible to use configuration use that so what we will do i will just do state and we will make it changes too that means if somebody tried to change the state to work in progress then we will check if there is any scrum task so what i will do i will do where gr and before that i will also check the scrum task table if i let's open this one if i have any scrum task yep i do have i will just check the table name and the table we have is autumn underscores from task i will copy this i will come over here and now i will write the script where gr equal to new glide record i will put the table name so it has to be rm and i will just do semicolon i will do gr dot query now even if one record is not present so that's something we have to check whether there is some task attached to the story or not so in that case i will do gr dot query and i will do we will search for the scrum task and here i will just mention story that's a field and i will do current dot semicolon and you will do gr dot query semicolon and we will do f and i will do gr dot next now here you have to abort the action if i change the state that means if i do like this not if i don't have single racket with the same story that means same we i don't have any scum task with the same story which i'm working on right now where exactly i'm updating the state then it should do current dot set abort action and that should be true i will do semicolon that means if there is no scrum task for this particular record then it should not change the state to work in progress and i can just save this so it is saved and now this one has scrum tasks so i cannot test this over here so maybe let me just close this and open the list of stories so i have list of stories and let me open this one and let's see if i have any scrum task i don't have you can see i don't have any scrum task state is cancelled but let me make it draft first and i will save it and then i will make this story i will change the state of the story to work in progress if i do that work in progress and if i save this you can see it says invalid update you cannot you cannot change the state the reason behind it because it does not have any scrum task in this but if i will add it let's say i'm going to add a scrum task and i'll just click on submit so i've added this task on the same story as of notice draft and if i click on work in progress and i save it this time it not give me any error you can see that i didn't get any kind of error that means our before update business rule worked and that is also with a scripting the next scenario we have for delete operation that means before delete business rule and the scenario is configuration item cannot be deleted if there are tasks or incidents associated with the same ci now this is very important kind of scenario that you don't want users to delete any racket if it has any associated rackets like for example configuration item you select configuration items on different tasks like incident change problem so before somebody deletes that you should not be able to delete it if the same configuration item has associated tasks so if i go to my instance and i will just close this tab now we are going to work on configuration item this time so what i will do i will just to maybe i will just directly go to business rules and i will come over here and i will just create a new business rule so i can make it inactive for now inactive and then i will create a new business rule and that is also on configuration item table that's a requirement i should not be able to delete that configuration item so if i come over here demo for before with a script and i will select over here configuration item configuration item um here's the one cmdb underscore ci i will check the advanced button over here and i will do before and we will do delete and then and i will come to advanced what i will do i have to basically glide record the task table so what i will do i will do where gr equal to i'll make it full screen i will do where gr called new glide record and here i can just put the table name task semicolon and i can do gr dot add query now here i will select the cmdb table that is cmdb underscore ci and i will do current dot and i can just do cis id i have to check for the current ci that's what i have to check then i can just do gr dot query and if i do that i can just do if now even a single task is found basically uh associated with this ci i should apart the actions and in that case i will do gr dot next and so if i'm deleting it and if i have found one racket even one record i should abort the action so i will do current dot set and i can just make it true semicolon i will not give the message you can give it yourself if you want as per your requirements i will do current dot set about action even if i will i will find one record one task even its incident change problem i should automatically abort the action so i will just click on save so it is saved and then i will just go to configuration item and i will open it maybe from an incident let's say i will just open it where i have that configuration item so maybe i will take it from i don't have configuration item here but i will do like this i will click on ok i'm just pulling configuration item over here a lot of lot of our blank and if i i think we don't have single one so if i go to maybe service desk i hope i should have some of the some of the tasks where configuration item is tagged and we do have lot here and if i pull configuration item so here we have configuration item i click on ok and i check i will just filter it out all the empty is one so now i have all the tasks where i have configuration items available let's say i have this one sap aura let's try to delete it i have admin access i can delete it but if it it will have associated tasks then it won't delete it it will abort the action so if i click on this delete and i click on delete here now you can see it didn't give me any message but i still have that configuration item it was not deleted you can see it is still there because it tills still has at least one record one task associated with this basically with this ci and that's the reason i cannot delete it i will still try to click on delete and now this is the warning message just out of the box this is not the one which we created so i will click on delete again no it won't delete it if i can reconfirm i will come over here see i still have it i open this yes it is still there so that means this ci is not deleted so that's how you can create before delete business rule the next scenario we have for query now you can use before query that means system has to take some action before it queries the records so what action should take so the scenario we have is only operational ci should be queried if user is not admin if the status is operational then only users should be able to see those racket ci rackets so how can i do that so if i come over here now in order to restrict this functionality i will i will have to create a business rule that is a query business rule and before query as well so i will again go to ci business tools because that's what we have to create and maybe we can use this one so i will just open this one and we will just change the configuration and we have to change the script as well so i will come over here and i will just mention when to run before query i can just mention over here and i can put the condition so if basically gs dot i think there's something we have to use like gs.get user and i can do has role so that means if user has that role i can put admin i copy this i will cut this over here i will put it like this and i will do not i will just do not that's what i have to check if user does not have that role then here i have to do current dot add query we have to only give operational one how exactly i will do that so let me maybe go to configuration item table maybe i will do cm db cmdb underscore ci dot config i will press enter it will open up the dictionary for me all the configuration of cmdb ci table and if i go to cmdb here dictionary entries and i search for maybe status if i so i have this operational status and i just maybe like this and let me open this as well i will open this status so i have dictionary entry and how many choices i have and i have operational so we will use the one choice that's what we want so if i come over here i just mentioned add query operational status and i will just put it like this one so that's my choice i can put in the string as well and i will just do like this and i will click on save now i'm i'm admin right now so if i go to for example list of configuration items so if i put over here cmdb cmdb underscore ci dot list and if i press enter i will be able to see all the ci's i have in the system you can see 3023 and if i fetch operational status that's a field we have and i click on ok and if i come over here it shows operational and if i filter this out yep i can still see others as well even if i do filter out and i still have retired non-operational i can still see but for example if i impersonate a non-admin user let's say this idle user let's see that how many ci records this user can see because this is about querying when you click on the list it's all about query you are querying the records so if i go to configuration and i maybe or maybe i will maybe type here cmdb cmdb underscore ci dot list and i press enter you can see that i cannot see all the ci's i can only see 2974 that means if i come over here and i just get that field operational status maybe i'll put it here click ok you will see i'm only able to see the operational one and if i group this we will see if it's yes i'm not able to see retired one that's how query business rule works before query that means before hitting the table basically it is checking the rules the basically before business query rules whether i have to filter some brackets or not and that's how it is filtering the rackets and this is how you can also achieve a lot of requirement for your customers and clients so that's how you create before query business rule and this is how you overall create before business rule with different or database operations and you can achieve a lot of functionalities for your customer and clients

View original source

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