How to set default value on Reference variable?

Radhika11
Tera Contributor

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.

Radhika11_0-1691777091276.png

 

2.  When I clicked on the Asset Tag, it showing all the Assets that belonging to the user.

Radhika11_1-1691777641366.png

 

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,

 

 

 

10 REPLIES 10

Bert_c1
Kilo Patron

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.

SwarnadeepNandy
Mega Sage

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 

Hello @SwarnadeepNandy 

Thank you so much for your help!

I think I totally screwed up where to enter the code.

Radhika11_0-1691814291226.png

Could you please share some screenshots, that would help me enormously?

 

Hi Radhika,

Just call the function populateAssetTag() inside onload function.

and define the function populateAssetTag() outside onload.

 

Kind Regards,

Swarnadeep Nandy