<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Service-now Guru&#187; Service-now.com Consulting Scripting Administration Development</title>
	<atom:link href="http://www.servicenowguru.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.servicenowguru.com</link>
	<description>Service-now.com Consulting Scripting Administration Development</description>
	<lastBuildDate>Fri, 03 Sep 2010 15:58:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Returning Random Records Using GlideRecord</title>
		<link>http://www.servicenowguru.com/scripting/returning-random-records-gliderecord/</link>
		<comments>http://www.servicenowguru.com/scripting/returning-random-records-gliderecord/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 12:10:18 +0000</pubDate>
		<dc:creator>Mark Stanger</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[GlideRecord]]></category>

		<guid isPermaLink="false">http://www.servicenowguru.com/?p=1983</guid>
		<description><![CDATA[I had a request from a client recently to generate a random list of records from a given table (specifically the Configuration Item table). These items would be used as a pool of records for a random audit of the records in that table. I don&#8217;t think this will be used all that often but [...]]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap-blue">I</span></p>
<p> had a request from a client recently to generate a random list of records from a given table (specifically the Configuration Item table).  These items would be used as a pool of records for a random audit of the records in that table.  I don&#8217;t think this will be used all that often but I figured I&#8217;d throw it out here so that people could use it if they needed it.  Here&#8217;s the code&#8230;<br />
<span id="more-1983"></span></p>
<div class="important-blue"><span class="important-title-blue">Return Random Records Script</span><br />
This script is set up to return an array containing the names of 5 randomly-selected records from the &#8216;cmdb_ci&#8217; table.  You can set the &#8216;tbl&#8217; and &#8216;returnNum&#8217; variables to customize the number of records and the table to query from.  You may also choose to modify the GlideRecord query to limit the scope of the query.</p>
<pre class="brush: jscript;">
var tbl = 'cmdb_ci'; //Query from this table
var returnNum = 5; //Return this number of records in the array
var answer = new Array();

//Set up the GlideRecord query
var rec = new GlideRecord(tbl);
rec.query();
if(rec.hasNext()){
   //Get the number of records queried
   var numRecs = rec.getRowCount();
   //Return 'returnNum' number of random records in an array
   for(i=0; i&lt;returnNum; i++){
      //Get a random number between 0 and 'numRecs'
      var randomNumber = Math.floor(Math.random()*numRecs);
      //Set the row number to 'randomNumber'
      rec.setLocation(randomNumber);
      //Add the random record to the array
      answer.push(rec.getDisplayValue().toString());
   }
}
gs.log(answer.join()); //Log the output
return answer; //Return the array
</pre>
</div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Returning+Random+Records+Using+GlideRecord+-+http://b2l.me/anw82m&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.servicenowguru.com/scripting/returning-random-records-gliderecord/&amp;t=Returning+Random+Records+Using+GlideRecord" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.servicenowguru.com/scripting/returning-random-records-gliderecord/&amp;title=Returning+Random+Records+Using+GlideRecord&amp;summary=I%20had%20a%20request%20from%20a%20client%20recently%20to%20generate%20a%20random%20list%20of%20records%20from%20a%20given%20table%20%28specifically%20the%20Configuration%20Item%20table%29.%20%20These%20items%20would%20be%20used%20as%20a%20pool%20of%20records%20for%20a%20random%20audit%20of%20the%20records%20in%20that%20table.%20%20I%20don%27t%20think%20this%20will%20be%20used%20all%20that%20often%20but%20I%20figured%20I&amp;source=Service-now Guru" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.servicenowguru.com/scripting/returning-random-records-gliderecord/&amp;title=Returning+Random+Records+Using+GlideRecord" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.servicenowguru.com/scripting/returning-random-records-gliderecord/&amp;title=Returning+Random+Records+Using+GlideRecord" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.servicenowguru.com/scripting/returning-random-records-gliderecord/&amp;title=Returning+Random+Records+Using+GlideRecord" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://www.servicenowguru.com/scripting/returning-random-records-gliderecord/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.servicenowguru.com/scripting/returning-random-records-gliderecord/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deactivating Users From LDAP</title>
		<link>http://www.servicenowguru.com/system-definition/imports/deactivating-users-ldap/</link>
		<comments>http://www.servicenowguru.com/system-definition/imports/deactivating-users-ldap/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 20:30:00 +0000</pubDate>
		<dc:creator>Mark Stanger</dc:creator>
				<category><![CDATA[Imports]]></category>
		<category><![CDATA[Import Sets]]></category>
		<category><![CDATA[LDAP]]></category>
		<category><![CDATA[Scheduled Import]]></category>

		<guid isPermaLink="false">http://www.servicenowguru.com/?p=1854</guid>
		<description><![CDATA[O ne of the most common LDAP integration requirements is to disable users in Service-now when they become disabled in the LDAP source. It is probably worth mentioning that you always want to deactivate user records (and most other records too) in Service-now instead of deleting them. Once a user record has been created in [...]]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap-orange">O</span></p>
<p>ne of the most common <a href="http://wiki.service-now.com/index.php?title=LDAP_Integration_Configuration" target="_blank">LDAP integration</a> requirements is to disable users in Service-now when they become disabled in the LDAP source.<br />
<span class="notice">It is probably worth mentioning that you always want to deactivate user records (and most other records too) in Service-now instead of deleting them.  Once a user record has been created in Service-now it should always remain in Service-now because that record could be linked to hundreds of other records (tasks, CIs, etc.).  Deleting the record kills the relationship to those other records.  Deactivating the record keeps that relationship in place.</span><br />
Because the exact steps to set up this behavior vary depending on your LDAP setup and processes, this configuration isn&#8217;t something that can be predefined in Service-now.  Typically a Service-now consultant assists with this setup and specific requirements are determined on a client-by-client basis.  It has been my experience that there are two common approaches that can be used to disable Service-now users from LDAP.  This article explains these approaches and how you can implement the needed functionality.<br />
<span id="more-1854"></span><br />
The basic idea to keep in mind when you set up either of these configurations is that the system needs to see some consistent data indicator per user object in LDAP that the user has been disabled.  Generally this indicator is membership in a specific OU (which could be identified by parsing the &#8216;dn&#8217; attribute) or through the use of the &#8216;useraccountcontrol&#8217; attribute&#8230;or both.  The bottom line is that Service-now cannot disable users from an external source unless it receives consistent data about those users indicating which ones should be disabled and which ones should not.  With either of the approaches below this data MUST come into Service-now by way of an import set table where it can be evaluated and transformed.  If these terms seem foreign to you you&#8217;ll want to familiarize yourself with <a href="http://wiki.service-now.com/index.php?title=Import_Sets" target="_blank">Service-now Import Sets</a> before you continue.</p>
<p>Disabling users in Service-now from an LDAP source typically involves one of these two methods&#8230;</p>
<p><span class="number-orange">1</span><strong>Separate import process from LDAP just for disabled users:</strong>  In this approach you don&#8217;t attempt to handle disabled users through the standard LDAP import at all.  Instead, you (or your LDAP Admin) produce a separate extract from your LDAP source that contains the users that need to be disabled.  This extract can then be used in a <strong>separate import</strong> to simply set the &#8216;active&#8217; flag on every record in the import to &#8216;false&#8217;.  The entire script (&#8216;target.active=false&#8217;) can be placed in the &#8216;Script&#8217; field directly on the &#8216;Table Transform Map&#8217; record.</p>
<p>The benefits to this solution are that the scripting is extremely simple and you don&#8217;t have to worry about identifying which users are active or inactive in Service-now.  You simply deactivate everything in the feed!  An additional benefit is that you don&#8217;t have to bring a bunch of users into a temporary import table just to see if they should be deactivated or not.  Depending on the number of users records that have to be evaluated in your transform (<a href="http://www.servicenowguru.com/service-now-general-knowledge/ldap-integration-attributes-tip/">and the number of attributes being brought in per record</a>), this may be the only way to disable users without impacting the performance of the LDAP load.  The obvious drawback to this method is that you have to have a separate process to create and drop the extract of disabled users in a location where your Service-now data source can pick it up.</p>
<p><span class="number-orange">2</span><strong>Open up the LDAP OU filter to bring everything in to your import set table (including disabled users) and then ignore inserts of disabled users based on certain script conditions:</strong>  The sample &#8216;Users&#8217; OU definition that Service-now provides in its out-of-box LDAP sample contains a filter that looks like this&#8230;</p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/09/ldapOUDefaultFilter.jpg"><img class="aligncenter size-medium wp-image-1976" title="ldapOUDefaultFilter" src="http://www.servicenowguru.com/wp-content/uploads/2010/09/ldapOUDefaultFilter-300x120.jpg" alt="" width="300" height="120" /></a></p>
<p>This filter is important because it defines what user records will be brought into the Service-now import set table to be evaluated.  The red-highlighted portion of the filter in the image above represents the common problem faced when you try to disable users from an LDAP import.  What it basically says is that all disabled users in your LDAP source should be filtered out.  This is great but you can&#8217;t very well disable a user record when you cannot see because it is disabled!  The solution in this case is to remove that portion of the filter so that you can see all of those user records.  &#8220;<em>Seeing</em>&#8221; those user records means that they are all brought into your temporary import set table so that they can be evaluated by an import set transform and disabled if necessary.</p>
<p>Once those records hit your import set table your transform takes over and can manipulate the target records in the Service-now user table as required.  The exact script that you write depends on the LDAP attributes that you bring into Service-now that indicate a disabled user.  Here are a couple of common examples that I&#8217;ve used before to filter based on OU (from the &#8216;dn&#8217; attribute which comes into your import table as &#8216;u_dn&#8217;) and the &#8216;useraccountcontrol&#8217; attribute (which is usually 514 or 546 for a disabled user).  For more information on the &#8216;userAccountControl&#8217; attribute <a href="http://support.microsoft.com/kb/305144">see this page</a>.</p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/09/ldapTranformDisableScript.jpg"><img class="aligncenter size-medium wp-image-1977" title="ldapTranformDisableScript" src="http://www.servicenowguru.com/wp-content/uploads/2010/09/ldapTranformDisableScript-300x202.jpg" alt="" width="300" height="202" /></a></p>
<pre class="brush: jscript;">
//Deactivate LDAP-disabled users during transform based on 'userAccountControl' attribute
if(source.u_useraccountcontrol == '514' || source.u_useraccountcontrol == '546'){
   target.active=false;
   target.locked_out=true;
}
</pre>
<pre class="brush: jscript;">
//Deactivate LDAP-disabled users during transform based on OU membership in 'dn'
if(source.u_dn.indexOf('OU=Disabled Accounts') &gt; -1){
   target.active = false;
   target.locked_out = true;
}
</pre>
<p>The last piece to this solution is <strong>VERY IMPORTANT</strong>.  Because you&#8217;ve opened your OU definition filter so that you can see enabled and disabled accounts from your LDAP source, Service-now will evaluate all of those records.  That part is desirable.  What isn&#8217;t desirable is the insertion of 50 thousand inactive LDAP accounts from 5 years ago that you don&#8217;t want to be created in Service-now.  You should NEVER create a user record in Service-now for a user that is inactive.  The only thing you want to do is disable any existing Service-now users as they are disabled in your LDAP source.  In order to prevent this from happening you need to set up a script that runs before the transform of each record and identifies if a record is disabled AND is being inserted.  If an insert of a disabled user is happening then the operation should be ignored by the transform.  Here&#8217;s what that &#8216;onBefore&#8217; transform map script looks like&#8230;</p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/09/ldapIgnoreDisabledInsert.jpg"><img class="aligncenter size-medium wp-image-1978" title="ldapIgnoreDisabledInsert" src="http://www.servicenowguru.com/wp-content/uploads/2010/09/ldapIgnoreDisabledInsert-300x128.jpg" alt="" width="300" height="128" /></a></p>
<p>Please note that the exact content of this script depends on the way disabled users are defined in your LDAP source.  You may need to change this script to meet the needs of your environment.</p>
<pre class="brush: jscript;">
//Ignore any insert of a disabled record as defined by the 'userAccountControl' attribute
var uc = source.u_useraccountcontrol;
if((uc == '514' || uc == '546') &amp;&amp; action == 'insert'){
   ignore = true;
}
</pre>
<p>The script examples above are given to show the different parts of solution #2.  If you put all of it together you can produce a very elegant solution that only requires a single script.  Here&#8217;s a great script that I got from Valor Poland that puts all of this together in a single script&#8230;and makes it so that you don&#8217;t have to rely on hard-coded &#8216;userAccountControl&#8217; values.  It also allows you the option of reactivating LDAP user accounts as well.  The script can be placed in the &#8216;Script&#8217; field of the &#8216;Table Transform Map&#8217; record or in an &#8216;onBefore&#8217; transform map script.  Very, very cool.<br />
<span class="attention">Note that this script makes use of the &#8216;userAccountControl&#8217; attribute.  If you are using another method to indicate disabled users in your LDAP source then this script wouldn&#8217;t apply.</span></p>
<pre class="brush: jscript;">
//Deactivate LDAP-disabled users during transform based on 'userAccountControl' attribute
//Convert the userAccountControl attribute back to a hex value
var ctrl = parseInt(source.u_useraccountcontrol, 10);
ctrl = ctrl.toString(16);

//The only digit we care about is the final one
//A final hex digit value of '2' in 'ctrl' means disabled
if(ctrl.substr(-1) == &quot;2&quot;){
   //Deactivate and lock the user account
   target.active = false;
   target.locked_out = true;
   //Ignore any insert of a disabled record
   if(action == 'insert'){
      ignore = true;
   }
}
else {
   //Optional:  Reactivate and unlock the user account
   //target.active = true;
   //target.locked_out = ctrl.substr(-2, 1) == &quot;1&quot;;
}
</pre>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Deactivating+Users+From+LDAP+-+http://b2l.me/anryw6&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.servicenowguru.com/system-definition/imports/deactivating-users-ldap/&amp;t=Deactivating+Users+From+LDAP" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.servicenowguru.com/system-definition/imports/deactivating-users-ldap/&amp;title=Deactivating+Users+From+LDAP&amp;summary=O%0D%0A%0D%0Ane%20of%20the%20most%20common%20LDAP%20integration%20requirements%20is%20to%20disable%20users%20in%20Service-now%20when%20they%20become%20disabled%20in%20the%20LDAP%20source.%0D%0AIt%20is%20probably%20worth%20mentioning%20that%20you%20always%20want%20to%20deactivate%20user%20records%20%28and%20most%20other%20records%20too%29%20in%20Service-now%20instead%20of%20deleting%20them.%20%20Once%20a%20use&amp;source=Service-now Guru" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.servicenowguru.com/system-definition/imports/deactivating-users-ldap/&amp;title=Deactivating+Users+From+LDAP" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.servicenowguru.com/system-definition/imports/deactivating-users-ldap/&amp;title=Deactivating+Users+From+LDAP" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.servicenowguru.com/system-definition/imports/deactivating-users-ldap/&amp;title=Deactivating+Users+From+LDAP" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://www.servicenowguru.com/system-definition/imports/deactivating-users-ldap/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.servicenowguru.com/system-definition/imports/deactivating-users-ldap/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Client &amp; Server Code in One UI Action</title>
		<link>http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/</link>
		<comments>http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 11:45:36 +0000</pubDate>
		<dc:creator>Mark Stanger</dc:creator>
				<category><![CDATA[UI actions]]></category>
		<category><![CDATA[Business rules]]></category>
		<category><![CDATA[Client scripts]]></category>

		<guid isPermaLink="false">http://www.servicenowguru.com/?p=1962</guid>
		<description><![CDATA[M ost Service-now administrators and consultants know how to configure and use UI Actions. UI Actions are UI elements that can show up on a form or a list as a button, link, or context menu. When these UI elements are clicked they execute some JavaScript. Most of the time UI Actions are used to [...]]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap-blue">M</span></p>
<p>ost Service-now administrators and consultants know how to configure and use UI Actions.  UI Actions are UI elements that can show up on a form or a list as a button, link, or context menu.  When these UI elements are clicked they execute some JavaScript.  Most of the time UI Actions are used to perform some server-side update to a record or records.  In other cases, you can use the &#8216;Client&#8217; checkbox on the UI Action record to execute some client-side JavaScript (including checking for mandatory fields).<br />
But what if you need to do both?  The classic case is when you want to click a button to make an update to a record, but only if the user has provided the correct input first.  An example would be a &#8216;Reopen Incident&#8217; button that changes the state on an incident record from &#8216;Resolved&#8217; to &#8216;Active&#8217;.  Usually you want to require the user to provide some sort of comment or additional information explaining why they are reopening the ticket.  The problem is that you don&#8217;t always want the &#8216;Comments&#8217; field to be mandatory so the validation needs to happen at the time the &#8216;Reopen Incident&#8217; button gets clicked.  Validation of mandatory fields needs to happen client-side but the update to your record needs to happen server-side.  How can you accomplish both of these things with a single UI Action?  This article shows you how.</p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/08/reopenIncident.jpg"><img src="http://www.servicenowguru.com/wp-content/uploads/2010/08/reopenIncident-300x140.jpg" alt="" title="reopenIncident" width="300" height="140" class="aligncenter size-medium wp-image-1949" /></a><br />
<span id="more-1962"></span><br />
The basic format for using a Client Script and Business Rule in the same UI Action looks something like this&#8230;</p>
<div class="important-blue"><span class="important-title-blue">UI Action Template</span><br />
Name:  -Button Name-<br />
Action name:  -button_action_name- (Should be unique per button on form and gets called from the UI Action script)<br />
Client:  True (MUST be checked)<br />
Form button/Form Context Menu/Form Link:  (UI Action must be one of these &#8216;Form&#8217; types)<br />
Onclick:  -runClientCode();- (Points to the function in your script that should be run when the UI Action gets clicked)<br />
Script:</p>
<pre class="brush: jscript;">
//Client-side 'onclick' function
function runClientCode(){
   if(&lt;CONDITION_TO_VALIDATE == false&gt;){
      return false;  //Abort submission
   }
   //Call the UI Action and skip the 'onclick' function
   gsftSubmit(null, g_form.getFormElement(), '&lt;button_action_name&gt;'); //MUST call the 'Action name' set in this UI Action
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if(typeof window == 'undefined')
   runBusRuleCode();

//Server-side function
function runBusRuleCode(){
   current.&lt;field_name&gt; = &lt;value&gt;;
   current.update();
   gs.addInfoMessage('You did it!');
   action.setRedirectURL(current);
}
</pre>
</div>
<p>So why does this work?  I had to go to a Service-now developer to find out.  The reason is that UI Actions can run scripts at two different times.  The first time is when the UI Action gets clicked.  When you define a &#8216;Client&#8217; UI Action you also give that UI Action the name of a function in your &#8216;Script&#8217; field to execute.  This function has to be called explicitly (through the &#8216;onclick&#8217; event) or it doesn&#8217;t run at all.<br />
The second time is on the way to the server.  This is how any UI Action without the &#8216;Client&#8217; checkbox selected gets run.  On the way to the server the entire UI Action script gets executed regardless of whether or not the &#8216;Client&#8217; checkbox is checked.  What this means is that any script you include in your UI Action that isn&#8217;t enclosed in a function will be run on the way to the server.  The script above takes advantage of this fact by making a specific call to the &#8216;Client&#8217; function, performing client-side validation, and then the UI Action calls itself if the client-side validation passes.<br />
When the UI Action calls itself it bypasses the &#8216;onclick&#8217; function because the button didn&#8217;t get clicked the second time.  So the script continues to the first point where there is something to execute.  At that point you can call your Server-side function!  The only thing you need to be careful of is that you only call the Server-side function if the script isn&#8217;t running in the client anymore.  That&#8217;s what the check in the middle does&#8230;and eliminates any browser errors saying that &#8216;current&#8217; (or any other Server-side function or object) isn&#8217;t defined.</p>
<p>Here is a solution I&#8217;ve used in the past to give users the ability to reopen an incident record.  The solution uses a UI Action button to check if the &#8216;Comments&#8217; field has been filled in (this is the &#8216;Client-side&#8217; portion).  If the validation passes, then the incident record gets updated.</p>
<div class="important-blue"><span class="important-title-blue">Reopen Incident UI Action</span><br />
<span class="attention">Note that this script uses the &#8216;State&#8217; field rather than the &#8216;Incident State&#8217; field.  In my opinion, it is much better to consolidate all of your state fields into one using the &#8216;State&#8217; field at the task level <a href="http://www.servicenowguru.com/downloads/?did=1">as described here</a>.</span><br />
Name:  Reopen Incident<br />
Action name:  reopen_incident<br />
Client:  True<br />
Form button:  True<br />
Onclick:  reopen();<br />
Condition:  current.state == 6<br />
Script:</p>
<pre class="brush: jscript;">
//Client-side 'onclick' function
function reopen(){
   if(g_form.getValue('comments') == ''){
      //Remove any existing field message, set comments mandatory, and show a new field message
      g_form.hideFieldMsg('comments');
      g_form.setMandatory('comments', true);
      g_form.showFieldMsg('comments','Comments are mandatory when reopening an Incident.','error');
      return false;  //Abort submission
   }
   //Call the UI Action and skip the 'onclick' function
   gsftSubmit(null, g_form.getFormElement(), 'reopen_incident'); //MUST call the 'Action name' set in this UI Action
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if(typeof window == 'undefined')
   serverReopen();

function serverReopen(){
   //Set the 'State' to 'Active', update and reload the record
   current.state = 2;
   current.update();
   gs.addInfoMessage('Incident ' + current.number + ' reopened.');
   action.setRedirectURL(current);
}
</pre>
</div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Client+%26+Server+Code+in+One+UI+Action+-+http://b2l.me/ands7n&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/&amp;t=Client+%26+Server+Code+in+One+UI+Action" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/&amp;title=Client+%26+Server+Code+in+One+UI+Action&amp;summary=Most%20Service-now%20administrators%20and%20consultants%20know%20how%20to%20configure%20and%20use%20UI%20Actions.%20%20UI%20Actions%20are%20UI%20elements%20that%20can%20show%20up%20on%20a%20form%20or%20a%20list%20as%20a%20button%2C%20link%2C%20or%20context%20menu.%20%20When%20these%20UI%20elements%20are%20clicked%20they%20execute%20some%20JavaScript.%20%20Most%20of%20the%20time%20UI%20Actions%20are%20used%20to%20pe&amp;source=Service-now Guru" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/&amp;title=Client+%26+Server+Code+in+One+UI+Action" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/&amp;title=Client+%26+Server+Code+in+One+UI+Action" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/&amp;title=Client+%26+Server+Code+in+One+UI+Action" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Scripted Web Services</title>
		<link>http://www.servicenowguru.com/integration/scripted-web-services/</link>
		<comments>http://www.servicenowguru.com/integration/scripted-web-services/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 20:50:23 +0000</pubDate>
		<dc:creator>Jacob Andersen</dc:creator>
				<category><![CDATA[Integration]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.servicenowguru.com/?p=1930</guid>
		<description><![CDATA[O ne of Service-now.com&#8217;s principal strengths comes from its extensible framework. Built into the framework is the ability to retrieve information using one of a myriad of methods. If you want to get data out of any table, you can get it via direct web services, using basic auth data retrieval, having it pushed to [...]]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap-orange">O</span></p>
<p>ne of Service-now.com&#8217;s principal strengths comes from its extensible framework.  Built into the framework is the ability to retrieve information using one of a myriad of methods.  If you want to get data out of any table, you can get it via <a href="http://wiki.service-now.com/index.php?title=Direct_Web_Services">direct web services</a>, using <a href="http://www.servicenowguru.com/integration-tools/integration-utilities/integration-utilities-basic-auth-data-retrieval/">basic auth data retrieval</a>, having it pushed to a client, FTP server or  linux server by using the <a href="http://www.servicenowguru.com/integration/exporting-report-ftp/">scheduled data extract</a>, and more.  However, there are times when none of these solutions give you a logical way to achieve what you want in a simple manner.  I will give two examples: 1) adding an item to a cart, 2) fetching an attachment from a record.  For tasks such as these, the flexible <em>Scripted Web Services</em> is the answer.<br />
<span id="more-1930"></span><br />
Scripted web services are used in many integrations where a product wants to accomplish something that doesn&#8217;t have a simple solution already built-in.</p>
<h1>How to use Scripted Web Services</h1>
<p><span class="attention">This functionality requires the <em><strong>Web Service Provider &#8211; Scripted</strong></em> plugin.</span></p>
<p>There are four parts to scripted web services: WSDL (generated for you), script, input variables, output variables.  The WSDL is created for you and is what you&#8217;ll use on the client side to access your new web service.  The script is simple JavaScript and performs the required action.  Within the script you retrieve the input variables that you expect the client to send your web services.  Those variables are references using <em><span style="text-decoration: underline;">request.</span><span style="font-style: normal;"> notation.  Once the script nears an end, you can set the output variables &#8211; the data you want to send back to the client &#8211; by using  <span style="text-decoration: underline;">response.</span><em> </em>notation. </span></em></p>
<p>This is the most flexible way to receive SOAP data and to return whatever you wish to return.  Let me explain by solving to two problems mentioned above.</p>
<h2>Ordering a Blackberry</h2>
<pre><a href="http://www.servicenowguru.com/wp-content/uploads/2010/08/InputOutputBlackberry.jpg"><img class="alignright size-full wp-image-1931" title="InputOutputBlackberry" src="http://www.servicenowguru.com/wp-content/uploads/2010/08/InputOutputBlackberry.jpg" alt="" width="234" height="213" /></a>
var cart = new Cart();
var item = cart.addItem('e2132865c0a801650010e411699');
cart.setVariable(item, 'original', request.phone_number);

// set the requested for
var gr = new GlideRecord("sys_user");
gr.addQuery("user_name", request.requested_for);
gr.query();
if (gr.next()) {
  var cartGR = cart.getCart();
  cartGR.requested_for = gr.sys_id;
  cartGR.update();
}

var rc = cart.placeOrder();
response.request_number = rc.number;</pre>
<p>The script is a simple example of how to order a backberrry for a specific user.  Phone_number and requested_for are sent via the soap client to the script and the scripted web service returns the request_number back to the client.  Something that would be very difficult to figure out using traditional web service methodologies is quite simple to do with using this technique.</p>
<h2>Fetching an attachment from a task record</h2>
<pre>var StringUtil = Packages.com.glide.util.StringUtil;<a href="http://www.servicenowguru.com/wp-content/uploads/2010/08/inoutFetchAttachment.jpg"><img class="alignright size-full wp-image-1932" title="inoutFetchAttachment" src="http://www.servicenowguru.com/wp-content/uploads/2010/08/inoutFetchAttachment.jpg" alt="" width="235" height="225" /></a>
var  gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", request.sys_id);
gr.query();
if (gr.next()){
   var sa = new  Packages.com.glide.ui.SysAttachment();
   var binData =  sa.getBytes(gr);
   var encData =  StringUtil.base64Encode(binData);
   response.file_name = gr.file_name;
   response.table_name = gr.table_name;
   response.encodedAttachment = encData;
}
else{
   gs.log("Record not found");
}</pre>
<p>Again, this is pretty straightforward.  What would be the alternative if direct web services were used?  Well, there would need to be a call to the sys_attachment record to find out the file_name, table_name, and associated task record.  Then there would need to be several more calls to fetch every attachment chunch (attachments are stored as chunks of data internally) only to reassemble the data client-side&#8230;.not very friendly.</p>
<p>I created an update set for the fetch attachment record that you may download and apply to your instance: <a href="http://www.servicenowguru.com/downloads/?did=16">Fetch Attachment scripted web service</a>.  Remember to enable the <strong><em>Web Service Provider &#8211; Scripted</em></strong> plugin before applying the update set.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Scripted+Web+Services+-+http://b2l.me/ajswtf&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.servicenowguru.com/integration/scripted-web-services/&amp;t=Scripted+Web+Services" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.servicenowguru.com/integration/scripted-web-services/&amp;title=Scripted+Web+Services&amp;summary=O%0D%0A%0D%0Ane%20of%20Service-now.com%27s%20principal%20strengths%20comes%20from%20its%20extensible%20framework.%20%20Built%20into%20the%20framework%20is%20the%20ability%20to%20retrieve%20information%20using%20one%20of%20a%20myriad%20of%20methods.%20%20If%20you%20want%20to%20get%20data%20out%20of%20any%20table%2C%20you%20can%20get%20it%20via%20direct%20web%20services%2C%20using%20basic%20auth%20data%20retrieval%2C&amp;source=Service-now Guru" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.servicenowguru.com/integration/scripted-web-services/&amp;title=Scripted+Web+Services" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.servicenowguru.com/integration/scripted-web-services/&amp;title=Scripted+Web+Services" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.servicenowguru.com/integration/scripted-web-services/&amp;title=Scripted+Web+Services" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://www.servicenowguru.com/integration/scripted-web-services/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.servicenowguru.com/integration/scripted-web-services/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Output BSM Map Action Info</title>
		<link>http://www.servicenowguru.com/system-definition/output-bsm-map-action-info/</link>
		<comments>http://www.servicenowguru.com/system-definition/output-bsm-map-action-info/#comments</comments>
		<pubDate>Tue, 17 Aug 2010 15:43:46 +0000</pubDate>
		<dc:creator>Mark Stanger</dc:creator>
				<category><![CDATA[System Definition]]></category>
		<category><![CDATA[BSM Map]]></category>
		<category><![CDATA[Client scripts]]></category>
		<category><![CDATA[Map Actions]]></category>

		<guid isPermaLink="false">http://www.servicenowguru.com/?p=1921</guid>
		<description><![CDATA[BSM maps (Business Service Maps) are a central feature of Service-now.com that allow users to view a visual representation of the Service-now CMDB and the Business Services and CIs that those services are composed of. Service-now BSM maps also allow you to display additional information about (and take action on) the CIs represented in the [...]]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap-orange">BSM</span></p>
<p>maps (Business Service Maps) are a central feature of Service-now.com that allow users to view a visual representation of the Service-now CMDB and the Business Services and CIs that those services are composed of.  <a href="http://wiki.service-now.com/index.php?title=Business_Service_Map" target="_blank">Service-now BSM maps</a> also allow you to display additional information about (and take action on) the CIs represented in the map through the use of <a href="http://wiki.service-now.com/index.php?title=Business_Service_Map#Map_Actions" target="_blank">BSM Map Actions</a>.  I was recently working to create a BSM Map Action that I wanted to display for particular types of CIs and I couldn&#8217;t see how to set up a condition that would allow me to identify the type of CI.<br />
What I learned is that each BSM Map has certain information available through URL parameters and through a &#8216;Data&#8217; object about each node on the map.  I just had to find the right piece of information!  The solution was to use something like the following in my &#8216;Condition&#8217; field on my BSM Map Action definition.<br />
<span id="more-1921"></span></p>
<pre class="brush: jscript;">
this.getData('ci_type') == 'cmdb_ci_win_server'
</pre>
<p>This script basically says that the Map Action should not be displayed unless the CI is a Windows Server.</p>
<p>There are several different pieces of data that you can use to help you construct these conditions.  Here is a script that I wrote to output this information in an alert message when a Map Action is clicked.  The script can be placed directly in the &#8216;Script&#8217; box on a Map Action record.</p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/08/alertBSMMapData.jpg"><img class="aligncenter size-medium wp-image-1922" title="alertBSMMapData" src="http://www.servicenowguru.com/wp-content/uploads/2010/08/alertBSMMapData-300x179.jpg" alt="" width="300" height="179" /></a></p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/08/BSMMapInfo.jpg"><img class="aligncenter size-medium wp-image-1923" title="BSMMapInfo" src="http://www.servicenowguru.com/wp-content/uploads/2010/08/BSMMapInfo-300x297.jpg" alt="" width="300" height="297" /></a></p>
<div class="important-orange"><span class="important-title-orange">&#8216;Alert Map Data&#8217; BSM Map Action</span><br />
Name:  Alert Map Data<br />
Type:  Menu<br />
Script:</p>
<pre class="brush: jscript;">
//Output data about a given Map Node
var mapData = '------Map Node Data------\n';
mapData += '--Access with: this.getID()--\n'
mapData += 'ID' + ' = ' + this.getID() + '\n\n';

mapData += '--Access with: this.getData(&quot;&lt;Node Name&gt;&quot;)--\n';
for(var n in this.data){
   mapData += n + ' = ' + this.data[n] + '\n';
}

//Output data about Diagram URL Parameters
var diaData = '\n\n------Diagram Parameter Data------\n'
diaData += 'Access with: this.diagram.getParam(&quot;&lt;Parameter Name&gt;&quot;)\n\n';
for(var p in this.diagram.params){
   diaData += p + ' = ' + this.diagram.params[p] + '\n';
}
alert(mapData + diaData);
</pre>
</div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Output+BSM+Map+Action+Info+-+http://b2l.me/ahyxeu&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.servicenowguru.com/system-definition/output-bsm-map-action-info/&amp;t=Output+BSM+Map+Action+Info" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.servicenowguru.com/system-definition/output-bsm-map-action-info/&amp;title=Output+BSM+Map+Action+Info&amp;summary=BSM%0D%0A%0D%0Amaps%20%28Business%20Service%20Maps%29%20are%20a%20central%20feature%20of%20Service-now.com%20that%20allow%20users%20to%20view%20a%20visual%20representation%20of%20the%20Service-now%20CMDB%20and%20the%20Business%20Services%20and%20CIs%20that%20those%20services%20are%20composed%20of.%20%20Service-now%20BSM%20maps%20also%20allow%20you%20to%20display%20additional%20information%20about%20%28a&amp;source=Service-now Guru" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.servicenowguru.com/system-definition/output-bsm-map-action-info/&amp;title=Output+BSM+Map+Action+Info" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.servicenowguru.com/system-definition/output-bsm-map-action-info/&amp;title=Output+BSM+Map+Action+Info" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.servicenowguru.com/system-definition/output-bsm-map-action-info/&amp;title=Output+BSM+Map+Action+Info" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://www.servicenowguru.com/system-definition/output-bsm-map-action-info/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.servicenowguru.com/system-definition/output-bsm-map-action-info/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wait for Closure of all Tasks in Graphical Workflow</title>
		<link>http://www.servicenowguru.com/graphical-workflow/wait-closure-tasks-graphical-workflow/</link>
		<comments>http://www.servicenowguru.com/graphical-workflow/wait-closure-tasks-graphical-workflow/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 14:57:14 +0000</pubDate>
		<dc:creator>Mark Stanger</dc:creator>
				<category><![CDATA[Graphical workflow]]></category>
		<category><![CDATA[Change management]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Service catalog]]></category>

		<guid isPermaLink="false">http://www.servicenowguru.com/?p=1909</guid>
		<description><![CDATA[A common Graphical Workflow requirement in Service-now is to tell the workflow to wait for some trigger before continuing. The &#8216;Wait For condition&#8217; activity is available out-of-box and is very simple to configure. Usually when working with Service Requests or Change Requests I am asked how you can set up the workflow to wait for [...]]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap-orange">A</span></p>
<p> common Graphical Workflow requirement in Service-now is to tell the workflow to wait for some trigger before continuing.  The &#8216;Wait For condition&#8217; activity is available out-of-box and is very simple to configure.  Usually when working with Service Requests or Change Requests I am asked how you can set up the workflow to wait for completion of all associated tasks before closing the Request Item or Change Request ticket.  This can be easily accomplished by using a script in your &#8216;Wait For condition&#8217; activity to query for any associated tasks that are still marked as &#8216;Active&#8217;.  If the task is marked as &#8216;Active&#8217; then it hasn&#8217;t been closed yet and the workflow should wait for that closure.  Here are some sample scripts that I&#8217;ve used before to wait for task completion on both Request Items and Change Requests.</p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/08/waitForCompletionWF.jpg"><img src="http://www.servicenowguru.com/wp-content/uploads/2010/08/waitForCompletionWF-300x161.jpg" alt="" title="waitForCompletionWF" width="300" height="161" class="aligncenter size-medium wp-image-1911" /></a></p>
<p><span id="more-1909"></span><br />
The scripts below can be pasted into the &#8216;Wait For condition&#8217; activity script field as shown here&#8230;</p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/08/waitForCompletionScript.jpg"><img src="http://www.servicenowguru.com/wp-content/uploads/2010/08/waitForCompletionScript-300x197.jpg" alt="" title="waitForCompletionScript" width="300" height="197" class="aligncenter size-medium wp-image-1912" /></a></p>
<div class="important-orange"><span class="important-title-orange">Request Item &#8216;Wait For&#8217; Scripts</span><br />
<strong><em>Request Item &#8211; Wait for Closure of All Tasks:</em></strong></p>
<pre class="brush: jscript;">
//Query for all tasks to see if they are inactive
var rec = new GlideRecord('sc_task');
rec.addQuery('request_item', current.sys_id);
rec.addQuery('active', true);
rec.query();
if(rec.hasNext()){
answer = false;
}
else{
//Continue
answer = true;
}
</pre>
<p><strong><em>Request Item &#8211; Wait for Closure of Workflow-generated Tasks:</em></strong></p>
<pre class="brush: jscript;">
//Query for all tasks to see if they are inactive
var rec = new GlideRecord('sc_task');
rec.addQuery('request_item', current.sys_id);
rec.addNotNullQuery('wf_activity');
rec.addQuery('active', true);
rec.query();
if(rec.hasNext()){
answer = false;
}
else{
//Continue
answer = true;
}
</pre>
</div>
<div class="important-orange"><span class="important-title-orange">Change Request &#8216;Wait For&#8217; Scripts</span><br />
<strong><em>Change Request &#8211; Wait for Closure of All Tasks:</em></strong></p>
<pre class="brush: jscript;">
//Query for all tasks to see if they are inactive
var rec = new GlideRecord('change_task');
rec.addQuery('change_request', current.sys_id);
rec.addQuery('active', true);
rec.query();
if(rec.hasNext()){
answer = false;
}
else{
//Continue
answer = true;
}
</pre>
<p><strong><em>Change Request &#8211; Wait for Closure of Workflow-generated Tasks:</em></strong></p>
<pre class="brush: jscript;">
//Query for all tasks to see if they are inactive
var rec = new GlideRecord('change_task');
rec.addQuery('change_request', current.sys_id);
rec.addNotNullQuery('wf_activity');
rec.addQuery('active', true);
rec.query();
if(rec.hasNext()){
answer = false;
}
else{
//Continue
answer = true;
}
</pre>
</div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Wait+for+Closure+of+all+Tasks+in+Graphical+Workflow+-+http://b2l.me/ahq2rx&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.servicenowguru.com/graphical-workflow/wait-closure-tasks-graphical-workflow/&amp;t=Wait+for+Closure+of+all+Tasks+in+Graphical+Workflow" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.servicenowguru.com/graphical-workflow/wait-closure-tasks-graphical-workflow/&amp;title=Wait+for+Closure+of+all+Tasks+in+Graphical+Workflow&amp;summary=A%20common%20Graphical%20Workflow%20requirement%20in%20Service-now%20is%20to%20tell%20the%20workflow%20to%20wait%20for%20some%20trigger%20before%20continuing.%20%20The%20%27Wait%20For%20condition%27%20activity%20is%20available%20out-of-box%20and%20is%20very%20simple%20to%20configure.%20%20Usually%20when%20working%20with%20Service%20Requests%20or%20Change%20Requests%20I%20am%20asked%20how%20you%20can&amp;source=Service-now Guru" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.servicenowguru.com/graphical-workflow/wait-closure-tasks-graphical-workflow/&amp;title=Wait+for+Closure+of+all+Tasks+in+Graphical+Workflow" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.servicenowguru.com/graphical-workflow/wait-closure-tasks-graphical-workflow/&amp;title=Wait+for+Closure+of+all+Tasks+in+Graphical+Workflow" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.servicenowguru.com/graphical-workflow/wait-closure-tasks-graphical-workflow/&amp;title=Wait+for+Closure+of+all+Tasks+in+Graphical+Workflow" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://www.servicenowguru.com/graphical-workflow/wait-closure-tasks-graphical-workflow/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.servicenowguru.com/graphical-workflow/wait-closure-tasks-graphical-workflow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manipulating Outbound Email in the &#8216;sys_email&#8217; Table</title>
		<link>http://www.servicenowguru.com/system-definition/email-notifications-system-definition/manipulating-outbound-email-sysemail-table/</link>
		<comments>http://www.servicenowguru.com/system-definition/email-notifications-system-definition/manipulating-outbound-email-sysemail-table/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 17:10:47 +0000</pubDate>
		<dc:creator>Mark Stanger</dc:creator>
				<category><![CDATA[Email Notifications]]></category>
		<category><![CDATA[Email notifications]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[sys_email]]></category>

		<guid isPermaLink="false">http://www.servicenowguru.com/?p=1892</guid>
		<description><![CDATA[H ere&#8217;s a cool tip that was just sent to me by my friend Jim Coyne. We collaborated to solve a problem that he had in his environment and this post shows the result he came up with. This post shows how you can manipulate records in the email log (sys_email table) when you have [...]]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap-blue">H</span></p>
<p>ere&#8217;s a cool tip that was just sent to me by my friend Jim Coyne.  We collaborated to solve a problem that he had in his environment and this post shows the result he came up with.  This post shows how you can manipulate records in the email log (sys_email table) when you have a need to change the contents or recipients of an email record.  This isn&#8217;t something that should be relied upon heavily and I would consider it basically a last resort but it can prove very helpful in solving the right type of problem.</p>
<p>The problem in this example was that there were emails being sent from Jim&#8217;s Service-now system that contained sensitive information.  It was necessary to send this information as part of an integration with a 3rd party system but they didn&#8217;t want to retain that information in Service-now to be viewed in logs and in the activity history of task records.</p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/08/outboundEmail.jpg"><img src="http://www.servicenowguru.com/wp-content/uploads/2010/08/outboundEmail-300x194.jpg" alt="" title="outboundEmail" width="300" height="194" class="aligncenter size-medium wp-image-1894" /></a><br />
<span id="more-1892"></span><br />
The solution that we talked about was to set up a business rule on the &#8216;sys_email&#8217; table to intercept these emails immediately after they were sent and parse out the sensitive information.  This way, the email could be sent to the 3rd party system, but there would be no record of that sensitive information retained in Service-now.  Jim then went and did the hard work and here is the resultant business rule&#8230;</p>
<div class="important-blue"><span class="important-title-blue">&#8216;sys_email&#8217; Business Rule</span><br />
The script makes sure the email we are looking at the proper type of email log record that we want to erase some of the contents for.  The result is that any of these emails now show up in task records (indicating that they&#8217;ve been sent) but they contain no sensitive information.</p>
<p>Name:  Remove Sensitive Email Log Info.<br />
Table:  Email (sys_email)<br />
When:  Before<br />
Insert/Update:  True<br />
Condition:  current.type.changesTo(&#8216;sent&#8217;)<br />
Script:</p>
<pre class="brush: jscript;">
var index = current.body.indexOf('Service Request Form');
if (current.recipients == 'abc@xyz.com' &amp;&amp; index != -1 ){
   current.body = 'Log information sent to 3rd party system';
   current.body_text = 'Log information sent to 3rd party system';
}
</pre>
</div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Manipulating+Outbound+Email+in+the+%27sys_email%27+Table+-+File: /data/app/webapp/functions.php<br />Line: 66<br />Message: Duplicate entry 'agmerW' for key 'code'&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.servicenowguru.com/system-definition/email-notifications-system-definition/manipulating-outbound-email-sysemail-table/&amp;t=Manipulating+Outbound+Email+in+the+%27sys_email%27+Table" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.servicenowguru.com/system-definition/email-notifications-system-definition/manipulating-outbound-email-sysemail-table/&amp;title=Manipulating+Outbound+Email+in+the+%27sys_email%27+Table&amp;summary=Here%27s%20a%20cool%20tip%20that%20was%20just%20sent%20to%20me%20by%20my%20friend%20Jim%20Coyne.%20%20We%20collaborated%20to%20solve%20a%20problem%20that%20he%20had%20in%20his%20environment%20and%20this%20post%20shows%20the%20result%20he%20came%20up%20with.%20%20This%20post%20shows%20how%20you%20can%20manipulate%20records%20in%20the%20email%20log%20%28sys_email%20table%29%20when%20you%20have%20a%20need%20to%20change%20the%20&amp;source=Service-now Guru" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.servicenowguru.com/system-definition/email-notifications-system-definition/manipulating-outbound-email-sysemail-table/&amp;title=Manipulating+Outbound+Email+in+the+%27sys_email%27+Table" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.servicenowguru.com/system-definition/email-notifications-system-definition/manipulating-outbound-email-sysemail-table/&amp;title=Manipulating+Outbound+Email+in+the+%27sys_email%27+Table" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.servicenowguru.com/system-definition/email-notifications-system-definition/manipulating-outbound-email-sysemail-table/&amp;title=Manipulating+Outbound+Email+in+the+%27sys_email%27+Table" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://www.servicenowguru.com/system-definition/email-notifications-system-definition/manipulating-outbound-email-sysemail-table/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.servicenowguru.com/system-definition/email-notifications-system-definition/manipulating-outbound-email-sysemail-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Allow Group Managers to Manage Group Members</title>
		<link>http://www.servicenowguru.com/system-definition/group-managers-manage-group-members/</link>
		<comments>http://www.servicenowguru.com/system-definition/group-managers-manage-group-members/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 19:48:22 +0000</pubDate>
		<dc:creator>Mark Stanger</dc:creator>
				<category><![CDATA[System Definition]]></category>
		<category><![CDATA[ACLs]]></category>
		<category><![CDATA[Group membership]]></category>
		<category><![CDATA[Groups]]></category>
		<category><![CDATA[List Control]]></category>
		<category><![CDATA[System Security]]></category>

		<guid isPermaLink="false">http://www.servicenowguru.com/?p=1882</guid>
		<description><![CDATA[I often get the request to set up access for group managers to be able to manage the members of their groups in Service-now. This configuration isn&#8217;t too difficult to set up but it does involve a few different pieces. It&#8217;s also important to consider your group setup in your system before allowing access in [...]]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap-orange">I</span></p>
<p> often get the request to set up access for group managers to be able to manage the members of their groups in Service-now.  This configuration isn&#8217;t too difficult to set up but it does involve a few different pieces.  It&#8217;s also important to consider your group setup in your system before allowing access in this way.  If you are bringing in group memberships from a data source like LDAP for example, the last thing you want is to have your managers manually changing those group memberships within Service-now.  The configuration shown below could be easily customized to allow access only to non-LDAP groups if you needed to do both however.</p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/08/manageMembers.jpg"><img src="http://www.servicenowguru.com/wp-content/uploads/2010/08/manageMembers-260x300.jpg" alt="" title="manageMembers" width="260" height="300" class="aligncenter size-medium wp-image-1886" /></a><br />
<span id="more-1882"></span><br />
This solution requires you to modify the out-of-box ACLs for the &#8216;sys_user_grmember&#8217; table.  You&#8217;ll also need to modify the &#8216;Omit Edit Condition&#8217; field for the &#8216;Group Members&#8217; related list on the &#8216;Group&#8217; form.  These configurations are outlined below.</p>
<div class="important-orange"><span class="important-title-orange">Write and Delete ACLs (&#8216;sys_user_grmember&#8217;)</span><br />
The following script can be used within your Write and Delete ACLs on the &#8216;sys_user_grmember&#8217; table.  No other roles or conditions are necessary for this configuration.</p>
<pre class="brush: jscript;">
var answer = false; //Restrict access by default
if(gs.hasRole('user_admin') || current.group.manager == gs.getUserID()){
answer = true; //Allow access if user has 'user_admin' role or is group manager
}
answer;
</pre>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/08/manageMembersWriteACL.jpg"><img src="http://www.servicenowguru.com/wp-content/uploads/2010/08/manageMembersWriteACL-300x261.jpg" alt="" title="manageMembersWriteACL" width="300" height="261" class="aligncenter size-medium wp-image-1885" /></a>
</div>
<div class="important-orange"><span class="important-title-orange">Create ACL(&#8216;sys_user_grmember&#8217;)</span><br />
The create ACL works a little bit differently because we don&#8217;t have access to &#8216;current.group.manager&#8217; before the record is created.  Because of this, you need to open up create permissions to the role that your group managers will have.  Typically these managers will have the &#8216;itil&#8217; role anyway so you can just set up your &#8216;create&#8217; ACL with the &#8216;itil&#8217; role defined in the related list at the bottom of the ACL as shown here&#8230;</p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/08/manageMembersCreateACL.jpg"><img src="http://www.servicenowguru.com/wp-content/uploads/2010/08/manageMembersCreateACL-300x216.jpg" alt="" title="manageMembersCreateACL" width="300" height="216" class="aligncenter size-medium wp-image-1883" /></a>
</div>
<div class="important-orange"><span class="important-title-orange">List Control &#8216;Omit Edit Condition&#8217;</span><br />
The final piece of controlling &#8216;Create&#8217; access is to limit the visibility of the &#8216;Edit&#8217; button on the &#8216;Group Members&#8217; related list on the &#8216;Group&#8217; form.  You can manage this by right-clicking the related list header and selecting &#8216;Personalize -> List control&#8217; from the context menu.  You can place this script in the &#8216;Omit Edit Condition&#8217; field to restrict visibility of the &#8216;Edit&#8217; button on the related list to those who have the &#8216;user_admin&#8217; role or are listed as the manager of the given group.<br />
<span class="note">If the &#8216;Omit Edit Condition&#8217; field is not visible you can add it by personalizing the &#8216;List Control&#8217; form</span></p>
<pre class="brush: jscript;">
var answer = true; //Hide the 'Edit' button by default
if(gs.hasRole('user_admin') || parent.manager == gs.getUserID()){
answer = false; //Show the 'Edit' button if user has 'user_admin' role or is group manager
}
answer;
</pre>
</div>
<p>That should do it!  You may also want to create a &#8216;Groups&#8217; module that is available for the role that your group managers have.  This will allow your group managers easy access to the groups in the system (and with a filter access to the groups that they manage).</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Allow+Group+Managers+to+Manage+Group+Members+-+http://b2l.me/af6dcm&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.servicenowguru.com/system-definition/group-managers-manage-group-members/&amp;t=Allow+Group+Managers+to+Manage+Group+Members" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.servicenowguru.com/system-definition/group-managers-manage-group-members/&amp;title=Allow+Group+Managers+to+Manage+Group+Members&amp;summary=I%20often%20get%20the%20request%20to%20set%20up%20access%20for%20group%20managers%20to%20be%20able%20to%20manage%20the%20members%20of%20their%20groups%20in%20Service-now.%20%20This%20configuration%20isn%27t%20too%20difficult%20to%20set%20up%20but%20it%20does%20involve%20a%20few%20different%20pieces.%20%20It%27s%20also%20important%20to%20consider%20your%20group%20setup%20in%20your%20system%20before%20allowing%20&amp;source=Service-now Guru" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.servicenowguru.com/system-definition/group-managers-manage-group-members/&amp;title=Allow+Group+Managers+to+Manage+Group+Members" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.servicenowguru.com/system-definition/group-managers-manage-group-members/&amp;title=Allow+Group+Managers+to+Manage+Group+Members" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.servicenowguru.com/system-definition/group-managers-manage-group-members/&amp;title=Allow+Group+Managers+to+Manage+Group+Members" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://www.servicenowguru.com/system-definition/group-managers-manage-group-members/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.servicenowguru.com/system-definition/group-managers-manage-group-members/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Defined Related Lists</title>
		<link>http://www.servicenowguru.com/system-definition/relationships/defined-related-lists/</link>
		<comments>http://www.servicenowguru.com/system-definition/relationships/defined-related-lists/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 11:10:35 +0000</pubDate>
		<dc:creator>Mark Stanger</dc:creator>
				<category><![CDATA[Relationships]]></category>
		<category><![CDATA[Approvals]]></category>
		<category><![CDATA[Attachments]]></category>
		<category><![CDATA[Defined Related Lists]]></category>

		<guid isPermaLink="false">http://www.servicenowguru.com/?p=1862</guid>
		<description><![CDATA[D efined Related Lists can be a very simple and useful tool to provide users with information in a related list directly on a form (even if that information is not directly associated with the record being viewed). The Service-now wiki contains documentation on this topic so I won&#8217;t cover that here. The point of [...]]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap-blue">D</span></p>
<p>efined Related Lists can be a very simple and useful tool to provide users with information in a related list directly on a form (even if that information is not directly associated with the record being viewed).  The Service-now wiki contains <a href="http://wiki.service-now.com/index.php?title=Creating_Defined_Related_Lists" target="_blank">documentation on this topic</a> so I won&#8217;t cover that here.  The point of this article is to point out something that often gets overlooked when working with Defined Related Lists and to share a few Defined Related Lists that I&#8217;ve used in the past.<br />
<span id="more-1862"></span><br />
One of the familiar defined related lists is the &#8216;Incidents by Same Caller&#8217; list that you can display at the bottom of your incident form.  The name says it all, but it looks something like this&#8230;</p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/07/definedRelatedList1.jpg"><img class="aligncenter size-medium wp-image-1864" title="definedRelatedList1" src="http://www.servicenowguru.com/wp-content/uploads/2010/07/definedRelatedList1-300x233.jpg" alt="" width="300" height="233" /></a></p>
<p>If you navigate to &#8216;System Definition -&gt; Relationships&#8217; and open up the &#8216;Incidents by Same Caller&#8217; relationship record you&#8217;ll see that it uses the following query to produce the resultant related list&#8230;</p>
<pre class="brush: jscript;">
current.addQuery('caller_id', parent.caller_id);
</pre>
<p>But what happens when we set the Caller field with an empty value?</p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/07/definedRelatedList2.jpg"><img class="aligncenter size-medium wp-image-1865" title="definedRelatedList2" src="http://www.servicenowguru.com/wp-content/uploads/2010/07/definedRelatedList2-300x232.jpg" alt="" width="300" height="232" /></a></p>
<p>The image above shows that you&#8217;ll get exactly what you&#8217;re supposed to get, but probably not what you want.  Depending on your setup, there might not be a whole lot of value in showing a list of incidents with no caller&#8230;at least at the bottom of your incident form.  The solution to this is to filter out any records where the field you are querying is empty.  In this case, we want to filter out any records where the Caller field is empty like this&#8230;</p>
<pre class="brush: jscript;">
current.addQuery('caller_id', parent.caller_id);
current.addNotNullQuery('caller_id');
</pre>
<p>Adding the &#8216;addNotNullQuery&#8217; line gives us the result we would expect at the bottom of our incident form.</p>
<p><a href="http://www.servicenowguru.com/wp-content/uploads/2010/07/definedRelatedList3.jpg"><img class="aligncenter size-medium wp-image-1866" title="definedRelatedList3" src="http://www.servicenowguru.com/wp-content/uploads/2010/07/definedRelatedList3-300x189.jpg" alt="" width="300" height="189" /></a></p>
<p>Here are a few other examples of defined related lists I have implemented in the past.  Please share any defined related lists you have come up with by commenting on this post below!</p>
<div class="important-blue"><span class="important-title-blue">Approval &#8211; Affected CIs</span><br />
&#8211;Shows a list of Affected CIs for the task being approved at the bottom of an approval form.<br />
Name:  Affected CIs<br />
Applies to table:  Approval (sysapproval_approver)<br />
Queries from table:  CIs Affected (task_ci)<br />
Query with:</p>
<pre class="brush: jscript;">
current.addQuery('task', parent.sysapproval);
current.addNotNullQuery('task');
</pre>
</div>
<div class="important-blue"><span class="important-title-blue">All Request Attachments</span><br />
&#8211;Shows a list of attachments from the parent request, request item, and catalog task records on a catalog task form.<br />
Name:  All Request Attachments<br />
Applies to table:  Catalog task (sc_task)<br />
Queries from table:  Attachment (sys_attachment)<br />
Query with:</p>
<pre class="brush: jscript;">
var qc = current.addQuery('table_sys_id', parent.sys_id);
qc.addOrCondition('table_sys_id', parent.request_item.sys_id);
qc.addOrCondition('table_sys_id', parent.request_item.request.sys_id);
current.addNotNullQuery('table_sys_id');
</pre>
</div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Defined+Related+Lists+-+http://b2l.me/adquxe&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.servicenowguru.com/system-definition/relationships/defined-related-lists/&amp;t=Defined+Related+Lists" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.servicenowguru.com/system-definition/relationships/defined-related-lists/&amp;title=Defined+Related+Lists&amp;summary=D%0D%0A%0D%0Aefined%20Related%20Lists%20can%20be%20a%20very%20simple%20and%20useful%20tool%20to%20provide%20users%20with%20information%20in%20a%20related%20list%20directly%20on%20a%20form%20%28even%20if%20that%20information%20is%20not%20directly%20associated%20with%20the%20record%20being%20viewed%29.%20%20The%20Service-now%20wiki%20contains%20documentation%20on%20this%20topic%20so%20I%20won%27t%20cover%20that%20h&amp;source=Service-now Guru" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.servicenowguru.com/system-definition/relationships/defined-related-lists/&amp;title=Defined+Related+Lists" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.servicenowguru.com/system-definition/relationships/defined-related-lists/&amp;title=Defined+Related+Lists" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.servicenowguru.com/system-definition/relationships/defined-related-lists/&amp;title=Defined+Related+Lists" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://www.servicenowguru.com/system-definition/relationships/defined-related-lists/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.servicenowguru.com/system-definition/relationships/defined-related-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8216;Copy&#8217; UI action for Change requests (Part 2!)</title>
		<link>http://www.servicenowguru.com/system-ui/ui-actions-system-ui/copy-ui-action-change-requests-part-2/</link>
		<comments>http://www.servicenowguru.com/system-ui/ui-actions-system-ui/copy-ui-action-change-requests-part-2/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 16:42:41 +0000</pubDate>
		<dc:creator>Mark Stanger</dc:creator>
				<category><![CDATA[UI actions]]></category>
		<category><![CDATA[Change management]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://www.servicenowguru.com/?p=1838</guid>
		<description><![CDATA[A few months ago I wrote about copying change requests using a UI action. While that method works great, it does require you to specify each and every field and value that you want to populate into the new change request. If you&#8217;ve got a lot of fields to copy over then you might end [...]]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap-orange">A</span></p>
<p> few months ago I wrote about <a href="http://www.servicenowguru.com/system-ui/ui-actions-system-ui/copy-ui-action-change-requests">copying change requests using a UI action</a>.  While that method works great, it does require you to specify each and every field and value that you want to populate into the new change request.  If you&#8217;ve got a lot of fields to copy over then you might end up with a pretty big script and a lot of items to copy over.  You also need to be aware of any new fields that get added after you create the script and make sure that they get copied if necessary.</p>
<p>The following method works in much the same way, but it copies by performing an insert against the current record (rather than starting from a brand new change record and supplying each value).  Because of this, you&#8217;re concerned about overriding any of the values (such as start and end dates) that you don&#8217;t want to be copied over from the record you are copying.  This method works better if you know you want to copy over all (or the majority) of the field values from a given change.<br />
<span id="more-1838"></span></p>
<div class="important-orange"><span class="important-title-orange">Copy Change UI action</span><br />
Name:  Copy change<br />
Form button:  True<br />
Table:  Change Request<br />
Condition:  gs.hasRole(&#8220;itil&#8221;) &#038;&#038; current.isValidRecord()<br />
Script:</p>
<pre class="brush: jscript;">
copyChange();
function copyChange() {
//Get the current sys_id value for querying
var chgID = current.sys_id.toString();
//Initialize new change for insertion
var newChange = current;
newChange.requested_by_date = 'NULL';
newChange.start_date = 'NULL';
newChange.end_date = 'NULL';
newChange.calendar_duration = 'NULL';
newChange.opened_at = current.opened_at;
newChange.opened_by = current.opened_by;
newChange.sys_created_on = current.sys_created_on;
newChange.sys_created_by = current.sys_created_by;
current.insert();

//Copy attachments for this change
Packages.com.glide.ui.SysAttachment.copy('change_request', chgID, 'change_request', newChange.sys_id);

//Copy associated tasks and CIs
copyTask(chgID);
copyCI(chgID);
gs.addInfoMessage('Change ticket ' + newChange.number + ' created.')
action.setRedirectURL(newChange);
}

function copyTask(chgID) {
//Find the current change tasks and copy them
var tasks = new GlideRecord('change_task');
tasks.addQuery('change_request', chgID);
tasks.query();
while(tasks.next()){
var taskID = tasks.sys_id.toString();
var newTask = tasks;
newTask.change_request = current.sys_id;
tasks.insert();

//Copy attachments for this task
Packages.com.glide.ui.SysAttachment.copy('change_task', taskID, 'change_task', tasks.sys_id);
}
}

function copyCI(chgID) {
//Copy over the affected CI list
var cis = new GlideRecord('task_ci');
cis.addQuery('task', chgID);
cis.query();
while(cis.next()){
var newCI = cis;
newCI.task = current.sys_id;
cis.insert();
}
}
</pre>
</div>
<p>One other method for copying a change (or any other ticket) is to reference the record to copy and copy from the referenced record.  You could use the script below to copy a change that is pulled from a reference field on any form.  Just provide the name of the field to grab the change from in line 3.</p>
<div class="important-orange"><span class="important-title-orange">Copy change UI action (For copying a referenced Change Request</span></p>
<pre class="brush: jscript;">
//Get the current sys_id value for querying
//Provide the name of the reference field to copy change from
var chgID = current.your_change_reference_field.toString();
var rec = new GlideRecord('change_request');
rec.get(chgID);
copyChange();

function copyChange() {
//Initialize new change for insertion
var newChange = rec;
newChange.requested_by_date = 'NULL';
newChange.start_date = 'NULL';
newChange.end_date = 'NULL';
newChange.calendar_duration = 'NULL';
newChange.opened_at = current.opened_at;
newChange.opened_by = current.opened_by;
newChange.sys_created_on = current.sys_created_on;
newChange.sys_created_by = current.sys_created_by;
rec.insert();

//Copy attachments for this change
Packages.com.glide.ui.SysAttachment.copy('change_request', chgID, 'change_request', rec.sys_id);

//Copy associated tasks and CIs
copyTask(chgID);
copyCI(chgID);
gs.addInfoMessage('Change ticket ' + newChange.number + ' created.');
action.setRedirectURL(rec);
}

function copyTask(chgID) {
//Find the current change tasks and copy them
var tasks = new GlideRecord('change_task');
tasks.addQuery('change_request', chgID);
tasks.query();
while(tasks.next()){
var taskID = tasks.sys_id.toString();
var newTask = tasks;
newTask.change_request = rec.sys_id;
tasks.insert();

//Copy attachments for this task
Packages.com.glide.ui.SysAttachment.copy('change_task', taskID, 'change_task', tasks.sys_id);
}
}

function copyCI(chgID) {
//Copy over the affected CI list
var cis = new GlideRecord('task_ci');
cis.addQuery('task', chgID);
cis.query();

while(cis.next()){
var newCI = cis;
newCI.task =
rec.sys_id;
cis.insert();
}
}
</pre>
</div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=%27Copy%27+UI+action+for+Change+requests+%28Part+2%21%29+-+http://b2l.me/ac3eyw&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.servicenowguru.com/system-ui/ui-actions-system-ui/copy-ui-action-change-requests-part-2/&amp;t=%27Copy%27+UI+action+for+Change+requests+%28Part+2%21%29" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.servicenowguru.com/system-ui/ui-actions-system-ui/copy-ui-action-change-requests-part-2/&amp;title=%27Copy%27+UI+action+for+Change+requests+%28Part+2%21%29&amp;summary=A%20few%20months%20ago%20I%20wrote%20about%20copying%20change%20requests%20using%20a%20UI%20action.%20%20While%20that%20method%20works%20great%2C%20it%20does%20require%20you%20to%20specify%20each%20and%20every%20field%20and%20value%20that%20you%20want%20to%20populate%20into%20the%20new%20change%20request.%20%20If%20you%27ve%20got%20a%20lot%20of%20fields%20to%20copy%20over%20then%20you%20might%20end%20up%20with%20a%20pret&amp;source=Service-now Guru" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.servicenowguru.com/system-ui/ui-actions-system-ui/copy-ui-action-change-requests-part-2/&amp;title=%27Copy%27+UI+action+for+Change+requests+%28Part+2%21%29" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.servicenowguru.com/system-ui/ui-actions-system-ui/copy-ui-action-change-requests-part-2/&amp;title=%27Copy%27+UI+action+for+Change+requests+%28Part+2%21%29" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.servicenowguru.com/system-ui/ui-actions-system-ui/copy-ui-action-change-requests-part-2/&amp;title=%27Copy%27+UI+action+for+Change+requests+%28Part+2%21%29" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://www.servicenowguru.com/system-ui/ui-actions-system-ui/copy-ui-action-change-requests-part-2/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.servicenowguru.com/system-ui/ui-actions-system-ui/copy-ui-action-change-requests-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->