<h2>Example business rule scripts</h2><br/><div style="overflow-x:auto"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta content="text/html; charset=UTF-8" /><meta name="copyright" content="(C) Copyright 2025" /><meta name="DC.rights.owner" content="(C) Copyright 2025" /><meta name="generator" content="DITA-OT" /><meta name="DC.type" content="concept" /><meta name="DC.title" content="Classic Business rules" /><meta name="abstract" content="A business rule is a server-side script that runs when a record is displayed, inserted, updated, or deleted, or when a table is queried." /><meta name="description" content="A business rule is a server-side script that runs when a record is displayed, inserted, updated, or deleted, or when a table is queried." /><meta name="DC.subject" content="business rules, business, rules, how they work, scoped applications, creating, Scripting, field values, business rules and client scripts for controlling, field value, null, Task Active State Management business rule, reference glide list, from a script, scripting" /><meta name="keywords" content="business rules, business, rules, how they work, scoped applications, creating, Scripting, field values, business rules and client scripts for controlling, field value, null, Task Active State Management business rule, reference glide list, from a script, scripting" /><meta name="DC.relation" scheme="URI" content="../../../script/server-scripting/concept/c_ServerScripting.html" /><meta name="DC.relation" scheme="URI" content="../../../build/custom-application/concept/build-applications.html" /><meta name="DC.relation" scheme="URI" content="../../../build/custom-application/concept/developing-applications.html" /><meta name="DC.relation" scheme="URI" content="../../../build/custom-application/reference/building-pro-code-applications.html" /><meta name="DC.relation" scheme="URI" content="../../../script/topic/c_Script.html" /><meta name="DC.creator" content="ServiceNow" /><meta name="DC.date.created" content="2023-08-03" /><meta name="DC.date.modified" content="2023-08-03" /><meta name="page-type" content="platform" /><meta name="DC.format" content="XHTML" /><meta name="DC.identifier" content="c_BusinessRules" /><link rel="stylesheet" type="text/css" href="../../../CSS/commonltr.css" /><title>Classic Business rules</title></head><body> <div class="nested0" id="c_BusinessRules"> <h1 class="title topictitle1" id="ariaid-title1">Classic Business rules</h1> <div class="body conbody"><p class="shortdesc"><span class="ph">A business rule is a server-side script that runs when a record is displayed, inserted, updated, or deleted, or when a table is queried.</span></p> <div class="p">Business rules are scripts that run when certain server-side conditions are met. Business rule conditions include when to run a business rule in relation to a database operation, and what record operations the business rule applies to. There are other scripting options available on the platform for client-side conditions, such as client scripts and UI actions.<div class="note"><span class="notetitle">Note:</span> Business rules are a classic automation solution that rely on scripting. Use <span class="ph">Flow Designer</span> for any new process automation to create automations that are easier to extend, reuse, understand, and upgrade. As many organizations have business rules in production, use this documentation to learn how to work with existing business rules.</div> </div> </div> <div class="related-links"> <div class="familylinks"> <div class="parentlink"><strong>Parent Topic:</strong> <a class="link" href="../../../script/server-scripting/concept/c_ServerScripting.html" title="Server scripts run on the server or database. They can change the appearance or behavior of ServiceNow or run as business rules when records and tables are accessed or modified.">Server-side scripting</a></div> </div> </div><div class="topic reference nested1" id="r_HowBusinessRulesWork"> <h2 class="title topictitle2" id="ariaid-title2">How business rules work</h2> <div class="body refbody"><p class="shortdesc">To configure business rules, you first need to determine when the business rule should run and what action it should take.</p> <div class="section"><h3 class="title sectiontitle">When business rules run</h3> <div class="p">Business rules run based on two sets of criteria.<ul class="ul" id="r_HowBusinessRulesWork__ul_q43_4h3_bp"><li class="li">When to run the business rule in relation to a database operation.</li><li class="li">What record operation the business rule applies to.</li></ul> The following options are provided to determine when the business rule should run. <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_HowBusinessRulesWork__table_w2s_1k3_bp" class="table" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 1. </span>When the business rule should run</span></caption><colgroup><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d375929e190">Option</th><th class="entry cellrowborder" style="vertical-align:top;" id="d375929e193">When the rule runs</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e190 ">Before</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e193 ">After the user submits the form but before any action is taken on the record in the database.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e190 ">After</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e193 ">After the user submits the form and after any action is taken on the record in the database.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e190 ">Async</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e193 ">After the user submits the form and after the scheduler runs the scheduled job created from the business rule. The system creates a scheduled job from the business rule after the user submits the form but before any action is taken on the record in the database. <div class="note"><span class="notetitle">Note:</span> Newly created business rules will run during upgrades.</div> <p class="p">If a record has an asynchronous business rule that makes decisions based on the data in the record, multiple updates to the record in quick succession can cause the business rule to execute out of order or incorrectly.</p> <p class="p">If multiple async business rules update the same record, the updates performed by one script could be overwritten by another script or made in an unexpected sequence because the order of execution isn't guaranteed. You can use the After option for business rules or <a class="xref" href="../administer/platform-events/concept/events.html" target="_blank" rel="noopener noreferrer">System Events</a> as an alternative in these situations.</p> </td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e190 ">Display</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e193 ">Before the form is presented to the user, just after the data is read from the database.</td></tr></tbody></table> </div> <div class="note"><span class="notetitle">Note:</span> <ul class="ul" id="r_HowBusinessRulesWork__ul_r2y_x2k_5tb"><li class="li">Asynchronous business rules do not have access to the previous version of a record. Therefore, the <span class="keyword apiname">changes()</span>, <span class="keyword apiname">changesTo()</span>, and <span class="keyword apiname">changesFrom()</span> <span class="keyword apiname">GlideElement</span> methods do not work with async rule script. However, the condition builder and condition field (advanced view) both support the <span class="keyword apiname">changes()</span>, <span class="keyword apiname">changesTo()</span>, and <span class="keyword apiname">changesFrom()</span> methods.</li><li class="li">Business rules do not honor ACLs until you want them to be honored. For more information, see <a class="xref" href="https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0656366" target="_blank" rel="noopener noreferrer">Relationship between Business Rules and Access Control Rules (ACLs)</a></li></ul> </div> </div> <div class="p">The following options are provided to determine what record operations the business rule applies to. <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_HowBusinessRulesWork__table_p2x_rk3_bp" class="table" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 2. </span>What record operation the business rule applies to</span></caption><colgroup><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d375929e301">Option</th><th class="entry cellrowborder" style="vertical-align:top;" id="d375929e304">When the rule runs</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e301 ">Insert</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e304 ">When the user creates a new record and the system inserts it into the database.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e301 ">Update</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e304 ">When the user modifies an existing record.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e301 ">Query</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e304 ">When the user sends a query for a record or list of records to the database. Typically you should use query operation for before business rules.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e301 ">Delete</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e304 ">When the user deletes a record.</td></tr></tbody></table> </div> </div> <div class="p"><div class="note"><span class="notetitle">Note:</span> Business rules only run record operations when called from the GlideRecord API. Some applications intentionally bypass business rule processing to perform record operations directly. In addition, business rules ignore API calls run with the setWorkflow() method set to false.</div> </div> <div class="p">This image shows when different types of business rules run:<div class="fig fignone" id="r_HowBusinessRulesWork__fig_gx2_vl3_bp"><span class="figcap"><span class="fig--title-label">Figure 1. </span>Business rule processing flow</span> <img class="image" id="r_HowBusinessRulesWork__image_b23_5vl_2p" src="../image/BusinessRuleProcessingFlow.png" alt="" /> </div> <div class="note"><span class="notetitle">Note:</span> Business rules apply consistently to records regardless of whether they are accessed through forms, lists, or web services. This is one major difference between business rules and client scripts, which apply only when the form is edited.</div> </div> </div> <div class="section"><h3 class="title sectiontitle">Business rule actions</h3> <div class="p">Business rules can perform a variety of actions. Common types of actions are:<ul class="ul" id="r_HowBusinessRulesWork__ul_yvs_fm3_bp"><li class="li">Changing field values on a form that the user is updating. Field values can be set to specific values available for that field, values copied from other fields, and relative values determined by the user's role.</li><li class="li">Displaying information messages to the user.</li><li class="li">Changing values of child tasks based on changes to parent tasks.</li><li class="li">Preventing users from accessing or modifying certain fields on a form.</li><li class="li">Aborting the current database transaction. For example, if certain conditions are met, prevent the user from saving the record in the database.</li></ul> Administrators can set field values, create information messages, and abort transactions without writing a script.</div> </div> <div class="section" id="r_HowBusinessRulesWork__section_gk5_ykj_ccb"><h3 class="title sectiontitle">Prevent recursive business rules</h3> <p class="p">Avoid using <span class="keyword apiname">current.update()</span> in a business rule script. The<span class="keyword apiname"> update()</span> method triggers business rules to run on the same table for insert and update operations, leading to a business rule calling itself over and over. Changes made in before business rules are automatically saved when all before business rules are complete, and after business rules are best used for updating related, not current, objects. When a recursive business rule is detected, the system stops it and logs the error in the system log. However, <span class="keyword apiname">current.update()</span> causes system performance issues and is never necessary.</p> <p class="p">You can prevent recursive business rules by using the <span class="keyword apiname">setWorkflow()</span> method with the false parameter. The combination of the <span class="keyword apiname">update()</span> and <span class="keyword apiname">setWorkflow()</span> methods is only recommended in special circumstances where the normal before and after guidelines mentioned above do not meet your requirements.</p> </div> </div> <div class="related-links"> <div class="linklist relinfo relconcepts"><strong>Related concepts</strong><br /> <ul class="linklist"><li class="linklist"><a class="link" href="../../../administer/task-table/concept/c_PrecBetweenAssignmentAndBusRules.html" title="Scripts, assignment rules, business rules, workflows, escalations, and engines all take effect in relation to a database operation, such as insert or update. In many cases, the order of these events is important.">Precedence between data lookup, assignment, and business rules</a></li></ul></div> <div class="linklist relinfo reltasks"><strong>Related tasks</strong><br /> <ul class="linklist"><li class="linklist"><a class="link" href="../../../script/business-rules/concept/c_BusinessRules.html#t_CreatingABusinessRule" title="You can create any type of business rule to run when a record is displayed, inserted, updated, or deleted, or when a table is queried.">Create a business rule</a></li></ul></div> </div> </div> <div class="topic concept nested1" id="c_BusinessRulesInScopedApps"> <h2 class="title topictitle2" id="ariaid-title3">Business rules in scoped applications</h2> <div class="body conbody"><p class="shortdesc">Every business rule is assigned to either a private application scope or to the global scope.</p> <div class="p">The types of business rules you can create and how you can access those rules varies depending on the scope of the business rule and the scope of the table it runs on.<div class="note"><span class="notetitle">Note:</span> The term global can refer to two different aspects of a business rule: the table it runs on and the scope it runs in. Business rules can either run on specific tables or be global. In addition, they can be in the global scope or in a private application scope.</div> </div> <div class="section"><h3 class="title sectiontitle">Business rules on specific tables</h3> <p class="p">Most business rules run on a specific table, which is defined in the <span class="ph uicontrol">Table</span> field. You can create business rules on tables in the same scope and on tables that allow configuration records from another application scope.</p> <p class="p">For tables that are in a different scope than the business rule record, the types of rules are limited.</p> <div class="p"><ul class="ul" id="c_BusinessRulesInScopedApps__ul_aht_rh1_rr"><li class="li">You can create a rule where <span class="ph uicontrol">When is async</span> with any of the following options:<ul class="ul"><li class="li"><span class="ph uicontrol">Insert</span>, <span class="ph uicontrol">Update</span>, and <span class="ph uicontrol">Delete</span> database operations. You cannot select <span class="ph uicontrol">Query</span>.</li><li class="li"><span class="ph uicontrol">Set field values</span> actions and scripts (the <span class="ph uicontrol">Script</span> field).</li></ul> </li><li class="li">You can create a rule where When is before with any of the following options:<ul class="ul"><li class="li"><span class="ph uicontrol">Insert</span>, <span class="ph uicontrol">Update</span>, and <span class="ph uicontrol">Delete</span> database operations. You cannot select <span class="ph uicontrol">Query</span>.</li><li class="li"><span class="ph uicontrol">Set field values</span> actions only. You cannot write scripts and you cannot abort the database transaction.</li></ul> </li><li class="li">You cannot create any other types of business rules on tables in a different scope.</li></ul> </div> <p class="p">Business rules on specific tables cannot be accessed by other business rules or scripts.</p> </div> <div class="section"><h3 class="title sectiontitle">Global business rules</h3> <div class="p"><div class="note warning"><span class="warningtitle">Warning:</span> Consider using script includes instead of global business rules. Script includes load only on request while global business rules load on every page in the system.</div> </div> <p class="p">Global business rules are business rules where the <span class="ph uicontrol">Table</span> field is set to <span class="ph uicontrol">Global</span>. Global business rules may be accessible on multiple tables and from other scripts, depending on their scope protection. For a global business rule, define the scope protection by setting the <span class="ph uicontrol">Accessible from</span> field:</p> <div class="p"><ul class="ul" id="c_BusinessRulesInScopedApps__ul_fqs_mh1_rr"><li class="li"><span class="ph uicontrol">This application scope only</span>: prevents applications in a different scope than the business rule from calling this business rule.</li><li class="li"><span class="ph uicontrol">All application scopes</span>: allows any application to call this business rule.<div class="p"><div class="note"><span class="notetitle">Note:</span> Global business rules do not support domain separation.</div> </div> </li></ul> </div> </div> <div class="section"><h3 class="title sectiontitle">Scripts in scoped business rules</h3> <p class="p">When you write a script in a business rule, you can access:</p> <div class="p"><ul class="ul" id="c_BusinessRulesInScopedApps__ul_ebp_gh1_rr"><li class="li">Any script includes and global business rules in the same scope as the business rule.</li><li class="li">Script includes and global business rules that allow applications in a different scope to call them. To call functions from another scope, you must specify the scope of the function.</li><li class="li">For business rules in a unique scope, you can access the scoped system APIs only.</li></ul> </div> </div> </div> </div> <div class="topic task nested1" id="t_CreatingABusinessRule"> <h2 class="title topictitle2" id="ariaid-title4">Create a business rule</h2> <div class="body taskbody"><p class="shortdesc">You can create any type of business rule to run when a record is displayed, inserted, updated, or deleted, or when a table is queried.</p> <div class="section context" id="t_CreatingABusinessRule__context_nb1_njq_k5b"> <div class="note"><span class="notetitle">Note:</span> These instructions and examples provide general guidance for how to implement this functionality. For help with unique use cases, refer to the <a class="xref" href="https://community.servicenow.com/community?id=community_forum&sys_id=75291a2ddbd897c068c1fb651f9619f3" target="_blank" rel="noopener noreferrer">Developer Community Forum</a>, where you can ask questions, interact with other developers, and search for existing solutions.</div> </div> <ol class="ol steps"><li class="li step stepexpand"> <span class="ph cmd">Navigate to <span class="ph menucascade"><span class="ph uicontrol">All</span> > <span class="ph uicontrol">System Definition</span> > <span class="ph uicontrol">Business Rules</span></span>.</span> </li><li class="li step stepexpand"> <span class="ph cmd">Click <span class="ph uicontrol">New</span>.</span> </li><li class="li step stepexpand"> <span class="ph cmd">Fill in the fields, as appropriate.</span> <div class="itemgroup info"> <div class="note"><span class="notetitle">Note:</span> You might need to configure the form to see all fields.</div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="t_CreatingABusinessRule__table_bb2_yq3_bp" class="table" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 3. </span>Business Rule fields</span></caption><colgroup><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d375929e777">Field</th><th class="entry cellrowborder" style="vertical-align:top;" id="d375929e780">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Name</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Enter a name for the business rule.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Table</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Select the table that the business rule runs on.<div class="p"><div class="note"><span class="notetitle">Note:</span> The list shows only tables and database views that meet the scope protections for business rules. Business rules defined for a database view can run only on <span class="ph uicontrol">Query</span>. A business rule for a database view cannot run on insert, update, or delete.</div> </div> </td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Application</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Application that contains this business rule.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Accessible from</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Scope protection for a global business rule. <div class="p"><div class="note"><span class="notetitle">Note:</span> This field is visible only when the <span class="ph uicontrol">Table</span> field is set to <span class="ph uicontrol">Global</span>. It does not apply to rules that run on specific tables.</div> </div> </td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Active</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Select this check box to enable the business rule.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Advanced</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Select this check box to see the advanced version of the form.</td></tr><tr class="row"><td class="entry cellrowborder" colspan="2" style="vertical-align:top;" headers="d375929e777 d375929e780 ">When to run</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">When</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 "><p class="p">[Advanced] Select when this business rule should execute: <span class="ph uicontrol">display</span>, <span class="ph uicontrol">before</span>, <span class="ph uicontrol">async</span>, or <span class="ph uicontrol">after</span> the database operation is complete.</p> <div class="p"><div class="note"><span class="notetitle">Note:</span> Consider setting the <span class="ph uicontrol">Order</span> for <span class="ph uicontrol">async</span> business rules as the system uses this value when creating the associated scheduled job.</div> <div class="note"><span class="notetitle">Note:</span> Newly created <span class="ph uicontrol">async</span> business rules run automatically on upgrade.</div> <div class="note"><span class="notetitle">Note:</span> Existing <span class="ph uicontrol">async</span> business rules can be migrated to use the new async behavior.</div> </div> </td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Order</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">[Advanced] Enter a number indicating the sequence in which this business rule should run. If there are multiple rules on a particular activity, the rules run in the order specified here, from lowest to highest.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Insert</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Select this check box to execute the business rule when a record is inserted into the database.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Update</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Select this check box to execute the business rule when a record is update.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Delete</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">[Advanced] Select this check box to execute the business rule when a record is deleted from the database.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Query</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">[Advanced] Select this check box to execute the business rule when a table is queried.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Filter Conditions</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Use the condition builder to determine when the business rule should run based on the field values in the selected Table. You can also use the <span class="ph uicontrol">Condition</span> field to build a condition with a script.<div class="note"><span class="notetitle">Note:</span> Filters based on string compares are case-sensitive.</div> </td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Role Conditions</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Select the roles that users who are modifying records in the table must have for this business rule to run.</td></tr><tr class="row"><td class="entry cellrowborder" colspan="2" style="vertical-align:top;" headers="d375929e777 d375929e780 ">Actions</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Set field values</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Set values for fields in the selected Table using the choice lists:<ul class="ul" id="t_CreatingABusinessRule__ul_lff_ps3_bp"><li class="li">The field</li><li class="li">The assignment operator:<ul class="ul" id="t_CreatingABusinessRule__ul_k2h_qs3_bp"><li class="li"><span class="ph uicontrol">To:</span> An exact value</li><li class="li"><span class="ph uicontrol">Same as:</span> The value of another field</li><li class="li"><span class="ph uicontrol">To (dynamic):</span> A value relative to the user configuring the business rule or a user with a specific role</li></ul> </li><li class="li">The value</li></ul> </td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Add message</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Select this check box and enter a message that appears when this business rule is run</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Abort action</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 "><p class="p">Select this check box to abort the current database transaction. For example, on a before insert business rule, if the conditions are met, do not insert the record into the database.</p> <p class="p">If you select this option, you cannot perform additional actions on the record, such as setting field values and running scripts. You can still display a message to users by selecting the <span class="ph uicontrol">Add message</span> check box and composing the message.</p> </td></tr><tr class="row"><td class="entry cellrowborder" colspan="2" style="vertical-align:top;" headers="d375929e777 d375929e780 ">Advanced</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Condition</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Create a JavaScript conditional statement to specify when the business rule should run. By adding the condition statement to this field, you tell the system to evaluate the condition separately and run the business rule only if the condition is true. If you decide to include the condition statement in the <span class="ph uicontrol">Script</span> field or if you use the condition builder, leave this field blank. To have the instance reevaluate the condition statement a second time before running an async business rule, add the system property <span class="keyword parmname">glide.businessrule.async_condition_check</span> and set the value to true.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Script</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 "><div class="p">[Advanced] Create a script that runs when the defined condition is true.<ul class="ul" id="t_CreatingABusinessRule__ul_cc4_tyg_jt"><li class="li">onAfter</li><li class="li">onAsync</li><li class="li">onBefore</li><li class="li">onDisplay</li></ul> </div> <p class="p">For more information and examples, see <a class="xref" href="c_BusinessRules.html#business-rule-examples" title="Find an example business rule script that helps you with a requirement of your organization.">Example business rule scripts</a>.</p> </td></tr><tr class="row"><td class="entry cellrowborder" colspan="2" style="vertical-align:top;" headers="d375929e777 d375929e780 ">Related list: Versions</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e777 ">Versions</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e780 ">Shows all versions of the business rule. Use this list to compare versions or to revert to a previous version.</td></tr></tbody></table> </div> </div> </li><li class="li step stepexpand"> <span class="ph cmd">Click <span class="ph uicontrol">Submit</span>.</span> </li></ol> </div> <div class="related-links"> <div class="linklist relinfo relref"><strong>Related reference</strong><br /> <ul class="linklist"><li class="linklist"><a class="link" href="../../../script/business-rules/concept/c_BusinessRules.html#r_HowBusinessRulesWork" title="To configure business rules, you first need to determine when the business rule should run and what action it should take.">How business rules work</a></li></ul></div> </div></div> <div class="topic concept nested1" id="c_UsingPredefinedGlobalVariables"> <h2 class="title topictitle2" id="ariaid-title5">Global variables in business rules</h2> <div class="body conbody"><p class="shortdesc">Predefined global variables are available for use in business rules.</p> <div class="section" id="c_UsingPredefinedGlobalVariables__section_shs_wdw_lz"> <p class="p">Use the following predefined global variables to reference the system in a business rule script.</p> <div class="p"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="c_UsingPredefinedGlobalVariables__table_jsc_wcw_lz" class="table" frame="border" border="1" rules="all"><colgroup><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d375929e1222">Global variable</th><th class="entry cellrowborder" style="vertical-align:top;" id="d375929e1225">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e1222 "><var class="keyword varname">current</var></td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e1225 ">Current state of the record being referenced. See "Prevent null pointer exceptions" below to check for nulls before using this variable.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e1222 "><var class="keyword varname">previous</var></td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e1225 ">State of the referenced record prior to any updates made during the execution context, where the execution context begins with the first update or delete operation and ends after the script and any referenced business rules are executed. If multiple updates are made to the record within one execution context, <var class="keyword varname">previous</var> will continue to hold the state of the record before the first update or delete operation. Available on update and delete operations only. Not available on async operations. See "Prevent null pointer exceptions" below to check for nulls before using this variable.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e1222 "><var class="keyword varname">g_scratchpad</var></td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e1225 ">Scratchpad object is available on display rules, and is used to pass information to the client to be accessed from client scripts.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e1222 "><var class="keyword varname">gs</var></td><td class="entry cellrowborder" style="vertical-align:top;" headers="d375929e1225 ">References to <a class="xref" href="../app-store/dev_portal/API_reference/glideSystemScoped/concept/c_GlideSystemScopedAPI.html" target="_blank" rel="noopener noreferrer">GlideSystem</a> functions.</td></tr></tbody></table> </div> </div> <p class="p">The variables <var class="keyword varname">current</var>, <var class="keyword varname">previous</var>, and <var class="keyword varname">g_scratchpad</var> are global across all business rules that run for a transaction.</p> </div> <div class="section" id="c_UsingPredefinedGlobalVariables__null"><h3 class="title sectiontitle">Prevent null pointer exceptions</h3> <div class="p">In some cases, there may not be a <var class="keyword varname">current</var> or <var class="keyword varname">previous</var> state for the record when a business rule runs, which means that the variables will be null. To check for null before using a variable, add the following code to your business rule: <pre class="pre codeblock"><code>if (current == null) // to prevent null pointer exceptions. return; </code></pre></div> </div> <div class="section" id="c_UsingPredefinedGlobalVariables__section_c1q_xdw_lz"><h3 class="title sectiontitle">Define variables</h3> <p class="p">User-defined variables are globally scoped by default. If a new variable is declared in an order 100 business rule, the business rule that runs next at order 200 also has access to the variable. This may introduce unexpected behavior.</p> <p class="p">To prevent such unexpected behavior, always wrap your code in a function. This protects your variables from conflicting with system variables or global variables in other business rules that are not wrapped in a function. Additionally, variables such as <var class="keyword varname">current</var> must be available when a function is invoked in order to be used.</p> <div class="p">The following script is vulnerable to conflicts with other code. If the variable <var class="keyword varname">now_GR</var> is used in other rules, the value of the variable may unexpectedly change.<pre class="pre codeblock"><code>var now_GR = new GlideRecord('incident'); now_GR.query(); while(now_GR.next()) { //do something }</code></pre></div> <div class="p">When this script is wrapped in a function, the variable is available only within the function and does not conflict with other functions using a variable named <var class="keyword varname">now_GR</var>.<pre class="pre codeblock"><code>myFunction(); function myFunction() { var now_GR = new GlideRecord('incident'); now_GR.query(); while(now_GR.next()) { //do something } }</code></pre></div> </div> </div> </div> <div class="topic reference nested1" id="r_BsnsRlClntScptsCntrlFldVl"> <h2 class="title topictitle2" id="ariaid-title6">Use business rules and client scripts to control field values</h2> <div class="body refbody"><p class="shortdesc">Implement both business rules and client scripts for a field to enable users to set record values properly using both forms and lists, and to see immediate changes to the values in forms as edits are made.</p> <div class="section"> <p class="p">The problem with using only a client script or a business rule to control updates to a field is that fields can be changed on either a form or a list. Client scripts and UI policies run on forms only (client-side) and do not apply to list editing. Allowing list editing with client scripts running on fields in a form can result in incorrect data being saved to the record. For systems in which client scripts or UI policies apply to forms, either disable list editing or create appropriate business rules or access control to control the setting of values in the list editor. A side effect of this is that security measures implemented in client scripts are easy to circumvent. The user only needs to edit the field in a list.</p> <p class="p">Business rules on a form are not dynamic, the user must update the record for the change to be seen. This makes using client scripts the preferred method for controlling field values on forms.</p> <p class="p">When using both a business rule and client script to control field values, the update behavior is the same across the system. This means that updated values are not different depending on whether a list of form is used to make the change. This means that the same functionality must be implemented twice, once in a client script and once in a business rule or access control.</p> </div> <div class="section"><h3 class="title sectiontitle">Example: Use a business rule to create email addresses during user record import</h3> <p class="p">An organization has a client script that sets the email address for a user to <span class="ph filepath">first.last@company.com</span>. Administrators do this so they can see the email address immediately when they enter the user's information. The administrator then performs a bulk import of users from a spreadsheet containing the users' first and last names. The expectation is that each user's email address will be set automatically, as they are when they edit the form. Since the client script runs only on the form (the interface to the record), it has no effect on data imported into the record from outside that interface, and no email addresses are created. To solve this problem, the administrator implements a business rule that runs when the import occurs and creates the email addresses.</p> </div> <div class="section"><h3 class="title sectiontitle">Example: Prevent list edit for a field that is not editable in the form</h3> <p class="p">An organization wants to hide the <span class="ph uicontrol">Priority</span> field on an incident form if the assignment group is <span class="ph uicontrol">Development</span>. They create a UI policy on the incident form to do this, but their users can still see and edit the <span class="ph uicontrol">Priority</span> field using the list editor. To rectify this, apply an access control to prevent read access to the <span class="ph uicontrol">Priority</span> field when the assignment group is <span class="ph uicontrol">Development</span>.</p> </div> </div> <div class="topic concept nested2" id="c_UsingNULLAsAFieldValue"> <h3 class="title topictitle3" id="ariaid-title7">Using NULL as a field value</h3> <div class="body conbody"><p class="shortdesc">The string NULL has a particular role in scripts and is a reserved word.</p> <p class="p">The reserved word is NULL in all capital letters. A field with the value <span class="ph uicontrol">Null</span> or <span class="ph uicontrol">null</span>, for example, is acceptable. Only use NULL to clear out a particular field.</p> <p class="p" id="c_UsingNULLAsAFieldValue__p_npz_t3w_q1b">Any NULL field values obtained from an import set data source are inserted into the staging table as empty field values. You should not use the term NULL as a field value in import set transform maps or anywhere in the <span class="ph uicontrol">First name</span> or <span class="ph uicontrol">Last name</span> fields. Also, do not use NULL in reference fields as the system interprets the value as a string containing the word NULL, not as a reserved word.</p> </div> </div> </div> <div class="topic concept nested1" id="c_ScriptingWithDisplayBusinessRules"> <h2 class="title topictitle2" id="ariaid-title8">Display business-rules</h2> <div class="body conbody"><p class="shortdesc">Display rules are processed when a user requests a record form.</p> <p class="p">The data is read from the database, display rules are executed, and the form is presented to the user. The current object is available and represents the record retrieved from the database. Any field changes are temporary since they are not yet submitted to the database. To the client, the form values appear to be the values from the database; there is no indication that the values were modified from a display rule. This is a similar concept to calculated fields.</p> <p class="p">The primary objective of display rules is to use a shared scratchpad object, <var class="keyword varname">g_scratchpad</var>, which is also sent to the client as part of the form. This can be useful when you need to build client scripts that require server data that is not typically part of the record being displayed. In most cases, this would require a client script making a call back to the server. If the data can be determined prior to the form being displayed, it is more efficient to provide the data to the client on the initial load. The form scratchpad object is an empty object by default, and used only to store name:value pairs of data.</p> <div class="p">To populate the form scratchpad with data from a display rule:<pre class="pre codeblock"><code>// From display business rule g_scratchpad.someName = "someValue"; g_scratchpad.anotherName = "anotherValue"; // If you want the client to have access to record fields not being displayed on the form g_scratchpad.created_by = current.sys_created_by; // These are simple examples, in most cases you'll probably perform some other // queries to test or get data</code></pre></div> <div class="p">To access the form scratchpad data from a client script:<pre class="pre codeblock"><code>// From client script if(g_scratchpad.someName == "someValue") { //do something special }</code></pre></div> </div> </div> <div class="topic concept nested1" id="c_TaskActiveStateMgmtBusRule"> <h2 class="title topictitle2" id="ariaid-title9">Task Active State Management business rule</h2> <div class="body conbody"><p class="shortdesc">This business rule determines whether the active field value needs to change based on changes to the <span class="ph uicontrol">State</span> field.</p> <p class="p">The Task Active State Management business rule is executed when the <span class="ph uicontrol">State</span> is changed for a task record. Its execution order is 50, and it runs before most other task business rules.</p> <div class="p">If the current task table has the <span class="keyword parmname">close_states</span> attribute defined on its table, or if it is inherited from a higher-level table, then the rule determines whether the active field needs to change. This is done by comparing the previous and current state values. <ul class="ul" id="c_TaskActiveStateMgmtBusRule__ul_lbn_gxq_fbb"><li class="li">If the state changes from an active state to an inactive state, the <span class="ph uicontrol">Active</span> field is set to false.</li><li class="li">If the state changes from an inactive state to an active state, the <span class="ph uicontrol">Active</span> field is set to true, effectively re-activating or re-opening the task.</li></ul> </div> <p class="p">It is recommended that you leverage the <span class="keyword parmname">(current.active.changesTo([true/false])</span> action in your business rule, as opposed to creating rules on each task table that mark tasks as inactive or active.</p> </div> </div> <div class="topic concept nested1" id="business-rule-examples"> <h2 class="title topictitle2" id="ariaid-title10">Example business rule scripts</h2> <div class="body conbody"><p class="shortdesc">Find an example business rule script that helps you with a requirement of your organization.</p> <div class="note"><span class="notetitle">Note:</span> These instructions and examples provide general guidance for how to implement this functionality. For help with unique use cases, refer to the <a class="xref" href="https://community.servicenow.com/community?id=community_forum&sys_id=75291a2ddbd897c068c1fb651f9619f3" target="_blank" rel="noopener noreferrer">Developer Community Forum</a>, where you can ask questions, interact with other developers, and search for existing solutions.</div> </div> <div class="topic concept nested2" id="c_ComparingTwoDateFields"> <h3 class="title topictitle3" id="ariaid-title11">Compare date fields in a business rule</h3> <div class="body conbody"><p class="shortdesc">It is possible to compare two date fields or two date and time fields in a business rule, and abort a record insert or update if they are not correct.</p> <p class="p">For example, you may want a start date to be before an end date. The following is an example script:</p> <div class="p"><pre class="pre codeblock"><code>if ((!current.u_date1.nil()) && (!current.u_date2.nil())) { var start = current.u_date1.getGlideObject().getNumericValue(); var end = current.u_date2.getGlideObject().getNumericValue(); if (start > end) { gs.addInfoMessage('start must be before end'); current.u_date1.setError('start must be before end') ; current.setAbortAction(true); } }</code></pre></div> <p class="p">This example has been tested in global scripts, and may need changes to work in scoped scripts. In addition to possibly needing API changes, security is more strict in scoped scripts.</p> <div class="p">As a good practice, make the business rule a before rule for insert and update actions. In the example script:<ul class="ul" id="c_ComparingTwoDateFields__ul_y4y_kjx_z4"><li class="li"><var class="keyword varname">u_date1</var> and <var class="keyword varname">u_date2</var> are the names of the two date fields. Replace these names with your own field names.</li><li class="li">The first line checks that both fields actually have a value.</li><li class="li">The next two lines create variables that have the dates' numerical values.</li><li class="li">The next two lines create different alert messages for the end user: one at the top of the form and one by the <var class="keyword varname">u_date1</var> field in the form.</li><li class="li">The last line aborts the insert or update if the date fields are not correct.</li></ul> </div> <div class="p">Here is a more complex example of the above comparison. If you have more than one pair of start and end dates, you can use arrays as shown. Additionally, this script requires the input dates to be within a certain range, in this case, no fewer than 30 days in the past and no more than 365 days in the future.<pre class="pre codeblock"><code>// Enter all start and end date fields you wish to check, as well as the previous values // Make sure that you keep the placement in the sequence the same for all pairs var startDate = new Array(current.start_date,current.work_start); var prevStartDate = new Array(previous.start_date,previous.work_start); var endDate = new Array(current.end_date,current.work_end); var prevEndDate = new Array(previous.end_date,previous.work_end); // The text string below is added to the front of ' start must be before end' var userAlert = new Array('Planned','Work'); // Set the number of Previous Days you want to check var pd = 30; // Set the number of Future Days you want to check var fd = 365; // You shouldn't have to modify anything below this line var nowdt = new GlideDateTime(); nowdt.setDisplayValue(gs.nowDateTime()); var nowMs = nowdt.getNumericValue(); var pdms = nowMs; // Subtract the product of previous days to get value in milliseconds pdms -= pd * 24 * 60 * 60 * 1000; var fdms = nowMs; // Add the product of future days to get value in miliseconds fdms += fd * 24 * 60 * 60 * 1000; var badDate = false; // Iterate through all start and end date / time fields for (x = 0; x < startDate.length; x ++) { if ((!startDate[x].nil()) && (!endDate[x].nil())) { var start = startDate[x].getGlideObject().getNumericValue(); var end = endDate[x].getGlideObject().getNumericValue(); if (start > end) { gs.addInfoMessage(userAlert[x] + ' start must be before end'); startDate[x].setError(userAlert[x] + ' start must be before end'); badDate = true; } else if ((prevStartDate[x]) != (startDate[x])) { if (start < pdms) { gs.addInfoMessage(userAlert[x] + ' start must be fewer than ' + pd + ' days ago'); startDate[x].setError(userAlert[x] + ' start must be fewer than ' + pd + ' days ago'); badDate = true; } } else if ((prevEndDate[x]) != (endDate[x])) { if (end > fdms) { gs.addInfoMessage(userAlert[x] + ' end must be fewer than ' + fd + ' days ahead'); endDate[x].setError(userAlert[x] + ' end must be fewer than ' + fd + ' days ahead'); badDate = true ; } } } } if (badDate == true ) { current. setAbortAction ( true ) ; }</code></pre></div> </div> </div> <div class="topic concept nested2" id="c_ParsingXMLPayloads"> <h3 class="title topictitle3" id="ariaid-title12">Parse XML payloads</h3> <div class="body conbody"><p class="shortdesc">Fields in XML format can be parsed with the system's <span class="keyword apiname">getXMLText</span> function.</p> <div class="p">Fields that get inserted into the database in XML format, such as the payload of an <code class="ph codeph">ecc_event</code> row, can be parsed with the system's <span class="keyword apiname">getXMLText</span> function. The <span class="keyword apiname">getXMLText</span> function takes a string and an XPATH expression. For example:<pre class="pre codeblock"><code>var name = gs.getXMLText("<name>joe</name>", "//name");</code></pre></div> <p class="p">returns the string 'joe'.</p> <div class="p">Assuming that the field "payload" contains XML, the function call might look like:<pre class="pre codeblock"><code>var name = gs.getXMLText(current.payload, "//name");</code></pre></div> <p class="p">For information on XPATH, visit <a class="xref" href="http://www.w3schools.com/xml/xpath_intro.asp" target="_blank" rel="noopener noreferrer">w3schools</a>.</p> </div> </div> <div class="topic concept nested2" id="c_AbortingADbActionInABusinessRule"> <h3 class="title topictitle3" id="ariaid-title13">Abort a database action in a before business-rule</h3> <div class="body conbody"><p class="shortdesc">In a before business rule script, you can cancel or abort the current database action using the <span class="keyword apiname">setAbortAction()</span> method.</p> <p class="p">For example, if the before business rule is executed during an insert action, and you have a condition in the script that calls <code class="ph codeph">current.setAbortAction(true)</code>, the new record stored in current is not created in the database. The business rule continues to run after calling<span class="keyword apiname"> setAbortAction()</span> and all subsequent business rules will execute normally. Calling this method only prevents the database action from occurring.</p> <p class="p">You can use the <span class="keyword apiname">isActionAborted()</span> method to determine if the current database action (insert, update, delete) is going to be aborted. <span class="keyword apiname">isActionAborted()</span> is initialized for new threads and the <span class="keyword apiname">next()</span> method explicitly sets its value to false.</p> <div class="note"><span class="notetitle">Note:</span> <span class="keyword apiname">setAbortAction()</span> can only be executed from the same scope as the record whose action is being aborted. <code class="ph codeph">current.setAbortAction</code> is not honored if executed in a business rule that is defined in a different scope.</div> </div> </div> <div class="topic concept nested2" id="c_DbActionsToTriggerTheBusRule"> <h3 class="title topictitle3" id="ariaid-title14">Determine the operation that triggered the business rule</h3> <div class="body conbody"><p class="shortdesc">You can write a script for a business rule that is triggered on more than one database action.</p> <div class="p">If you want the business rule script to dynamically branch depending on the action that triggered the event, you can use the <span class="keyword apiname">operation()</span> function. For example:<pre class="pre codeblock"><code>if(current.operation() == "update") { current.updates ++; } if(current.operation() == "insert") { current.updates = 0; }</code></pre></div> </div> </div> <div class="topic concept nested2" id="c_ScriptingAnORCondition"> <h3 class="title topictitle3" id="ariaid-title15">Use an OR condition in a business rule</h3> <div class="body conbody"><p class="shortdesc">An <span class="ph uicontrol">OR</span> condition can be added to any query part within a business rule.</p> <div class="p">An <span class="ph uicontrol">OR</span> condition can be added to any query part within a business rule with the <span class="keyword apiname">addOrCondition()</span> method. The example below shows a query for finding all the incidents that have either a 1 or a 2 priority. The first <span class="keyword apiname">addQuery()</span> condition is defined as a variable and is used in the <span class="ph uicontrol">OR</span> condition.<pre class="pre codeblock"><code>var inc = new GlideRecord('incident'); var qc = inc.addQuery('priority','1'); qc.addOrCondition('priority','2'); inc.query(); while(inc.next()) { // processing for the incident goes here }</code></pre></div> <div class="p">The following script is a more complex example, using two query condition variables doing the equivalent of <code class="ph codeph">(priority = 1 OR priority = 2) AND (impact = 2 OR impact = 3)</code>. The results of the <span class="ph uicontrol">OR</span> condition are run with two variables, <var class="keyword varname">qc1</var> and <var class="keyword varname">qc2</var>. This allows you to manipulate the query condition object later in the script, such as inside an <code class="ph codeph">IF</code> condition or <code class="ph codeph">WHILE</code> loop.<pre class="pre codeblock"><code>var inc = new GlideRecord('incident'); var qc1 = inc.addQuery('priority','1'); qc1.addOrCondition('priority','2'); var qc2 = inc.addQuery('impact','2'); qc2.addOrCondition('impact','3'); inc.query(); while(inc.next()) { // processing for the incident goes here }</code></pre></div> </div> </div> <div class="topic reference nested2" id="r_ReferencingAGlideListFromAScript"> <h3 class="title topictitle3" id="ariaid-title16">Reference a Glide list from a business rule</h3> <div class="body refbody"><p class="shortdesc">A field defined as a glide list is an array of values stored in a single field.</p> <div class="section"> <p class="p">Here are some examples of how to process a glide_list field when writing business rules. Generally a glide_list field contains a list of reference values to other tables.</p> </div> <div class="section"><h4 class="title sectiontitle">Examples</h4> <p class="p">For example, the <span class="ph uicontrol">Watch list</span> field within tasks is a glide_list containing references to user records.</p> <p class="p">The code below shows how to reference the field.</p> <div class="p"><pre class="pre codeblock"><code>// list will contain a series of reference (sys_id) values separated by a comma // array will be a javascript array of reference values var list = current.watch_list.toString(); var array = list.split(","); for (var i=0; i < array.length; i++) { gs.print("Reference value is: " + array[i]); }</code></pre></div> <div class="p">Output: <pre class="pre codeblock"><code>*** Script: Reference value is: 62826bf03710200044e0bfc8bcbe5df1 *** Script: Reference value is: c2826bf03710200044e0bfc8bcbe5d45 *** Script: Reference value is: 5f74e421c0a8010e01ec0d74a7ee2cc6 *** Script: Reference value is: 06826bf03710200044e0bfc8bcbe5d57</code></pre></div> <p class="p">You can also get the display values associated with the reference values by using the <span class="keyword apiname">getDisplayValue()</span> method as shown below.</p> <div class="p"><pre class="pre codeblock"><code>// list will contain a series of display values separated by a comma // array will be a javascript array of display values var list = current.watch_list.getDisplayValue(); var array = list.split(","); for (var i=0; i < array.length; i++) { gs.print("Display value is: " + array[i]); }</code></pre></div> <div class="p">Output: <pre class="pre codeblock"><code>*** Script: Display value is: Abel Tuter *** Script: Display value is: Ashley Leonesio *** Script: Display value is: Charles Beckley *** Script: Display value is: Cherie Fuhri</code></pre></div> </div> </div> </div> <div class="topic reference nested2" id="r_UsingIndexOfSearchString"> <h3 class="title topictitle3" id="ariaid-title17">Use indexOf("searchString") to find a string in a Glide list</h3> <div class="body refbody"><p class="shortdesc">Use <span class="keyword apiname">indexOf</span>("<span class="keyword parmname">searchString</span>") to return the location of the string passed into the method if the glide list field, such as a Watch list, has at least one value in it.</p> <div class="section"> <p class="p">If the field is empty, it returns <code class="ph codeph">undefined</code>. To avoid returning an undefined value, do any of the following:</p> <ul class="ul"><li class="li">Force the field to a string, such as: <span class="keyword apiname">watch_list.toString().indexOf</span>("<span class="keyword parmname">searchString</span>")</li><li class="li">Check for an empty Glide list field with a condition before using <span class="keyword apiname">indexOf()</span>, such as: if (<span class="keyword apiname">watch_list.nil()</span> || <span class="keyword apiname">watch_list.indexOf</span>("<span class="keyword parmname">searchString</span>") == -1)</li></ul> </div> </div> </div> <div class="topic concept nested2" id="c_ExampleScriptLockingUserAccts"> <h3 class="title topictitle3" id="ariaid-title18">Lock user accounts</h3> <div class="body conbody"><p class="shortdesc">You can lock user accounts if the user is not active.</p> <div class="p">The following business rule script locks user accounts if the user is not active in the LDAP directory or the user does not have self-service, itil, or admin access to the instance.<pre class="pre codeblock"><code>// Lock accounts if bcNetIDStatus != active in LDAP and user does not // have self-service, itil or admin role var rls = current.accumulated_roles.toString(); if(current.u_bcnetidstatus == 'active' && (rls.indexOf(',itil,') > 0 || rls.indexOf(',admin,') > 0 || rls.indexOf(',ess,') > 0 )) { current.locked_out = false; } else { current.locked_out = true; } var now_GR = new GlideRecord("sys_user"); now_GR.query(); while(now_GR.next()) { now_GR.update(); gs.info("updating " + gr.getDisplayValue()); }</code></pre></div> </div> </div> <div class="topic concept nested2" id="c_ExScrDefBeforeQueryBusinessRule"> <h3 class="title topictitle3" id="ariaid-title19">Default before-query business rule</h3> <div class="body conbody"><p class="shortdesc">You can use a query business rule that executes before a database query is made.</p> <div class="p">Use this query business rule to prevent users from accessing certain records. Consider the following example from a default business rule that limits access to incident records.<ul class="ul" id="c_ExScrDefBeforeQueryBusinessRule__ul_cq3_jqx_z4"><li class="li">Name: incident query</li><li class="li">Table: Incident</li><li class="li">When: before, query</li><li class="li">Script:</li></ul> <pre class="pre codeblock"><code>if(!gs.hasRole("itil") && gs.isInteractive()) { var u = gs.getUserID(); var qc = current.addQuery("caller_id",u).addOrCondition("opened_by",u).addOrCondition("watch_list","CONTAINS",u); gs.print("query restricted to user: " + u); }</code></pre></div> <div class="p">This example prevents users from accessing incident records unless they have the itil role, or are listed in the <span class="ph uicontrol">Caller</span> or <span class="ph uicontrol">Opened by</span> field. So, for example, when self-service users open a list of incidents, they can only see the incidents they submitted.<div class="note"><span class="notetitle">Note:</span> You can also use access controls to restrict the records that users can see.</div> </div> </div> </div> </div> </div> </body></html></div>