In MRVS how to get only 1st Category from multiple Model Categories

Shekhar Deshin1
Mega Guru

@Ankur Bawiskar 

 

In MRVS how to get only 1st Category from multiple Model Categories

Under cmdb_model table we have field Model Categories how i can take only first Category and add to MRVS data.

Now adding it takes all the categories.

Example:

Model Categories- Disk,Server,Linux,test

in MRVS i need only first Record to add here is 'Disk' how to get only 1st Value.

 

Thanks,

Shekhar,

 

 

1 ACCEPTED SOLUTION

thats right from client script you can call the script include

Glad I could help! If this solved your issue, please mark it as ✅ Helpful and ✅ Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****

View solution in original post

5 REPLIES 5

Ajay_Chavan
Kilo Sage

here is the sample model:

Screenshot 2025-09-06 at 00.55.01.png

 

Run the script in BG:

Screenshot 2025-09-06 at 00.56.23.png

 

BG script: 

// Server-side script for glide_list field - First category only

// Get first category from your specific model
var modelGR = new GlideRecord('cmdb_model');
if (modelGR.get('8583ce8237732000158bbfc8bcbe5dd9')) {
    
    // Get glide_list value (comma-separated sys_ids)
    var categoryList = modelGR.getValue('cmdb_model_category');
    
    if (categoryList) {
        // Get first sys_id from the list
        var firstCategorySysId = categoryList.split(',')[0];
        
        // Get the category name
        var categoryGR = new GlideRecord('cmdb_model_category');
        if (categoryGR.get(firstCategorySysId)) {
            var firstCategoryName = categoryGR.getDisplayValue();
            
            gs.print('Model: ' + modelGR.getDisplayValue());
            gs.print('All categories: ' + modelGR.getDisplayValue('cmdb_model_category'));
            gs.print('First category sys_id: ' + firstCategorySysId);
            gs.print('First category name: ' + firstCategoryName);
        }
    }
}

// Function for reuse
function getFirstCategory(modelSysId) {
    var gr = new GlideRecord('cmdb_model');
    if (gr.get(modelSysId)) {
        var catList = gr.getValue('cmdb_model_category');
        if (catList) {
            var firstCatId = catList.split(',')[0];
            var catGR = new GlideRecord('cmdb_model_category');
            if (catGR.get(firstCatId)) {
                return catGR.getDisplayValue();
            }
        }
    }
    return '';
}

// Usage with your model:
var firstCat = getFirstCategory('8583ce8237732000158bbfc8bcbe5dd9');
Glad I could help! If this solved your issue, please mark it as ✅ Helpful and ✅ Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****

Thanks for your reply and suggestions can you let me know on MRVS how i can do this validation you mean i need to write the script include and from there i need to get on Client MRVS side?

 

thats right from client script you can call the script include

Glad I could help! If this solved your issue, please mark it as ✅ Helpful and ✅ Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****

Ankur Bawiskar
Tera Patron
Tera Patron

@Shekhar Deshin1 

what type of variable are you using in MRVS? share screenshot of variable config

what did you try?

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