You have attempted an invalid sublist or line item operation error
Why I am getting error as “You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist.”
I am trying to set a value on apply sublist field on a payment record in netsuite.
/**
*@NApiVersion 2.1
*@NScriptType UserEventScript
*/
define([‘N/record’,’N/search’], function (record,search) {
function afterSubmit(context) {
try {
if (context.type == context.UserEventType.CREATE || context.type == context.UserEventType.EDIT) {
var inv_Rec = context.newRecord;
var inv_id = inv_Rec.id;
log.debug(‘Invoice Id’, inv_id);
var loadrec = record.load({
type: record.Type.INVOICE,
id: inv_id
});
var card_Val = loadrec.getValue({ fieldId: ‘custbody_celigo_shopify_giftcard_id’ }); // shopify_giftcard_id
log.debug(“card_Val”, card_Val);
if (card_Val != null && card_Val != ” && card_Val != ‘null’ && card_Val != undefined && card_Val != ‘undefined’ && card_Val != ‘@NONE@’ && card_Val != ‘NaN’) {
var DueAmount = loadrec.getValue({ fieldId: ‘amountremainingtotalbox’ }); //Amount Due
log.debug(“DueAmount”, DueAmount);
var Shopify_Dt = loadrec.getValue({ fieldId: ‘custbody9’ });
log.debug(“Shopify date”, Shopify_Dt);
if (DueAmount != null && DueAmount != ” && DueAmount != ‘null’ && DueAmount != undefined && DueAmount != ‘undefined’ && DueAmount != ‘@NONE@’ && DueAmount != ‘NaN’ || DueAmount == 0) {
var salesOrders = search.create({
type: “salesorder”,
filters:
[
[“type”, “anyof”, “SalesOrd”],
“AND”,
[“applyingtransaction.internalid”, “anyof”, inv_id],
“AND”,
[“mainline”, “is”, “F”]
],
columns:
[
search.createColumn({
name: “internalid”,
summary: “GROUP”,
label: “Internal ID”
}),
search.createColumn({
name: “shipcountry”,
summary: “GROUP”,
label: “Shipping Country”
})
]
});
log.debug(“salesOrders”, salesOrders);
let searchResult = salesOrders.run().getRange({ start: 0, end: 10 });
if (searchResult.length > 0) {
var so_id = searchResult[0].getValue({
name: “internalid”,
summary: “GROUP”,
label: “Internal ID”
});
var shipCountry = searchResult[0].getValue({
name: “shipcountry”,
summary: “GROUP”,
label: “Shipping Country”
});
log.debug(“so_id”, so_id);
log.debug(“shipCountry”, shipCountry);
}
//if (shipCountry == ‘CA’ || shipCountry == ‘AU’) {
var trRecord = record.transform({
fromType: record.Type.INVOICE,
fromId: inv_id,
toType: record.Type.CUSTOMER_PAYMENT,
isDynamic: true,
});
log.debug(“Transfer Record”, trRecord);
trRecord.setValue({
fieldId: ‘paymentoption’,
value: 18
});
trRecord.setValue({
fieldId: ‘custbody_salesord_id’,
value: so_id
});
trRecord.setValue({
fieldId: ‘trandate’,
value: Shopify_Dt
});
trRecord.setValue({
fieldId: ‘account’,
value: 959
});
trRecord.setValue({
fieldId: ‘paymentoption’,
value: 15
});
var lines=trRecord.selectNewLine({sublistId:’apply’});
log.debug(“lines”,lines);
trRecord.setCurrentSublistValue({sublistId:’apply’, fieldId: ‘amount’, value:DueAmount});
trRecord.commitLine({sublistId:’apply’});
var rec_id = trRecord.save();
log.debug(“save record”, rec_id);
//}
}
}
}
}
catch (e) {
log.error({
title: ‘Error ‘,
details: e.message
});
}
}
return {
afterSubmit: afterSubmit
};
});
Because you cannot selectNewLine on the apply sublist. Typically, you find the existing line you need and set the apply field to true.
Now also I am getting same error,
/**
*@NApiVersion 2.1
*@NScriptType UserEventScript
*/
define([‘N/record’,’N/search’], function (record,search) {
function afterSubmit(context) {
try {
if (context.type == context.UserEventType.CREATE || context.type == context.UserEventType.EDIT) {
var inv_Rec = context.newRecord;
var inv_id = inv_Rec.id;
log.debug(‘Invoice Id’, inv_id);
var loadrec = record.load({
type: record.Type.INVOICE,
id: inv_id
});
var card_Val = loadrec.getValue({ fieldId: ‘custbody_celigo_shopify_giftcard_id’ }); // shopify_giftcard_id
log.debug(“card_Val”, card_Val);
if (card_Val != null && card_Val != ” && card_Val != ‘null’ && card_Val != undefined && card_Val != ‘undefined’ && card_Val != ‘@NONE@’ && card_Val != ‘NaN’) {
//var DueAmount = loadrec.getValue({ fieldId: ‘amountremainingtotalbox’ }); //Amount Due
var DueAmount=10;
log.debug(“DueAmount”, DueAmount);
var Shopify_Dt = loadrec.getValue({ fieldId: ‘custbody9’ });
log.debug(“Shopify date”, Shopify_Dt);
if (DueAmount != null && DueAmount != ” && DueAmount != ‘null’ && DueAmount != undefined && DueAmount != ‘undefined’ && DueAmount != ‘@NONE@’ && DueAmount != ‘NaN’ || DueAmount == 0) {
var salesOrders = search.create({
type: “salesorder”,
filters:
[
[“type”, “anyof”, “SalesOrd”],
“AND”,
[“applyingtransaction.internalid”, “anyof”, inv_id],
“AND”,
[“mainline”, “is”, “F”]
],
columns:
[
search.createColumn({
name: “internalid”,
summary: “GROUP”,
label: “Internal ID”
}),
search.createColumn({
name: “shipcountry”,
summary: “GROUP”,
label: “Shipping Country”
})
]
});
log.debug(“salesOrders”, salesOrders);
let searchResult = salesOrders.run().getRange({ start: 0, end: 10 });
if (searchResult.length > 0) {
var so_id = searchResult[0].getValue({
name: “internalid”,
summary: “GROUP”,
label: “Internal ID”
});
var shipCountry = searchResult[0].getValue({
name: “shipcountry”,
summary: “GROUP”,
label: “Shipping Country”
});
log.debug(“so_id”, so_id);
log.debug(“shipCountry”, shipCountry);
}
//if (shipCountry == ‘CA’ || shipCountry == ‘AU’) {
var trRecord = record.transform({
fromType: record.Type.INVOICE,
fromId: inv_id,
toType: record.Type.CUSTOMER_PAYMENT,
isDynamic: true,
});
log.debug(“Transfer Record”, trRecord);
trRecord.setValue({
fieldId: ‘paymentoption’,
value: 18
});
trRecord.setValue({
fieldId: ‘custbody_salesord_id’,
value: so_id
});
trRecord.setValue({
fieldId: ‘trandate’,
value: Shopify_Dt
});
trRecord.setValue({
fieldId: ‘account’,
value: 959
});
trRecord.setValue({
fieldId: ‘paymentoption’,
value: 15
});
trRecord.selectNewLine({
sublistId: ‘apply’
});
trRecord.setCurrentSublistValue({
sublistId: ‘apply’,
fieldId: ‘apply’,
value: true
});
trRecord.setCurrentSublistValue({
sublistId: ‘apply’,
fieldId: ‘amount’,
value: DueAmount,
ignoreFieldChange: true
});
trRecord.commitLine({
sublistId: ‘apply’
});
var rec_id = trRecord.save();
log.debug(“save record”, rec_id);
//}
}
}
}
}
catch (e) {
log.error({
title: ‘Error ‘,
details: e.message
});
}
}
return {
afterSubmit: afterSubmit
};
});



