<?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>ServiceNow &#8211; XtremeOwnage</title>
	<atom:link href="https://xtremeownage.com/category/technology/servicenow/feed/" rel="self" type="application/rss+xml" />
	<link>https://xtremeownage.com</link>
	<description>Cars, Computers, and Code.</description>
	<lastBuildDate>Sun, 08 Jan 2023 02:09:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.3</generator>

<image>
	<url>https://xtremeownage.com/wp-content/uploads/2019/09/cropped-Turbo-512-2-100x100.png</url>
	<title>ServiceNow &#8211; XtremeOwnage</title>
	<link>https://xtremeownage.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>ServiceNow &#8211; Query Every Asset A user has ever been assigned to</title>
		<link>https://xtremeownage.com/2019/10/03/servicenow-query-every-asset-a-user-has-ever-been-assigned-to/</link>
		
		<dc:creator><![CDATA[XO]]></dc:creator>
		<pubDate>Thu, 03 Oct 2019 19:17:53 +0000</pubDate>
				<category><![CDATA[ServiceNow]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[servicenow]]></category>
		<category><![CDATA[sys_audit]]></category>
		<category><![CDATA[sys_history]]></category>
		<category><![CDATA[sys_user]]></category>
		<guid isPermaLink="false">https://xtremeownage.com/?p=462</guid>

					<description><![CDATA[A method to query every asset that has ever been assigned to a list of users.]]></description>
										<content:encoded><![CDATA[
<p>So, an interesting request came across my desk earlier today, to query every asset a user has ever been assigned.</p>



<p>Normally- I would say this should be an easy request&#8230; Query sys_audit where tablename=alm_asset, fieldname=assigned_to,  filter by the values&#8230; and build a report based on this data.</p>



<p>However- data in sys_audit, stays forever, for every single table. So, if you attempt to run that query on any service-now instance that has been up for a few years, You will notice, it crashes.</p>



<p>This is because neither fieldname, nor tablename are indexed columns (on the underlying database)</p>



<p>However- if you look at an asset, and select history, this data is being pulled from sys_audit. </p>



<p>You may ask, well- why does that work when I cannot query the table at all??</p>



<p>The answer is- sys_audit.documentkey is an indexed field. So- when the related query looks for sys_audit where document_key = sys_id of the alm_asset, it returns very quickly, because it is indexed.</p>



<p></p>



<p>Now that we have gotten that part out of the way&#8230;. What about the report?</p>



<p>Well- while I am very knowledgeable on the BACKEND of service-now, and its available APIs, I do not know enough knowledge to build the query within service-now. BUT- I did create the logic in c# using a service-now library which I have created.</p>



<p>If you are a experienced service-now individual, you may be able to build a report based on my logic.</p>



<pre class="wp-block-code"><code lang="csharp" class="language-csharp">var Users = sn.OfType&lt;sys_user>().Where(o => (Insert lamba expression here to locate the specific users)).ToList();

foreach (var rec in sn.OfType&lt;alm_asset>())
{
    var history = sn.OfType&lt;sys_audit>()
        .Where(o => o.documentkey == (object)rec.sys_id)
        .Where(o => o.fieldname == nameof(alm_asset.assigned_to) || o.fieldname == nameof(alm_asset.u_custom_assigned_to))
        //Force query to list.
        .ToList()
        //Local Expression
        .Where(o => Users.Any(usr => usr.sys_id.Equals(o.newvalue)));

    foreach (var match in history)
    {
        //Do Something with you results.
    }
}</code></pre>



<p>The above query is quite inefficient, because it pulls down ALL assigned_to history change records for every asset to evaluate locally. However- it is many times faster then attempting to query sys_audit directly.</p>



<p>sys_history only contains 28 days of data, before any mentions it.</p>



<p>Summary:</p>



<ol><li>Query all assets</li><li>For each asset, query sys_audit where documentkey = asset.sys_id</li><li>Perform additional filtering on sys_audit, for specific field names, or values.</li><li>Profit.</li></ol>



<p>Edit- as a last note, this could easily be implement in service-now javascript.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
