Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Debug the Record Producer Script

ShivangiC
Tera Contributor

I have written a script to map the fields on custom table and create a record in another custom table as well [ as record producer is linked to another custom table]. We need record producer to create records in 2 tables based on Expense Category [reference type variable] shown as dropdown in record producer. There are 2 different MRVS for 2 different Categories. Here is the Script:-

 

var erSysId = current.sys_id; // Get current sys_id now
 var currency = producer.currency;
current.description = "Expense Request raised by Record Producer ";


//Process Multi-row Variable Set (MRVS)
if(producer.getDisplayValue('expense_choice') == "Planned Expense"){
    var lineItems = producer.line_items;
    var rowCount = lineItems.getRowCount();

    for (var i = 0; i < rowCount; i++) {
        var lineItem = lineItems.getRow(i);
        var li = new GlideRecord('x_merl4_expense_ma_line_of_items');
        li.initialize();
        li.er_no = erSysId;
        li.expected_budget=lineItem.expected_budget;
        li.currency = currency;
        li.advance_amount_requested = lineItem.adv_amt;
        li.start_date_of_expense = producer.start_date;
        li.end_date_of_expense=producer.end_date;
        li.expense_type = lineItem.expense_type;
        li.short_description = lineItem.description;
        li.expected_budget=lineItem.expected_budget;
        li.state = 'Request Submitted';
        li.insert();
    }}
    if(producer.getDisplayValue('expense_choice') == 'Claim Expense'){
        var items = producer.line_items_claim;
        var rows = items.getRowCount();

        for (var i = 0; i < rows; i++){
            var line_Item = items.getRow(i);
            var li = new GlideRecord('x_merl4_expense_ma_line_of_items');
            li.initialize();
            li.er_no = erSysId;
            li.expense_type=items.exp_type;
// Error line, acc. to log Unable to fetch exp_amount variable of this record producer
            li.amount=producer.currency+items.exp_amount; 
            li.attachement=items.bill_receipt;
            li.short_description=items.short_description;
            li.state='Request Submitted';
            li.insert();
        }
    }
 
I cross checked variable name is correct but why it not fetching the value I am unable to understand. Please debug the script.
1 ACCEPTED SOLUTION

YaswanthKurre
Tera Guru

Hi Shivangi,

 

The error occurs because you're referencing "items.exp_amount" instead of the current row's "line_Item.exp_amount." Here's the corrected script: 

 

var erSysId = current.sys_id; // Get current sys_id now
var currency = producer.currency;
current.description = "Expense Request raised by Record Producer";

// Process Multi-row Variable Set (MRVS)
if(producer.getDisplayValue('expense_choice') == "Planned Expense"){
    var lineItems = producer.line_items;
    var rowCount = lineItems.getRowCount();

    for (var i = 0; i < rowCount; i++) {
        var lineItem = lineItems.getRow(i);
        var li = new GlideRecord('x_merl4_expense_ma_line_of_items');
        li.initialize();
        li.er_no = erSysId;
        li.expected_budget = lineItem.expected_budget;
        li.currency = currency;
        li.advance_amount_requested = lineItem.adv_amt;
        li.start_date_of_expense = producer.start_date;
        li.end_date_of_expense = producer.end_date;
        li.expense_type = lineItem.expense_type;
        li.short_description = lineItem.description;
        li.expected_budget = lineItem.expected_budget;
        li.state = 'Request Submitted';
        li.insert();
    }
} else if(producer.getDisplayValue('expense_choice') == 'Claim Expense'){
    var items = producer.line_items_claim;
    var rows = items.getRowCount();

    for (var i = 0; i < rows; i++){
        var line_Item = items.getRow(i); // Access the current row
        var li = new GlideRecord('x_merl4_expense_ma_line_of_items');
        li.initialize();
        li.er_no = erSysId;
        li.expense_type = line_Item.exp_type; // Use line_Item.exp_type
        li.amount = line_Item.exp_amount; // Use line_Item.exp_amount (not producer.currency + ...)
        li.attachement = line_Item.bill_receipt; // Ensure variable name matches (e.g., "attachement" vs "attachment")
        li.short_description = line_Item.short_description;
        li.state = 'Request Submitted';
        li.insert();
    }
}

 

Mark this as helpful and correct, if this solves your issue.

 

Thanks,

Yaswanth

View solution in original post

1 REPLY 1

YaswanthKurre
Tera Guru

Hi Shivangi,

 

The error occurs because you're referencing "items.exp_amount" instead of the current row's "line_Item.exp_amount." Here's the corrected script: 

 

var erSysId = current.sys_id; // Get current sys_id now
var currency = producer.currency;
current.description = "Expense Request raised by Record Producer";

// Process Multi-row Variable Set (MRVS)
if(producer.getDisplayValue('expense_choice') == "Planned Expense"){
    var lineItems = producer.line_items;
    var rowCount = lineItems.getRowCount();

    for (var i = 0; i < rowCount; i++) {
        var lineItem = lineItems.getRow(i);
        var li = new GlideRecord('x_merl4_expense_ma_line_of_items');
        li.initialize();
        li.er_no = erSysId;
        li.expected_budget = lineItem.expected_budget;
        li.currency = currency;
        li.advance_amount_requested = lineItem.adv_amt;
        li.start_date_of_expense = producer.start_date;
        li.end_date_of_expense = producer.end_date;
        li.expense_type = lineItem.expense_type;
        li.short_description = lineItem.description;
        li.expected_budget = lineItem.expected_budget;
        li.state = 'Request Submitted';
        li.insert();
    }
} else if(producer.getDisplayValue('expense_choice') == 'Claim Expense'){
    var items = producer.line_items_claim;
    var rows = items.getRowCount();

    for (var i = 0; i < rows; i++){
        var line_Item = items.getRow(i); // Access the current row
        var li = new GlideRecord('x_merl4_expense_ma_line_of_items');
        li.initialize();
        li.er_no = erSysId;
        li.expense_type = line_Item.exp_type; // Use line_Item.exp_type
        li.amount = line_Item.exp_amount; // Use line_Item.exp_amount (not producer.currency + ...)
        li.attachement = line_Item.bill_receipt; // Ensure variable name matches (e.g., "attachement" vs "attachment")
        li.short_description = line_Item.short_description;
        li.state = 'Request Submitted';
        li.insert();
    }
}

 

Mark this as helpful and correct, if this solves your issue.

 

Thanks,

Yaswanth