How can I reference the manager id to return the Manager name as the Approver? GlideRecord interactions start with a database query. This is ServiceNow Flow Designer Training.ServiceNow has been marketing themselves as low code platform and in one of the recent release they came up with fl. That is why a business rule is the method of choice for our examples. Basic GlideRecord query Alter and reuse these scripts found in this post for your ServiceNow implementation. Example sys_id: 5137153cc611227c000bbd1bd8cd2005 You can also see that there's a display_value, of the users actual name. Teams. Ive found it helpful to use AddEncodedQuery to manage those types of complex queries more easily. This will return one record, because a if statement is used to cycle through the query results. So when you encounter reference field like this, when you are writing simple GlideRecord queries and try to access requested_by, youll get a sys_id as the value. gr.query(); while (gr.next()){ We have no affiliation with ServiceNow. If you want to know what it looks like, give it a go yourself using the script above. Don't hard-code those mappings, building your object 1 field at a time, just throw it through this function and let the magic do its work! (err) {outputs. Whether you're a new admin or a seasoned consultant, you're guaranteed to find quality solutions that will aid you in your ServiceNow journey! We are struggling with using AddQuery and AddORCondition to create an advanced query. Hopefully, this gives you some information and more importantly the tools and knowledge to learn the APIs and probe the depths of GlideRecord. If you ask your account manager, they'll even be able to help you build use cases for IH. Another way outside of the script debugger we were using above to examine what fields are available is getFields(). I usually use the addEncocdedQuery method when dealing with date queries. The evolution of the old workflow editor. The true value is not what you see in the field, for example a users display name on an incident. Wouldn't it be nice to have a Flow Designer action that will let you send Connect Chat messages to users within flows? gr.addQuery('state', 'IN', '1,2'); Now is a good time to talk about the performance of your GlideRecord queries. We will utilize a variety of tools to expose the details of GlideRecord under the hood. Above we have the query I have chosen to use. At first, you will see that it is undefined until we execute the incidents.query() line where it becomes an empty incident GlideRecord object and a pointer to just before the first results. gr1.query(); You can see that we just replace our addQuery () lines with one single encoded query. Speaking of having more knowledge of the object in question it is useful to know more about our GlideRecord. Could someone help me with a ui policy task, it's easy Planning to create a dashboard, should be done in which Press J to jump to the feed. This action is not possible on the client, because you cant/shouldnt access the GlideRecord class client side. Add the provided user to the conversation as a subscriber, // Conversation Sys ID passed, add message to existing conversation, // 1. It worked for me . One thing I think this lacks is the, http://wiki.servicenow.com/index.php?title=Inserting/Updating_GlideRecord_with_References. The Snowball - 2023 - An Independent ServiceNow Site, How To Use setValue() In A GlideRecord Query. There are a couple more examples that I could probably share though. Could this be added? // no Conversation Sys ID passed, start a new conversation, // 2. Dont know if its still relevant, but I had the same issue. On the other hand side best practice is using GlideAjax for Client -> Server -> Client calls. This is good if you just want to find one record, however the query would have returned seven items, which isn't completely efficent. with lots of variations on the mix of .addQuery() .addOrCondition() would not retrieve the correct data. This will work in any server side script, so a business rule or script include. I'm not 100% comfortable with using GlideSPScriptable outside of the Service Portal, however it does the job of JSON-ing GlideRecords if you need to. Get the conversation by provided Sys ID. This sys_id value, is the value of the record on the related table. Special characters like underscores (_) are removed. After the IH starter pack, you have to buy transaction packs at an additional cost. Thankfully getTable(), isValidRecord(), getEncodedQuery(), and more exist for us to interact with unknown GlideRecord objects. To listen and watch more detail about GlideRecord you can watch Community MVP Steven Bell on the ServiceNow Community YouTube. See the GlideRecord API reference for a complete list of methods. In the inputs section of the flow action, we will need a few things: The inputs screen should appear as follows after setting these up: The outputs screen should appear as follows after setting these up: Next, we will insert a Script Step into the action, and name it Send Message. Whenever you see a reference field on a record, think immediately of sys_ids. Comment out your update statement and add a log statement to check the script for accuracy before actually using it. ServiceNows table structure is a MySQL relational database. Ive tried both and have listed the results of each below. Im not that familiar with Salesforce & Rightnow so I couldnt say how Service-now compares. It appears to be down. When you are mass updating records, sometimes you don't want to run the business rules/workflow on every record you updated or have your name and the last updated time be when you updated it. see: http://community.servicenow.com/forum/5356. However, if you want to orchestrate flows outside the ServiceNow platform, it required IntegrationHub (IH). CANNOT be used in Client scripts and UI policies! Learn the 24 patterns to solve any coding interview question without getting lost in a maze of LeetCode-style practice problems. Correct - buying IH at the moment. Automate any processfrom simple productivity to complex transformationin a no-code, environment. With Service portal async GR is very wanted. Field value must be equal to the value supplied. To set a value in the field, setValue(name, value) comes into play. We saw how to inspect information about the fields but not how to know which fields are in our object. The Element API allows us to do things like getting values and not just pointers to values which can and will likely change when a .next() is executed. Sign-up to get the latest news and update information from ServiceNow Guru! In the first action, we will specify the User, Subject, and Body arguments: Then, in the second instances of the Connect Chat - Send Message action, we only need to provide the Conversation Sys ID (found in the outputs of the first call to the action), as well as the Body: Finally, we can optionally add a short wait between the 2 messages. I will publish a more detailed post just on performance soon. The generalized strategy is: - Create a GlideRecord object for the table of interest. GlideRecord Scripting The most common and fundamental scripting used in ServiceNow is GlideRecord. Here is an example of how to get all the current objects fields, print them out, and iterate over each one if we wish to get more detailed information from them. It returns a string containing the value of the field. ", // If a list of fields has not been provided, use all fields, // getElements returns a Java array. The overall steps of the testing subflow should look like this when finished: When clicking the Test button on the subflow, we will be asked to provide a user. ServiceNow Client and Server Side Programming. initialize (): creates an empty record suitable for population before an insert. Field must be less than the value supplied. Thanks very much for your suggestions I am very much intrigued with the capabilities of these constructions. Glide classes are divided into two further categories: client-side and server-side. This way, there is only one flow action to work with when building flows. But David, can't I just use JSON.stringify directly on a GlideRecord? This will print out the following sys_ids, of the users. Add the following script in the Script editor: The code above should be documented well enough to understand, but it should be called out that creating a new conversation via the sn_connect.Conversation.create function does not return a GlideRecord object or Sys ID. You can also see that theres a display_value, of the users actual name. This just showcases the benefit and ease of using the encoded query as a shortcut. Im a newbie to SN and I must admit Ive found the learning curve a bit steep for customising SN, having come from other SaaS systems as Salesforce & Rightnow. E.g. By using that method, you can simply build the query filter in a standard list so that you can see exactly what you want, then right-click the breadcrumb and select Copy query. I'd ask your account manager for the contracted number. When youre scripting with reference fields and sys_ids, understanding how to use getDisplayValue() is incredibly useful. . Flow designer is a platform capability. Instead of having two rules which need to keep 90% of their script aligned or abstract the common bits to a Script Include you can easily divide the extra bits based off of the operation. To start the new year, I want to dive into the depths of the GlideRecord object and how this information can make you a better developer. Creating Request from Okta via API does not create RITM. An easy way to identify the encoded query string to use is to create a filter or a module with the query parameters you want to use, and then hover over the link or breadcrumb and look at the URL. Luckily I like scripting and it makes it easy to script. Since Flow Designer is designed for process owners to automate a process using natural language, developers could create custom actions for process owners use them without worrying of the codes at the far side. One thing to note about updating a reference field to null is that it has to be done as described here: var gr1 = new GlideRecord(incident); This method of using the list view to perform our query does a few things for us. gr.addQuery(active, true); First and foremost is you get to inspect the results of your query in the list view and validate what your expected results are. The example shown on the right will get all records where the short_description field contains the text 'Error' anywhere in the field. - Execute the . ServiceNow Developer Blog in a script action (parm2 = sys_id of an inc and parm1 = display value of an assignment group), I have: var outage = new GlideRecord(incident); I prefer the last entry for how quickly and clearly the script is understood. var newArray = new Array(); You can build the query you want in a module or filter definition to see what the encoded query should look like. Get field values Choose the current logged in user, and then click Run: When we navigate back to the Platform UI tab, we will see 2 messages delivered via connect chat: We now have a reusable action for sending Connect Chat notifications to users via Flow Designer! The GlideRecord class is the way to interact with the ServiceNow database from a script. gr1.update(); We will start by creating a business rule which will be used to hold our script, and be invoked in order to inspect the values in the script debugger. How search works: Punctuation and capital letters are ignored. I think the current starter includes 500k transactions. Skip to page content. Sometimes, you want to get a record from ServiceNow as a simple Javascript object. Heres how to get the current date and time in ServiceNow. var qc = current.addQuery(B) It gives some information from presentations Ive given in the past about how some of this stuff connects. All Rights Reserved. There are some queries that doesnt seem to be in this post which is very nice to have. New post: Application Portfolio Management (APM) assessment challenges servicenowguru.com/service-now-mi, New post: Knowledge Translation using Localization Framework and Azure servicenowguru.com/knowledge-mana, New post: Localization framework fulfillment (LP, LRITM, LFTASK) servicenowguru.com/uncategorized/. A GlideRecord contains both records and fields. This is a pretty popular thing to do, especially for integrations where you're passing data around as JSON payloads. There are methods to move around the returned results in a non-linear manner that I might cover later. While you can dot-walk down multiple levels of referenced fields, it is my preference to work with the target referenced object directly if I need more than one field at a time. You might want to take special note of some of these, like variables that are not directly on the incident table. In this piece, we will attempt to discuss the basic steps in creating a custom action in Flow Designer. All explanations and examples are easy to follow. Youll know a field is a reference field when you see the i icon, with a circle around it, to the right of the field. I think as you become more familiar with Service-now youll see that the majority of configurations are GUI-based or require some pretty light scripting. grInc.addQuery ( . // var obj = getGrObject(grIncident, ["sys_id", "caller_id", "description"]); // "value": "1c741bd70b2322007518478d83673af3". Simply put, you use getDisplayValue(), when you have a GlideRecord object that has a reference field. This will translate sys_ids into human readable information. var gr = new GlideRecord(incident); Ill see if I can get something out next week. Important Note: Always run GlideRecord statements in a development instance first and make sure they work correctly before using in production! *Fantastic* posting, Mark! Since we have been working with a business rule, we should check out a few functions and their common uses. Copyright 2022 Kevin Custer. Heres how to get the current date and time in ServiceNow. Available as an Action Designer action step. We will be building a flow action that can both start a conversation, or add messages to an existing conversation. value is the new value that we want to set. Reddit and its partners use cookies and similar technologies to provide you with a better experience. Note: An Integration Hub subscription is required to enable integration features such as running a Script step on a MID Server. You can also view the icons within. You can also use Copy Query to help figure out your encoded query content which is helpful. See what happens when we allow incidents.next() to execute which means the debugger pointer has moved past it. These systems let you use GUIs to do most things and for more advanced customisation there is a scripting option. GlideRecord To start the new year, I want to dive into the depths of the GlideRecord object and how this information can make you a better developer. Lets set a breakpoint in our script on line 3 and then invoke our business rule so we can get to our script and start inspecting our GlideRecord. The Snowball - 2023 - An Independent ServiceNow Site, How To Use setValue() In A GlideRecord Query. Field must start with the value supplied. In the example below, it uses a Script Include and Client Script to set the Department field on a form based on the Requested For user. Not that I would do the following but just as an example. outage.setWorkflow(false); It saved me huge amount of time which I may have spent looking through the Service-Now Wiki pages. I find the encodedquery to be extremely helpful especially when my query includes things like created this week or created before a specific date. http://www.snc-blog.com/2012/10/22/temporarily-circumventing-business-rules-to-update-work-notes/, Hopefully this is helping you out? I've updated the article. We will utilize a variety of tools to expose the details of GlideRecord under the hood. To just print the current date and time in a single method, use: Alternative to the GlideDateTime() class, you can use the JavaScript Date() object, which has more methods to retrieve helpful information from. The table is it a valid object, what query was used, and more. Another nice addition to this list would be applyEncodedQuery We wont dive into GlideElement deeper here, but to point you in the right direction you can use getED() to get more information about the element in question which allows you to make abstract handlers when needed. AND IncidentState =6), (Where Priority = 2 In this example, I am taking the last 5 P1 Incidents, and looping through them, and printing the caller_id sys_id, and the display value of the caller_id record. by Kevin Custer on October 5, 2021 . I prefer to use an encoded query instead of this, but there are situations where this is easier. Flow Designer - No-Code Workflows - ServiceNow Products Flow Designer Easily create end-to-end digital workflows. The Script step is available by default to run JavaScript on a local instance. Is there a way to query for a date ? https://developer.servicenow.com/dev.do#!/reference/api/paris/server/no-namespace/GlideQueryAPI#GQ-get_S_O?navFilter=glidequery. gr1.priority = NULL; Hey Peter, this question is probably better suited for ServiceNow support or the ServiceNow community. If you want to get the display value of a field, you can add $DISPLAY to the end of a field name. The part of the URL after sysparm_query= is the encoded query for that link. Did you ever determine a way to do it. grInc.query (); while (grInc.next ()) { grInc.state = 7; // Closed grInc.update (); } OR What Is A Dictionary Override In ServiceNow? So if I had a URL that looked like this Back to the components of our GlideRecord. Thanks for this great resource} Field must be greater than the value supplied. The Snowball An Independent ServiceNow Blog & Newsletter. Thanks for the comment. Flow designer is a platform capability. The example shown on the right will get all records where the short_description field does not contain the text 'Error' anywhere in the field. 49, 2020 I wouldn't recommend using getForm if all you want is the information. You can completely ignore the GlideRecord addQuery(), I just had to use an example of something. While the content of this post isnt new information (additional examples can be found on the Service-now wiki), my aim is to provide a single page of information containing some common examples of these methods as a reference. When you run this example in a background script, you will log the actual value of the related record. Glide mainly shares Javascript APIs to deal with front-end and back-end, resulting in a highly interactive and user-friendly application. I think the current starter includes 500k transactions. If you've worked with the ServiceNow Service Portal before, you've likely seen this line of code in a number of widgets: This does something similar to my script above, and returns a plain object with the information about the fields. Pay attention to the gs.log() statement in the loop, as theres one simple difference. The return type of this function is void. In some rare cases, it may be necessary to perform a query from a client-side javascript (client script or UI policy). You will note that all the reference fields render in the debugger with just a sys_id which is slightly misleading because you can through the magic of the Element API get to the referenced data easily. newArray2.push(gr.number); Question: If I run a GlideRecord query and then want to add another condition to the query and rerun it, is that possible? Users with the action_designer role can create a custom action with one or more action steps. I've been in the ServiceNow ecosystem since 2011. If you are doing an update statement in your script, it is good to be extra careful. As you step through the lines, you can watch what the debugger has for the incidents object. This kinda GlideRecords should only be used Client side as the new Service portal doesnt allow sync calls. Perfect for integrations! The above is by no means a complete list of how you can get the managers name, department, phone, and title. Scripting around dates and time in ServiceNow has caused every ServiceNow Engineer some pain at a certain point. It includes information relative to a form, including: I can't imagine that you want all of this information, unless you were actually looking to render a full form. Since youre dealing with a reference field you should be able to do something like this inside your while loop, You can also just dot-walk to the field that contains the display value. Comment out your delete statement and add a log statement to check the script for accuracy before actually using it. 1 Answer Sorted by: 1 You could write JavaScript to find and close the Incidents, and run it from a scheduled job. // Returns an object, ready to be JSON-ified. This bulk means calling this function can take as long as it would to open the form page for this record, which is much slower than other methods. Written with by the Developer Advocate team, 2023 2022 by ServiceNow Elite. Glide class works hand in hand with ServiceNow to use databases and perform operations on the UI objects and pages. As a quick reminder, the script debugger can only be used in a synchronous script that is run in the current users session. We will then use that action in an example flow. It would be nice if their wiki included a clearer explanation on how Client Scripts, UI Policies, UI Actions, Business Rules & Access Control all fits together. Benefits. addQuery('sys_id', 'IN', '0331ddb40a0a3c0e40c83e9f7520f860,032ebb5a0a0a3c0e2e2204a495526dce'); Retrieves only records of a specified class for tables which are extended. Im having trouble with setWorkflow. Does anyone know if Flow Designer is an additional cost add-on? The easiest way to inspect the details of the fields and values that are available to the GlideRecord object and when they are available is the script debugger. Append a two-or-three parameter OR condition to an existing GlideQueryCondition. Im not sure why exactly its not working as expected in your case. You can pass this to an action and to the Script step. GlideRecord To start the new year, I want to dive into the depths of the GlideRecord object and how this information can make you a better developer. Out of box, the full name on sys_user is setup to display as the field to show when its being referred to. Flow designer is free until you look at integrations. I'm seeing in Paris they have done a lot of work on it. Is there a way to get the display value from a SYS ID returned in a query? You may also choose to modify the GlideRecord query to limit the scope of the query. (One email per month). newArray.push(gr.number); I know that the SNC documentation team is really focusing right now on scripting documentation so what you see on the wiki should be getting better by the day. When youre scripting with reference fields and sys_ids, understanding how to use getDisplayValue() is incredibly useful. getValue(String name) returns null if the field is empty or does not exist. Resulted in a script error Object doesnt support this property or method. In reference to the Or query is there any documentation about what a QueryCondition object is, and what its other functions are? I end up having to write scripts to check things that should be check-able in an IF. The get method returns the first record in the result set. Then you can just add that as an encoded query and not have to worry about the correct AddOrCondition setup. Can also be used in Client scripts and UI policies. GlideClassElement is the glide class sub-element that we need to set for GlideRecord, GlideForm, etc. Click here to download the update set containing everything we built in this article! To test this newly built Flow Designer action, we will create a Subflow that will use it multiple times to send messages to a single conversation. Copyright 2023 Educative, Inc. All rights reserved. ServiceNow Developer Blog Get Query Shortcut (used to get a single GlideRecord). Using an encoded query is often easier than multiple addQuery lines. Anytime you see a reference field on a form, you need to know that the true value of that , Want to get better at ServiceNow? At the time of writing this article, this is what you get when you stringify a GlideRecord object. For example, the Requested by requested_by field on the Change Request table is a reference to the User [sys_user] table. It's worth noting that this function returns more than just the values of a record, but all of the information to render a form. Great to have all of these listed together thanks! We have no affiliation with ServiceNow. That knowledge allows us to make a generic business rule which can handle all three operations but might have just a little extra handling for the delete case. We have an array for the managers names which will demonstrate a common error many newer devs fall into when looping over the results and trying to place the results into an array. You use the getDisplayValue() method to convert the sys_id of the reference field, to a human readable value, or the display value of the record in question. EX: When you find the Requested by field, youll see that the value is some long string, which is the sys_id of the record thats on a different table. var grInc = new GlideRecord ('incident'); // Add filtering logic here . Note: These methods are designed for use in server-side JavaScript (everything EXCEPT client scripts and UI policies). I found it on the SNBlog, the author is Stefan Bohncke. Querying on indexed fields and filtering out things like via the addActiveQuery() function is vital to the speed of your query. Ah, you are correct. We cant directly enumerate over these GlideRecord objects, but under the hood, ServiceNow is doing so. // Since the Conversation API does not provide a GlideRecord object or Sys ID, // look up the most recently created conversation by subject and return the Sys ID, Add user to a conversation, used only when creating a conversation, Set the subject of a conversation, used only when creating a conversation, The message to send to the conversation, used in both new and existing conversations, The conversation's Sys ID, used only when sending a message to an existing conversation, The conversation's Sys ID, used in output to chain together multiple messages in a single conversation, Return any errors caught during execution. Q&A for work. The fields of your object are called GlideElements. gr.addQuery('number', 'STARTSWITH', 'INC'); There are quite a few functions that are available on these GlideRecord Elements that you can utilize to interact with those objects. So I created an addEncodedQuery() and it produced the correct data on the display list but any further filtering on the list is ignored. Automate any processfrom simple productivity to complex transformationin a no-code, environment. Id prefer using an encoded query if possible. It looks pretty intimidating when bunched all together, but if you break it up at the ^ or ^OR which represents AND and OR it is much more readable. One large wasteful performance issue that comes up routinely is using a GlideRecord query to get a row count. Hopefully some of the content here helps you to get going a little bit faster. Well done. The Script: var inc = new GlideRecord ("incident"); inc.addEncodedQuery ("priority=1^ORpriority=2 . The examples are in no specific order - so just ctrl+f or cmd+f and search to find what you need! The real code Im doing has quite a few query conditions and Im rebuilding the same query multiple times just to add one more condition and it just seems inefficient. Practice your skills in a hands-on, setup-free coding environment. Use addQuery(YOURENCODEDQUERYHERE) instead. getDisplayValue() can only be used on reference fields in ServiceNow. But when you DONT use, getDisplayValue() on the reference field, youll just print out the sys_id of the related record. Furthermore, when you hover over it, youll see a modal window popup on the screen, to the data, showing more fields. Note that you can also chain your OR condition as well, which is usually simpler, An alternative to a standard query is to use an encoded query to create your query string instead of using addQuery and addOrCondition statements. addQuery('short_description', 'DOES NOT CONTAIN', 'Error'); Field must contain the value supplied anywhere in the string provided. This script is set up to return an array containing the names of 5 randomly-selected records from the 'cmdb_ci' table. This reference field relationship allows us to do things like dot-walk to different tables in ServiceNow.