- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 08:15 PM
Hi,
I have a requirement to generate asset tag based on location's country with below conditions.
- It should only generate for India.
- 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.
- Once code generated asset tag should become read only
Can someone help me with code.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 09:32 PM
Hi @Amit_H ,
You can follow below steps.
- Create one property to hold the auto increment number.
- 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.
- 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);
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
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 08:30 PM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 09:32 PM
Hi @Amit_H ,
You can follow below steps.
- Create one property to hold the auto increment number.
- 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.
- 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);
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
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 09:49 PM