Catalog item - auto populate a MRVS with users assigned assets

Bidduam
Tera Guru

I'd like to have a catalog item that displays the currently assigned assets for a user in a multi row variable set.

 

I feel this is the best way to display all assets on a catalog item since different users will have differing numbers of assets assigned to them.

Anyone know how I'd go about this?

Bidduam_0-1737439716855.png

 

1 ACCEPTED SOLUTION

@Bidduam 

Here you go without Script Include

1) create catalog client script which applies on Catalog Item

2) UI Type- ALL

3) Script as this

function onLoad() {

    var gr = new GlideRecord("alm_asset");
    gr.addQuery("assigned_to", g_user.userID);
    gr.query(checkRecord);
    function checkRecord(gr) {
        var arr = [];
        while (gr.next()) {
            var obj = {};
            obj['mrvs_asset_number'] = gr.sys_id.toString();
            obj['mrvs_asset_description'] = gr.display_name.toString(); // stores the asset display name

            // if you wish to populate the model name then you will have to use split on the display name and store
            arr.push(obj);
        }
        g_form.setValue('mrvs_assigned_assets', JSON.stringify(arr));
    }
}

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

View solution in original post

11 REPLIES 11

@Bidduam 

Hope you are doing good.

Did my reply answer your question?

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

Hi @Ankur Bawiskar 

Thanks for the reply, sorry I haven't gotten back to you until now, been on leave unexpectedly.

I can see in your post you linked me to the script include, however don't totally understand how to adapt it to what I'm trying to do or how to call it in the catalog client script?

 

I have 2 variables in my MRVS:

  • mrvs_asset_number - reference field on the alm_asset table
  • mrvs_asset_description - single line text field

The asset number variable when manually selected shows the asset_tag field in the list.

The asset description I want to get is the information in the name field on the asset form, which appears to be coming from the product model table (cmdb_model)? model.name

 

Are you able to assist by showing how the script include and catalog client script should be updated please?

@Bidduam

think your earlier question was different

You asked to show logged in user's asset onload into MRVS

Now you are referring you want whenever mrvs_asset_number is selected in MRVS it should fetch product model name and populate in mrvs_asset_description mrvs?

If your question is 1st scenario then this link will help you to auto populate logged in user's asset

How to auto populate project details in a multi row variable set 

If your question is 2nd scenario then create onChange catalog client script which applies to MRVS and variable mrvs_asset_number 

Use GlideAjax if you wish to fetch Model.name and set in mrvs_asset_description 

If you want to show Display name then use getReference with callback and set in mrvs_asset_description

 

AnkurBawiskar_1-1737602142505.png

 

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

@Ankur Bawiskar yes I want to auto populate a list of the currently logged in users assets.

The reason I mentioned mrvs_asset_number is that it is the name of one of my variables in the MRVS was all. I thought that might be helpful, but it seems to have caused confusion sorry.

This is a screenshot of my variable set:

Bidduam_0-1737605628704.png

 

I have looked at the link you provided and I don't know what to edit to make it work?

 

@Bidduam 

Here you go without Script Include

1) create catalog client script which applies on Catalog Item

2) UI Type- ALL

3) Script as this

function onLoad() {

    var gr = new GlideRecord("alm_asset");
    gr.addQuery("assigned_to", g_user.userID);
    gr.query(checkRecord);
    function checkRecord(gr) {
        var arr = [];
        while (gr.next()) {
            var obj = {};
            obj['mrvs_asset_number'] = gr.sys_id.toString();
            obj['mrvs_asset_description'] = gr.display_name.toString(); // stores the asset display name

            // if you wish to populate the model name then you will have to use split on the display name and store
            arr.push(obj);
        }
        g_form.setValue('mrvs_assigned_assets', JSON.stringify(arr));
    }
}

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