Auto populate the current users laptop in a catalog item

Moedeb
Tera Guru

I have a reference field on alm_asset and I just want to show in that field (on load) the current users assigned laptop.

So for us laptops all have a model category of 'Computer', so I'd tried to set the default value of the field as follows:

Variable name: vs_laptop_asset_number

Table: alm_asset

Asset model category 'Computer' sys_id: 81feb9c137101000deeabfc8bcbe5dc4

Asset assigned user field name: owned_by

 

Default value:

javascript:model_category=81feb9c137101000deeabfc8bcbe5dc4^owned_by=gs.getUserID();

 

This obviously has not worked as I'm asking all of you for some help please

1 ACCEPTED SOLUTION

@Moedeb 

 

In that case, you can may be populate the asset owned as per the asset table in one variable which could be read only and have a confirmation checkbox stating "Does the above listed asset does not belongs to you ?". If the user response is yes, you can have a variable for them to select the asset.

 

Let me know your thoughts on this and we can further work it towards closure.


Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

24 REPLIES 24

Thank you @Omkar Mone that did help with the original idea that @Juhi Poddar suggested and made that work, however I can't use that option anyway as it doesn't allow the user to change the asset if it is incorrect for any reason.

@Juhi Poddar  or @Amit Verma is there a way to make this actually populate the reference field, rather than just act as a filter?

So doing what has been suggested by you both as far as restricting / filtering the asset list to only show the current users assets is usable for me, but I would like it so the user doesn't have to 'select' it still.

 

Is there a way?

 

All the other script includes and client scripts and such just are not working for me at all.

Juhi Poddar
Kilo Patron

Hello @Moedeb 

 

To achieve your goal of setting a default value for the current user's laptop while also allowing the user to select other options from the alm_asset table, use a Client Script and script include to set the default value of the reference field to the current user's assigned laptop.

 

Create a System Property in sys_properties Table

 

  • Navigate to System Properties > All Properties in the application navigator.
  • Click New to create a new system property.
  • Fill out the fields:
  • Name: modelCategorySysId (or something meaningful).
  • Type: String.
  • Value: 81feb9c137101000deeabfc8bcbe5dc4 (the sys_id for the "Computer" model category).
  • Description: "Stores the sys_id for the 'Computer' model category."
  • Click Submit to save the property.

Create a Script Include (Server-Side)

create a Script Include to perform the server-side GlideRecord query.

var LaptopLookup = Class.create();
LaptopLookup.prototype = {
    initialize: function() {},
    // Function to get the current user's assigned laptop
    getUserLaptop: function() {
        var result = {};
        var modelCategorySysId = gs.getProperty('modelCategorySysId'); // Get system property
        var gr = new GlideRecord('alm_asset');
        gr.addQuery('model_category', modelCategorySysId);
        gr.addQuery('owned_by', gs.getUserID());
        gr.query();
        
        if (gr.next()) {
            result.sys_id = gr.sys_id.toString();
        } else {
            result.sys_id = '';
        }
        return JSON.stringify(result); // Return as a JSON string
    },
    type: 'LaptopLookup'
};

This Script Include queries the alm_asset table to find the current user’s assigned laptop. It’s set up to return the sys_id of the laptop in JSON format.

Create a Client Script (Client-Side)

In the Client Script, you use GlideAjax to call the server-side Script Include and set the reference field's value.

This will set the field to the current user's laptop when the form loads. However, the user will still be able to change the value in the field.

function onLoad() {
    // Create a GlideAjax object and call the Script Include
    var ga = new GlideAjax('LaptopLookup');
    ga.addParam('sysparm_name', 'getUserLaptop'); // Call the getUserLaptop function
    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);
        
        // Set the value of the reference field to the user's laptop if found
        if (result.sys_id) {
            g_form.setValue('vs_laptop_asset_number', result.sys_id);
        }
    });
}

The Script Include runs server-side and queries the alm_asset table for the current user's laptop. The Client Script uses GlideAjax to call the Script Include and set the value of the reference field (vs_laptop_asset_number).

GlideAjax is designed to allow client-side scripts to interact with server-side logic (e.g., GlideRecord, gs).

gs.getProperty() and GlideRecord queries are only possible on the server, so they must be executed in a Script Include or other server-side script.

This approach ensures that the data is fetched properly and securely while adhering to client/server architecture limitations in ServiceNow.

 

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thank You

Juhi Poddar

 

 

@Juhi Poddar ok, so I've tried what you've suggested:

 

Created a system property and called it:

model_category_Computer_sys_id
 
Created a script include with the following:
Name: LaptopLookup
API Name: global.LaptopLookup
Client callable - false
mobile callable - false
sandbox enabled - false
Script:

 

var LaptopLookup = Class.create();
LaptopLookup.prototype = {
    initialize: function() {},
    // Function to get the current user's assigned laptop
    getUserLaptop: function() {
        var result = {};
        var model_category_Computer_sys_id = gs.getProperty('model_category_Computer_sys_id'); // Get system property
        var gr = new GlideRecord('alm_asset');
        gr.addQuery('model_category', model_category_Computer_sys_id);
        gr.addQuery('owned_by', gs.getUserID());
        gr.query();

        if (gr.next()) {
            result.sys_id = gr.sys_id.toString();
        } else {
            result.sys_id = '';
        }
        return JSON.stringify(result); // Return as a JSON string
    },
    type: 'LaptopLookup'
};

 

 

Then created a "Client Script":

Name: Populate Laptop

Table: Asset [alm_asset]

UI Type: All

Type: onLoad

 

Script:

 

function onLoad() {
    // Create a GlideAjax object and call the Script Include
    var ga = new GlideAjax('LaptopLookup');
    ga.addParam('sysparm_name', 'getUserLaptop'); // Call the getUserLaptop function
    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);
        
        // Set the value of the reference field to the user's laptop if found
        if (result.sys_id) {
            g_form.setValue('vs_laptop_asset_number', result.sys_id);
        }
    });
}

 

 

Tried the catalog item and still nothing populated in the field.

Hello @Moedeb 

Since we are calling this script include from client script, client callable has to be marked as true. Also once you create script include without marking it as client callable and later just checkbox the client callable will not make the script include client callable. In that case you need to create a fresh script include with checkbox true to client callable.