Before Query Business Rule ServiceNow with Example | ServiceNow Query Business Rule Vs ACL
hello friends welcome back to my youtube channel basico servicenow learning so in today's video we are going to talk about query business rule or we can say before query business rule so let's see what all topics we are going to cover today so in this video we are going to talk about what is before query business rule in servicenow and why it is used for we can also talk about differentiation between before query business rule and sales guys you all know right that acls are basically used to restrict the data in servicenow and the last we also do a practical implementation of before query business rule by implementing us use case so let's see what is a before query business rule so it is a type of business role in servicenow that we can use to limit what all records user can access from a given table so table can be a incident problem change or it can be any table right so it basically applies on row level so let's say if you are seeing a incident table list so it will apply on that if you are seeing a problem table list view right so it will apply on that in other words we can also say that it is used for data segregation on an instance query business rule executes before the query is sent to the database and it executes synchronously so i believe this definition is not that clear to you right so once we do a practical implementation this all things will be cleared okay so let's see and validate what we are saying here that query business rule executes before the query is sent to the database okay so let's see here we have a image where let's say if you are working as a user right and if you perform something in service now let's say you logged into application or you clicked to view a list of incident records then this query rule which we are talking about before query business will get executes so the definition which we are talking about earlier that very business rule executes before the query sent to the database so this basically is telling this thing then the database query happens and see here we have display rule before rule and after rules right you are aware of this so after this business rules comes into picture so once the database query makes then display business rule executes and once the user submitted the form let's say he filled the information into incident form and submitted it then the before business rule executes before the database gets update and once the database updates then the after business rule or async business will get executed so we are not talking about this we already have created the video for this right or in our youtube channel we are talking about here the query rule so let's see how we can implement it but before implementing let's have a understanding that what is a difference between a before query rule and an acl in servicenow we will also see that what and where we can use before query business rule with some examples so here we have some examples so let's say user cannot access the incident record in incident list view which belongs to xyz group let's say you have a requirement where you have to implement that if user belongs to xyz group he cannot access the incident request we can use or we can implement a query business rule here another example is login user can view only those problem records or incident records which are opened by him right there is another example which is like it can also be used to support data segregations so i believe most of you are like aware of domain separation right so it can also be used to support data segregation within domain separated environment for example user from xyz company cannot view the incident of abc company so these are few examples there are lots of other examples right depends on the requirement which you are getting that what kind of restriction you want to do into a table now let's talk about query business rule versus acl so because we all know that as per definition query business rule is also used to restrict the data right and acl we know that it is basically used to restrict the access okay so here is a difference of it query br provide access restriction only in row label as i told you earlier right that if you want to like restrict some data in incident list view problem list view or any table list view then query business will restrict that but asl provide access restriction global row label and field level so badly we use acl right but on for a row level access we can use a query business rule that depends on that what exactly the requirement is now the another difference is that when query business rule is restricting the access then user will not see any message by default at the bottom of the page related to the access restriction if we are doing same thing from acl then message such as number of rows removed from the list by security constraint displayed to the user which basically degrades the user experience right because once this is displayed to a user this type of message user click here and there to see what exactly is happening why i'm not seeing the records right so i believe that while working on servicenow you have seen this message at the bottom of a list view right now another difference is that we cannot debug query business rule from debug security module but we can debug asl from the same this is a very basic difference i mentioned over here just for as a point only now another is in service now the records are first queried and then after ac rules are applied so as we have already discussed that query business rule executes before here right so acell executes when this comes into picture right so this is the main difference guys this is a some of the differences between acl and a query business rule now let's see and implement some use case practically to have a better understanding of query business rule that how exactly it works and how we can implement that in servicenow instance so let's see what all use cases we are going to implement today so we have a very basic use case for today implementation so the first use case is that login user can only see the incident record which is assigned to a specific group and he should be a member of that group too another one is login user can only see the incident which is assigned to him so these are the basic examples or we can see use case which we are going to implement today so let's navigate to servicenow personal developer instance so guys this is my servicenow personal developer instance so before implementing the before query business rule let me give you a brief of that what exactly the use case which we are going to implement so we are talking about incident over here that goes to incident click on all so once i click on all see i have number of 68 records so here i have logged in through system administrator in another browser i have opened the same instance with a specific user so let's navigate there so here we have logged in through this basic olatum one he is an ideal user and a part of a specific group i'll also display that group to you so here this basic collector one is seeing a 68 records which admin was seeing right here we have lots of assignment groups different assignment groups right so we have to segregate the data or we have to apply a restriction here means this later basically item one can only see a instant record of a specific group let's say basically atom group so he can only see the records of basically atom group basically he is a part of that group so let me open that group and give you understanding let's say i am opening this basically atom group see here we have a group where we have two group members that is basically atom one the user which i have shown you earlier and we have another user that is basically atom two and we have a role here that is iti so here i am talking about these two users and this group so i am restricting the data that basically one can only see the records incident records which is assigned to this group so let me give you example if i say if i filter this so we have only 12 records means if the item 1 opens this particular page then only 12 requires will be displayed to him currently it is 68 records okay so let's see how we can implement it so to do this what we have to do type business rule here now click on new okay you can give any name over here i am selecting a table incident because i am applying this into incidents see that right am clicking at once because i am going to write a query in it or code in it when to run as i have said that we also say query business rule as before query business rule so here when is before order is depends on then we have see here four check boxes incident update delete and query so if we select insert update and delete this basically works when the when we have to perform the database query right so if we using this it means we are doing a before business rule we are not talking about a query business rule so for query business rule we are selecting only this query means we want to query before a thing hit to a database so now here we select the query only for query business rule now in advance we have to write the condition that what exactly we want to filter or what we have to do so as i have said that user needs to be a part of a specific group that is basic lactam group okay then he can only see the incident record which is assigned to that group only so i am saying here that if okay i'm applying condition i'm saying gs.get user is member of a specific group the group is this i can use the name even though i'm can also use the cis id so i am using a society of it so i am saying if a logged in user is a member of this group okay and gs.get session dot is interactive so here i am saying that if the session is an actual user session so this basically say this okay so here i have given the condition now what i have to do i have to write a query samsung current dot add query i am saying that assignment group should be this okay so [Music] incident com let me open incident form for you you most are aware of this right just for explanation that if in case if any beginner one of my friend is seeing this so he is able to recognize it why i am using that so here we are using assignment group so i am saying assignment group should be this let's say okay we are done with the query let's save it this is all let's see if it works or not so here we have 68 records for basic item one so let me refresh this page see now how many records are there only 12 records are there right so in this way see when i scroll down we don't have a message as i have shown in the representation that when we restrict the data using query business rule when then we don't have a message but if we use acl then we have this type of message at the bottom so here out of 68 12 are displaying over here right means 56 records are not displayed so if we are restricting this to acl then we have a message that fixed that there is a constraint that you are not able to see and the records are 56 but using br we are not getting that message at the bottom right so let me give you another example also uh and i'm clicking on all see here i'm admin user but here i can see 68 because i am not basically part of this filtration right so because of that i am seeing how many records total records but it is applicable for a user as far as the filtration or as far as the syntax which we have written he can see only 12 records now the another thing which we have to execute is or we have to implement is that logging user can only see the incident which is assigned to him for example so here basically term is into assigned to let's see how many records are there for him show matching so here we have four records of basically item so we have to write a code for that that he can only see the incident record which is assigned to him okay so let's move to here and here we have to add some query over here i'm saying current dot at query where i am saying assign to so assign 2 is basically at this field so the name of the assigned to a is assigned to so here i can use assign to so here's a login user right so i can write that user id here okay so this basically give me a sys id of a particular user whose over is logged in the society will populate over here and this query will get performed okay so i believe now you have understanding right whatever query we are writing over here accordingly to that data is getting displayed to a user okay let's save it now again go to later here click on all see how many records he is seeing now he's seeing only four records earlier he was seeing 12 records so let's log in with some other user let's say log out just for example he is basically item one right now the another user was as i displayed earlier latim chew is also a part of that group right so here i am saying login see now this time basically atom2 is going to see the incident so he can see the records which is only assigned to him let's click on r see he is seeing only two records that is later two and atom one was seeing four records but admin is seeing let's say all 68 records so in this way you can restrict the access so let's say for example if i see remove if then what happened let's say i am saying that whosoever is accessing the incident record can only see then student record assigned to this group okay let's see what happened then now save it means this is also applicable for administrator so let's see and or we can say this will be applicable for all the users because we are not giving any condition over here now we are saying that if anybody is in accessing the incident table and it is a query business rule then this is a query which will run the assignment group should be this only those incident record will be displayed to him so let's go here again click on all or refresh the page see how many records he is seeing 12. early admin was seeing 68 now he's seeing 12 records let's say flashlight number one now let's see how many requests he can see see how he is also seeing 12 records so the query which we have given here is basically that whosoever is we don't care the only thing that he can see only these records so in this way before query business rule works so you can apply any of the condition any of the query which you want here as per the requirement and you can see the results right the example which i have given is very basic just for understanding so that you can implement the use case which was assigned to you or which is assigned to you to implement in your current organization or to practice i will also share all this code and the slide content in my site i will share that link in the description box i believe whatever things i told you i have given the understanding of all those concepts right guys if you feel that this video is helpful for you then please like share and subscribe my youtube channel thank you friends have a nice day bye bye
https://www.youtube.com/watch?v=froCfspiOTQ