The CreatorCon Call for Content is officially open! Get started here.

How do I populate a reference field in a MRVS, from onload script?

Chris Macdonald
Tera Contributor

Hi all,

I am trying to populate a multi row variable set via an onload script.

The mrvs is called software_requested, and contains two variables.  'justification' is a multi line text field, no problem.  'software' is a reference field and this is where I'm failing.  I tried passing in the sys id of the documents I need to select, but no good.  My code below.

Any assistance greatly appreciated.

Chris

 

function onLoad() {
    //Type appropriate comment here, and begin script below
    //   function onLoad() {

    var rowObj = [];
    rowObj.push({
        "actions": "",
        "software": '32b3fd841baf3810183cffbdcc4bcb81',
        "justification":"New starter - Advisory"
    });
    rowObj.push({
        "actions": "",
        "software": '10d3b9841baf3810183cffbdcc4bcb8e',
        "justification":"New starter - Advisory"
    });

    g_form.setValue('software_requested', JSON.stringify(rowObj));
}

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

are you sure you are setting the correct sys_id of the record from the table which is being referred by software variable?

Not working for admins or non-admins?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Chetan Mahajan
Kilo Sage

Hi Chris,

                    Refer this article it may help you Multi Row Variable Set autofill from server side data.

Kindly mark correct and helpful if applicable.

Mohith Devatte
Tera Sage
Tera Sage

Hello,

Please make sure you are doing below things correctly

1) passing the correct sys_id 

2) while setting the value correct MVRS back end name and try using below code .

3) Make sure your client script is at catalog item level and not inside MVRS.

4) Also make sure you used correct back end names for field too(software and justification)

I removed actions element from JSON because we need set actions for MVRS that will be take care Automatically we just have to set the field values in key value pair like below.

function onLoad() {
   

    var rowObj = [];
 rowObj.push(
{
 "software": '32b3fd841baf3810183cffbdcc4bcb81',
 "justification":"New starter - Advisory"
 }, 
{
"software": '10d3b9841baf3810183cffbdcc4bcb8e',
 "justification":"New starter - Advisory"
}
);
g_form.setValue('software_requested', JSON.stringify(rowObj));
}

Please mark my answer correct it helps you 

Chris Macdonald
Tera Contributor

Thank you all for your excellent assistance.  Turned out to be a dumb user error. I hadn't checked properly and the field was not a reference, but a lookup select box. As soon as I converted it to a reference, all worked fine.  Oops.  Thanks again