Dynamically Populate MVRS based on Record Producer Variable Value

ShivangiC
Tera Contributor

My requirement is on populating a reference variable[reference to expense request table] 'Referral ticket' in record producer I want its related fields to auto-populate in record producer variables like start and end date of expense, title, cost center , sub-cost center etc and there are line items[another table] as a related list in expense request table, I want expense type and short description of it to auto populated as rows of multi row variable set in record producer.

 

I have written script include and catalog client script till populating the record producer variables. But how to populate MRVS with the line items info. This is my Script Include:- 

var response = {};
    var referralId = this.getParameter('sysparm_referral_id');
    if (!referralId) return JSON.stringify(response);

    var exp = new GlideRecord('x_merl4_expense_ma_expense_request');
    if (exp.get(referralId)) {
      response.start_date = exp.getValue('start_date_of_expense');
      response.end_date = exp.getValue('end_date_of_expense');
      response.title = exp.getValue('description');
      response.cost_center = exp.getValue('cost_center');
      response.sub_cost_center = exp.getValue('sub_cost_center');

      // Fetch related line items
      var lineItems = [];
      var li = new GlideRecord('x_merl4_expense_ma_line_of_items');
      li.addQuery('er_no', referralId);
      li.query();
      while (li.next()) {
        lineItems.push({
          expense_type: li.getValue('expense_type'),
          short_description: li.getValue('short_description')
        });
      }
      response.line_items = lineItems;
    }

    return JSON.stringify(response);
  },
 
 
Please help me with script to write for populating MRVS 
1 REPLY 1

ShivangiC
Tera Contributor

And this my Catalog client script which is not auto populating MRVS:- 

// Call a GlideAjax script to get the values from the referenced record
        var ga = new GlideAjax('GetReferralTicketDetails');
        ga.addParam('sysparm_name', 'getDetails');
        ga.addParam('sysparm_referral_id', newValue);
        ga.getXMLAnswer(function(response) {
            var result = JSON.parse(response);
            if (!result) return;

            g_form.setValue('cost_center', result.cost_center);
            g_form.setValue('sub_cost_center', result.sub_cost_center);
            g_form.setValue('start_date_of_expense', result.start_date);
            g_form.setValue('end_date_of_expense', result.end_date);
            g_form.setReadOnly('cost_center', true);
            g_form.setReadOnly('sub_cost_center', true);
            g_form.setValue('title',result.title);

            //Populating MRVS  
            var mrvs = 'line_items_claim';
            var row=[];
        result.line_items.forEach(function(item) {
                row.push({
                    'expense_type':item.expense_type,
                    'short_description':item.short_description
                });
                // Push the new record to the MRVS
            g_form.addInfoMessage("Rows of MRVS -> "+JSON.stringify(row));
            g_form.setValue(mrvs, JSON.stringify(row));
            });
        });