I want o display the logged-in user assets in portal

Tharun13
Tera Contributor

Hi Team,

 

I have computer table and we have maintain the all users assets. we have identified the based on "assigned to". 

I want to display the logged in user assets. 
how to display the assets in portal.

 

Could you please provide the steps.

Regards,

Tharun 

 

 

2 REPLIES 2

Ramya V
Kilo Sage

Hi @Tharun13 ,

I implemented it by creating a new widget on the asset table (alm_asset) and adding it to the portal page. If you have an asset tag and clicking on that asset tag redirects to the record of the asset. The result is shown in the below screenshot:

Screenshot 2025-02-23 at 4.00.24 PM.png

 You can implement this on your computer table.

HTML Script:

<div class="asset-widget">
<h3>Your Assigned Assets</h3>
<div ng-if="assets.length > 0" class="asset-list">
<div ng-repeat="asset in assets" class="asset-item">
<div>
<strong>Asset Tag:</strong>
<span ng-if="asset.asset_tag !== 'N/A'">
<a href="/alm_asset.do?sys_id={{ asset.sys_id }}" target="_blank">
{{ asset.asset_tag }}
</a>
</span>
<span ng-if="asset.asset_tag === 'N/A'">
{{ asset.asset_tag }}
</span>
</div>
<div>
<strong>Asset Name:</strong> {{ asset.asset_name }}
</div>
</div>
</div>
<div ng-if="assets.length === 0" class="no-assets">
<p>No assets found.</p>
</div>
</div>

 

CSS:

.asset-widget {
font-family: Arial, sans-serif;
padding: 15px;
background-color: #f9f9f9;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.asset-widget h3 {
font-size: 18px;
color: #333;
margin-bottom: 10px;
}

.asset-list {
margin-top: 10px;
}

.asset-item {
padding: 10px;
border-bottom: 1px solid #ddd;
margin-bottom: 10px;
}

.asset-item:last-child {
border-bottom: none;
}

.no-assets {
color: #888;
font-size: 14px;
}

.asset-widget a {
color: #007bff;
text-decoration: none;
}

.asset-widget a:hover {
text-decoration: underline;
}

 

Server Script:

(function() {
var gr = new GlideRecord('alm_asset');
gr.addQuery('assigned_to', gs.getUserID());
gr.query();
 
var assets = [];
while (gr.next()) {
// Retrieve asset tag; if empty, default to 'N/A'
var tag = gr.getValue('asset_tag') || 'N/A';
 
// Retrieve asset name from the model reference, if available.
var assetName = 'Unnamed Asset';
if (gr.model) {
var modelGR = gr.model.getRefRecord();
assetName = modelGR.getDisplayValue() || 'Unnamed Asset';
}
 
// Capture the sys_id so we can link to the asset record
var sys_id = gr.getUniqueValue();
 
assets.push({
'asset_tag': tag,
'asset_name': assetName,
'sys_id': sys_id
});
}
 
// Pass the array to the client
data.assets = assets;
})();
 
Client Controller script:
api.controller = function($scope) {
var c = this;
if (c.data && c.data.assets && Array.isArray(c.data.assets)) {
$scope.assets = c.data.assets;
} else {
$scope.assets = [];
}
// Debug log
console.log('Assets:', $scope.assets);
};
You can add CSS according to your requirements.
 
Please mark this answer as Helpful if this solution works for you.

Ankur Bawiskar
Tera Patron
Tera Patron

@Tharun13 

you can have your own widget and add it to portal page.

It will show assets assigned to logged in user

HTML:

<div>
  <h2>Your Assets</h2>
  <ul>
    <li ng-repeat="asset in c.data.assets">
      {{asset.name}} - {{asset.serial_number}}
    </li>
  </ul>
</div>

Client Controller:

(function() {
  var c = this;
  c.data.assets = [];

  // Fetch the logged-in user's assets
  var gr = new GlideRecord('alm_asset');
  gr.addQuery('assigned_to', gs.getUserID());
  gr.query();
  while (gr.next()) {
    c.data.assets.push({
      name: gr.getValue('name'),
      serial_number: gr.getValue('serial_number')
    });
  }
})();

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader