Asset Tag Dynamic Generation

Amit_H
Tera Contributor

Hi,

 

I have a requirement to generate asset tag based on location's country with below conditions.

  1. It should only generate for India.
  2. Tag should follow format like IND-CP-000001 where IND is asset's location's country, CP is model category's code and 000001 is increment number.
  3. Once code generated asset tag should become read only

Can someone help me with code.

1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @Amit_H ,

 

You can follow below steps.

  1. Create one property to hold the auto increment number.
  2. Create before BR on "alm_hardware" table as this table contains all the hardware asset. If you will do it on "alm_asset" then it will generate for consumable item as well, which supposed to not happen.
  3. Use below code to generate the tag and read and set back auto increment value.
 var country = current.location.country;
    var modelCode = current.model_category.code;

    var oldNum = gs.getProperty('tag_number');
    var newNum = parseInt(oldNum) + 1;
    var finalNum = String(newNum).padStart(6, '0');

    var assetTag = country + '-' + modelCode + '-' + finalNum;
    current.asset_tag = assetTag;

    gs.setProperty('tag_number', newNum);

RunjayPatel_0-1730870960582.png

RunjayPatel_1-1730871115553.png

 

 

4. Create one UI policy to make field read only. Add condition same as business rule except "asset tag is not empty".

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

Part 2. In this video i have talked about overview on ServiceNow platform/tool. How you can opt for personal dev instance (PDI)? how to login in ServiceNow instance and navigation to OOB modules. For document please visit: https://servicenowwithrunjay.com/ Follow Facebook page for latest update on
3 REPLIES 3

Abhay Kumar1
Giga Sage

@Amit_H You can create a BR and this will run when a new asset is inserted, generate the asset tag based on your condition, please feel free to modify below demo scripts which will work here:

(function executeRule(current, previous /*null when async*/) {

    // Check if the asset's location country is India

    var location = current.location.getRefRecord();

    if (location && location.country == 'India') {

 

        // Generate the prefix for the asset tag

        var countryCode = "IND";

        var modelCategoryCode = "CP"; // Assuming CP is the static model category code

 

        // Get the next increment number

        var gr = new GlideRecord('alm_asset');

        gr.addEncodedQuery('location.country=India^asset_tagSTARTSWITHIND-CP');

        gr.orderByDesc('asset_tag');

        gr.setLimit(1);

        gr.query();

 

        var increment = 1;

        if (gr.next()) {

            // Extract the increment part and increase it by 1

            var lastTag = gr.asset_tag.toString();

            var lastIncrement = parseInt(lastTag.substring(7), 10); // Get the numeric part

            increment = lastIncrement + 1;

        }

 

        // Pad increment with leading zeros and format tag

        var incrementStr = increment.toString().padStart(6, '0');

        var assetTag = countryCode + '-' + modelCategoryCode + '-' + incrementStr;

 

        // Set the generated asset tag

        current.asset_tag = assetTag;

 

        // Make asset tag read-only

        current.setReadOnly('asset_tag', true);

    }

 

})(current, previous);

Note: Also making field read only 

Hope this will help you.

Runjay Patel
Giga Sage

Hi @Amit_H ,

 

You can follow below steps.

  1. Create one property to hold the auto increment number.
  2. Create before BR on "alm_hardware" table as this table contains all the hardware asset. If you will do it on "alm_asset" then it will generate for consumable item as well, which supposed to not happen.
  3. Use below code to generate the tag and read and set back auto increment value.
 var country = current.location.country;
    var modelCode = current.model_category.code;

    var oldNum = gs.getProperty('tag_number');
    var newNum = parseInt(oldNum) + 1;
    var finalNum = String(newNum).padStart(6, '0');

    var assetTag = country + '-' + modelCode + '-' + finalNum;
    current.asset_tag = assetTag;

    gs.setProperty('tag_number', newNum);

RunjayPatel_0-1730870960582.png

RunjayPatel_1-1730871115553.png

 

 

4. Create one UI policy to make field read only. Add condition same as business rule except "asset tag is not empty".

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Part 2. In this video i have talked about overview on ServiceNow platform/tool. How you can opt for personal dev instance (PDI)? how to login in ServiceNow instance and navigation to OOB modules. For document please visit: https://servicenowwithrunjay.com/ Follow Facebook page for latest update on

Amit_H
Tera Contributor

Thanks @Abhay Kumar1 and @Runjay Patel 

 

Thanks for the reply will try and let you know.