С помощью друга Кенджи я выкладываю обновленный скрипт.Я надеюсь, что это может быть полезно для всех.Спасибо!
/**
* @NApiVersion 2.x
* @NScriptType userEventScript
* @author Adriano Barbosa
* @since 2019.2
*/
define(['N/log', 'N/record', 'N/search'], function(log, record, search) {
function devolucaoFornecedor(context) {
var df, itemCount, id_item_linha, bsc_id_item_linha, qtde_item_linha, peso_item, peso_bl, volumes,
peso_total_itens, fornecedor;
var bodyObject = {};
var itens = [];
df = context.newRecord;
itemCount = df.getLineCount({ sublistId: 'item' });
volumes = 0;
peso_total_itens = 0;
for ( linha_item=0; linha_item<itemCount; linha_item++ ) {
id_item_linha = df.getSublistValue({ sublistId: 'item', fieldId: 'item', line: linha_item });
bsc_id_item_linha = search.lookupFields({ type: 'inventoryitem', id: id_item_linha,
columns: [ 'itemid', 'custitem_enl_weight' ]
});
qtde_item_linha = df.getSublistValue({ sublistId: 'item', fieldId: 'quantity', line: linha_item });
peso_item = bsc_id_item_linha.custitem_enl_weight;
// peso_bl = (peso_item x qtde_item_linha)
peso_bl = peso_item * qtde_item_linha;
// volumes = quantidade de todos os itens da DF
volumes = qtde_item_linha + volumes;
peso_total_itens = peso_bl + peso_total_itens;
var itensObj = {};
itensObj['codigo_item_linha'] = bsc_id_item_linha.itemid;
itensObj['qtde_item_linha'] = qtde_item_linha;
itensObj['peso_item'] = peso_item || 0;
itensObj['peso_bl'] = peso_bl || 0;
itensObj['volumes'] = volumes;
itensObj['peso_total_itens'] = peso_total_itens;
itens.push(itensObj);
}
if ( context.type === context.UserEventType.CREATE ) {
bodyObject['total_linhas_itens'] = itemCount;
bodyObject['peso_geral'] = peso_total_itens;
bodyObject['volume_geral'] = volumes;
bodyObject['itens'] = itens;
df.setValue({ fieldId: 'custbody_enl_grossweight', value: peso_total_itens })
.setValue({ fieldId: 'custbody_enl_netweight', value: peso_total_itens })
.setValue({ fieldId: 'custbody_enl_volumesqty', value: volumes })
.setValue({ fieldId: 'custbody_enl_volumetype', value: 'PC' });
log.debug({ title: 'Devolução de Fornecedor cadastrada!', details: bodyObject });
return true;
} else if ( context.type === context.UserEventType.EDIT ) {
bodyObject['id_df'] = context.newRecord.id;
bodyObject['num_df'] = df.getValue({ fieldId: 'tranid' });
bsc_fornecedor_id_df = search.create({ type: "vendorreturnauthorization",
filters: [
[ "type", "anyof" , "VendAuth" ], "AND",
[ "internalid", "anyof" , bodyObject['id_df'] ], "AND",
["mainline","is","T"]
],
columns: [
search.createColumn({ name: "formulatext", formula: "{entity}", label: "fornecedor" })
]
}).run().each(function(result){
fornecedor = result.getValue({ name: 'formulatext' });
log.debug({ title: 'fornecedor', details: fornecedor });
return false;
});
bodyObject['fornecedor'] = fornecedor || 'Vide num_df';
bodyObject['total_linhas_itens'] = itemCount;
bodyObject['peso_geral'] = peso_total_itens;
bodyObject['volume_geral'] = volumes;
bodyObject['itens'] = itens;
df.setValue({ fieldId: 'custbody_enl_grossweight', value: peso_total_itens })
.setValue({ fieldId: 'custbody_enl_netweight', value: peso_total_itens })
.setValue({ fieldId: 'custbody_enl_volumesqty', value: volumes })
.setValue({ fieldId: 'custbody_enl_volumetype', value: 'PC' });
log.debug({ title: 'Devolução de Fornecedor Modificada', details: bodyObject });
return true;
}
return true;
}
return { beforeSubmit: devolucaoFornecedor }
})