Multirow variable data to description of HR case

si21
Tera Guru

Hi experts,

How can we populated multirow variable data on record producer to the generated HR case description ?

Please provide me your suggestions

Thanks in advance.

7 REPLIES 7

Astik Thombare
Tera Sage

Hi @si21 ,

 

To populate multi-row variable data on a record producer to the generated HR case description in ServiceNow, you can follow these steps:

  1. Create the Multi-Row Variable Set:

    • Define a multi-row variable set in your catalog item or record producer.
    • Add the necessary variables to this multi-row variable set.
  2. Define the Record Producer:

    • Create a record producer for your HR case.
    • Include the multi-row variable set you created in the record producer.
  3. Script to Populate HR Case Description:

    • Use a script in the "Script" field of the record producer or in a business rule to retrieve the data from the multi-row variable set and format it into the HR case description.

Here's a step-by-step guide with sample code:

Step 1: Create Multi-Row Variable Set

  1. Navigate to Service Catalog > Catalog Definition > Maintain Items.
  2. Select or create the catalog item/record producer.
  3. Scroll down to the "Variable Sets" related list and click "New".
  4. Choose "Multi-Row Variable Set" and define the variables.

Step 2: Create Record Producer

  1. Navigate to Service Catalog > Catalog Definitions > Record Producers.
  2. Click "New" and fill in the details for your HR case record producer.
  3. Under the "Variables" tab, include your multi-row variable set.

Step 3: Script to Populate HR Case Description

In the "Script" field of the record producer, you can write a script to process the multi-row variable data. Here is a sample script:

 

 

 

(function() {
    // Retrieve the record producer variables
    var gr = current; // 'current' is the record producer record

    // Initialize a description string
    var description = 'HR Case Description:\n';

    // Retrieve the multi-row variable set
    var multiRowVariableSet = gr.variables.multi_row_variable_set_name; // Replace with your multi-row variable set name

    // Iterate over each row in the multi-row variable set
    for (var i = 0; i < multiRowVariableSet.getRowCount(); i++) {
        var row = multiRowVariableSet.getRow(i);
        description += '\nRow ' + (i + 1) + ':\n';
        
        // Iterate over each variable in the row and append to description
        for (var field in row) {
            if (row.hasOwnProperty(field)) {
                var variable = row[field];
                description += variable.getLabel() + ': ' + variable + '\n';
            }
        }
    }

    // Set the description field on the HR case
    gr.description = description;

    // Optionally, set other fields or take additional actions
    // gr.short_description = 'HR Case Created from Record Producer';
    // gr.category = 'HR Category';
    // gr.insert(); // If you need to insert the record manually

})();

 

 

 

Explanation

  1. Retrieve Record Producer Variables: The current object represents the record producer record.
  2. Initialize Description: Start with an initial description string.
  3. Retrieve and Iterate Multi-Row Variable Set: Access the multi-row variable set and iterate over each row.
  4. Append Each Variable to Description: For each variable in the row, append its label and value to the description string.
  5. Set Description: Assign the constructed description to the HR case's description field.

Deploy the Script

  1. Place the script in the Script field of the record producer.
  2. Test the record producer by filling in the multi-row variables and submitting the form.
  3. Verify that the HR case is created with the correctly formatted description.

Additional Considerations

  • Security and Permissions: Ensure the script has the necessary permissions to access the variables and set fields on the HR case.
  • Error Handling: Add error handling to manage cases where variables might be empty or missing.
  • Performance: For large datasets, consider optimizing the script for better performance.

By following these steps, you can successfully populate multi-row variable data from a record producer into the HR case description in ServiceNow.

 

Thanks,
Astik

Hi Astik, I tried this code and description is not getting populated. It is empty.

Ankur Bawiskar
Tera Patron
Tera Patron

@si21 

in the record producer script; access the MRVS i.e. json; parse it and populate

something similar to this; but enhance it

How to transfer a multi-row variable set from a record producer to a case form 

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

Sid_Takali
Kilo Patron
Kilo Patron

HI @si21 Write a script like this in Record Producer Script field

 

var mvrs = JSON.parse(producer.mrvs_Name);
var desc1 = '';
for (var i = 0; i < mvrs.length; i++) {
    desc1 += "MRVS variable 1 # " + " : " + mvrs[i].mrvs_variable_1 + "  MRVS variable 2 # " + " : " + mvrs[i].mrvs_variable_1 + "  MRVS variable 3 #" + " : " + mvrs[i].mrvs_variable_3 + '\n';
}
current.description =  desc1;

 

The above script will populate MRVS data to Description field like below

SiddharamTakali_0-1718357820290.png

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0863636#:~:text=MRVS%20variab...

 

Regards,

Sid