How to set default value on Reference variable?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2023 11:28 AM
Hello everyone,
I have two variables (Requested For and Asset Tag).
Based on the user name, asset tag will automatic populate the assets that the user currently have.
1. When the form load, the Asset Tag is showing empty.
2. When I clicked on the Asset Tag, it showing all the Assets that belonging to the user.
Here is what I would like to accomplish:
When form load.....
If user has 1 asset, then default the asset tag in the Asset Tag field
else if user has more then 1 asset, then don't default to the first asset, just list them so that user have an option to select from the dropdown.
Thank you so much,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2023 01:32 PM
Hi,
You can use a Catalog Client Script, with Type set to "onLoad" I believe. See examples in your instance, I have 81 of those in my PDI. You will probably need to use GlideAjax to get values from the "Asset" table.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2023 03:17 PM
Hi Radhika,
Use a client script that runs on load and on change of the Requested For field. The script can use the g_form.getReference() method to get the user object from the Requested For field, and then use the GlideRecord API to query the Asset table for the assets that belong to that user. The script can then check the number of assets returned by the query, and set the Asset Tag field accordingly.
Here is an example script that you can use or modify for your client script:
// Define a function to populate the Asset Tag field based on the Requested For field
function populateAssetTag() {
// Get the user object from the Requested For field
g_form.getReference(‘requested_for’, function (user) {
// Create a GlideRecord object for the Asset table
var assetGr = new GlideRecord(‘alm_asset’);
// Add a query to filter by the user’s sys_id
assetGr.addQuery(‘assigned_to’, user.sys_id);
// Execute the query
assetGr.query();
// Check the number of assets returned by the query
if (assetGr.getRowCount() == 1) {
// If there is only one asset, set the Asset Tag field to that asset’s tag
assetGr.next();
g_form.setValue(‘asset_tag’, assetGr.asset_tag);
}
});
}
// Call the function on load
populateAssetTag();
// Add an onChange client script for the Requested For field to call the function on change g_form.onChange(‘requested_for’, populateAssetTag);
Kind Regards,
Swarnadeep Nandy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2023 09:27 PM
Hello @SwarnadeepNandy
Thank you so much for your help!
I think I totally screwed up where to enter the code.
Could you please share some screenshots, that would help me enormously?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2023 08:16 AM
Hi Radhika,
Just call the function populateAssetTag() inside onload function.
and define the function populateAssetTag() outside onload.
Kind Regards,
Swarnadeep Nandy