How to Map the number Field from a Record Producer to Created Records in a Backend Table?

gautam_dhamija
Tera Contributor

I am working on a record producer in ServiceNow that uses an MRVS (Multi-Row Variable Set) to create multiple records in a backend table. While the records are being created successfully, the number field of the record (mapped to the record producer) is not being set in the backend table as expected.

Here’s the code I’m using:

var mrvs = producer.po_detail; // Retrieve the MRVS variable

// Parse the MRVS rows into JSON
var rows = JSON.parse(mrvs);

// Loop through each row in the parsed JSON
for (var i = 0; i < rows.length; i++) {
// Create a new record in the 'x_perii_account_0_purchase_order' table
var purchaseOrder = new GlideRecord('x_perii_account_0_purchase_order');
purchaseOrder.initialize();

// Map fields from MRVS
purchaseOrder.branch_code = rows[i].branch_code;
purchaseOrder.po_number = rows[i].po_number;
purchaseOrder.po_type = rows[i].po_type;
purchaseOrder.po_amount = rows[i].po_amount;
purchaseOrder.po_open_amount = rows[i].po_open_amount;
purchaseOrder.po_issue_date = rows[i].po_issue_date;

// Map the 'number' field from the record producer to the created record
purchaseOrder.invoice_master = current.number;

// Insert the record
purchaseOrder.insert();
}

While the other fields (like branch_code, po_number, etc.) are correctly mapped from the MRVS rows to the backend table, the invoice_master field (which should store the number from the record producer) remains blank in the created records.

How can I ensure the number field from the record producer is correctly set in the invoice_master field of the created records?

 

3 REPLIES 3

Runjay Patel
Giga Sage

Hi @gautam_dhamija ,

 

This is because Record Producer script will be executed before the actual Record is generated.

To update the number field you may have to write business rule.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Abhay Kumar1
Giga Sage

@gautam_dhamija The issue lies in how you're trying to access the current.number field in the script. In the context of a record producer, the current object refers to the record being created in the target table of the record producer, not the record producer itself. The number field might not yet be set or available when the script runs.

 

To ensure that the number field from the target record is correctly mapped to the invoice_master field in the backend table.

Hope this will help you.

Brad Warman
Giga Sage

Hi @gautam_dhamija. Double check that your field is indeed called number on the record producer and that it is configured as the same type as the invoice_master field.