convert JSON.stringgify() to Parse and display a value

Somasekhar6
Tera Contributor

Hi team, I have one Multi Row variable Set in RITM with help of Script include I am getting those details as below.

User Assets
Model Life Cycle Stage Life Cycle Stage Status Serial Number
Hewlett Packard EliteBook 800OperationalIn Use5CG2134KRM
HP HP EliteBook 845 G8 Notebook PCInventoryAvailable5CG22624QB

 

after RITM generation i need to send notification with details of User and Asset model only I used the below email script to fetch a values but user name is getting correct but in place of model it's showing SYSID of that model how can i show the display value? 

 

in script include i'm setting values for Multirow variable set.

 

Script Include: 

 

var listValuename = [];

        {


            var assignedto = this.getParameter('sysparm_assignedto');
            var grasset = new GlideRecord('alm_hardware');
            grasset.addQuery('assigned_to', assignedto);
            grasset.addEncodedQuery('model_category=81feb9c137101000deeabfc8bcbe5dc4^ORmodel_category=8a6187671b6a01502c37c882604bcb2e^ORmodel_category=0be11f58c7432010f74c784c95c260b7^life_cycle_stage=Operational^life_cycle_stage_status=In Use');
            grasset.query();
            while (grasset.next()) {


                listValuename.push({

                    "model": grasset.getValue('sys_id'),
                    "life_cycle_stage": grasset.getValue('sys_id'),
                    "life_cycle_stage_status": grasset.getValue('sys_id'),
                    "serial_number": grasset.getValue('sys_id')
                });

            }
        }

        return JSON.stringify(listValuename);
   
 

 


email Script:

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here
    var asset = current.variables.user_s_assets.model;
    var associate = current.variables.userid.getDisplayValue();

    template.print("Associate Name is " + associate);
    template.print("<br>");
    template.print("Asset Name " + asset);

})(current, template, email, email_action, event);

 

Notification Preview as below

 

Associate Name is M Somasekhar 
Asset Name [00031a741bb115d478cf54e8624bcbec, 7c892eb41bb915d478cf54e8624bcb0a]

Action Initiated: Sample Catalog


Thanks for your Time and support

 

2 REPLIES 2

Rahul Talreja
Mega Sage
Mega Sage

Hi @Somasekhar6 ,
Have you tried using:

 var asset = current.variables.user_s_assets.model.getDisplayValue('display_name');
Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Somasekhar6 

It seems like the issue is that you are getting the sys_id of the asset model instead of its display value. To get the display value of the asset model, you need to use getDisplayValue() instead of getValue('sys_id').

Let's update your Script Include to fetch the display values for the model, life cycle stage, life cycle stage status, and serial number:

var listValuename = [];

// Your existing code

while (grasset.next()) {
    listValuename.push({
        "model": grasset.model.getDisplayValue(),
        "life_cycle_stage": grasset.life_cycle_stage.getDisplayValue(),
        "life_cycle_stage_status": grasset.life_cycle_stage_status.getDisplayValue(),
        "serial_number": grasset.serial_number.getDisplayValue()
    });
}

// Rest of your code (if any)

return JSON.stringify(listValuename);

 

 

 

By using getDisplayValue(), you will get the display value for each field instead of the sys_id. With this update, your JSON data will contain the actual names of the model, life cycle stage, life cycle stage status, and serial number.

Additionally, in your email script, you need to loop through the asset list since it's a multi-row variable set. Here's the updated email script:

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here
    var assets = JSON.parse(current.variables.user_s_assets);
    
    for (var i = 0; i < assets.length; i++) {
        var asset = assets[i];
        var associate = current.variables.userid.getDisplayValue();

        template.print("Associate Name is " + associate);
        template.print("<br>");
        template.print("Asset Model: " + asset.model);
        template.print("<br>");
        template.print("Life Cycle Stage: " + asset.life_cycle_stage);
        template.print("<br>");
        template.print("Life Cycle Stage Status: " + asset.life_cycle_stage_status);
        template.print("<br>");
        template.print("Serial Number: " + asset.serial_number);
        template.print("<br>");
        template.print("<br>");
    }
})(current, template, email, email_action, event);

 

 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)