Hello,
The following is a User Event Script used to Create An Inventory Item upon button push from a Custom Record Form
/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
define(["N/ui/dialog", "N/ui/message", "N/search", "N/record", "N/currentRecord"], runScript);
function runScript(dialog, message, search, record, currentRecord){
// /**
// * @param {ClientScriptContext.saveRecord} context
// */
return{
saveRecord: function (context){
var title = 'saveRecord() :: ';
try {
var currentRecord = context.currentRecord;
var ReqRec = currentRecord.id;
log.debug({
title: "Rec Id",
details: ReqRec
});
console.log(ReqRec);
var TrfLinesRec = {};
var subsidiary = currentRecord.getValue("custrecord_itbs_cps_subsidary__");
var reqtranid = currentRecord.getValue("custrecord122");
if (reqtranid) {
var transRec = record.load({
type: "customrecord_cps_contrat_de_vente_a_reme",// contrat de vente id
id: reqtranid,
});
var agence = transRec.getValue("custrecord2488");
log.debug({
title: "Agence",
details: agence
});
var nom = transRec.getValue("custrecord2490");
log.debug({
title: "Nom Client",
details: nom
});
var valeur = transRec.getValue("custrecord_itbs_cps_ventre_chiffre");
log.debug({
title: "Valeur Vente",
details: valeur
});
var idcontrat = transRec.getValue("customrecord_cps_contrat_de_vente_a_reme");
log.debug({
title: "Id Contrat",
details: idcontrat
});
var type = transRec.getValue("custrecord_itbs_cashdeal_type_");
log.debug({
title: "Type ",
details: type
});
}
var line = currentRecord.getLineCount("recmachcustomrecord_itbs_cd_cashimmo_contrat");
console.log(line);
var TrfLines = [];
for (var i = 0; i < line; i++) {
var intid = currentRecord.getSublistValue({
sublistId: "recmachcustomrecord_itbs_cd_cashimmo_contrat",
fieldId: "recordid",
line: i,
});
log.debug({
title: "Line Id",
details: intid
});
var process = currentRecord.getSublistValue({
sublistId: "recmachcustomrecord_itbs_cd_cashimmo_contrat",
fieldId: "custrecord_itbs_cd_description",
line: i,
});
log.debug({
title: "process",
details: process
});
var numeroTF = currentRecord.getSublistValue({
sublistId: "recmachcustomrecord_itbs_cd_cashimmo_contrat",
fieldId: "custrecord_itbs_cd_no_tf",
line: i,
});
if (process == true) {
TrfLines.push({
recordid: intid,
process: process,
numeroTF: numeroTF,
/* item: item,
transqty: transqty,
cost: unitcost,
lotnum: lotNum,
bin: bin,
isUseBin: isUseBin*/
});
}
}
TrfLinesRec = {
reqtranid: reqtranid,
subsidiary: subsidiary,
agence: agence,
nom: nom,
valeur: valeur,
idcontrat: idcontrat,
type: type,
// subaccount: subaccount,
TrfLines: TrfLines,
};
log.debug({
title: "item info",
details: JSON.stringify(TrfLinesRec)
});
log.debug({
title: "length",
details: TrfLinesRec.TrfLines.length
});
if (TrfLinesRec.TrfLines.length) {
console.log('TrfLinesRec:', TrfLinesRec)
createItemRecords({
lineArr: TrfLinesRec,
id: ReqRec,
});
}
return true;
} //End of TRY
catch (e) {
log.error('Error in ' + title, e.message + " : " + e);
}
},// End of SAVED RECORD(GET.VALUE)
/**
* Create Inventory Item record
*
* @governance 0 units
* @param {Object} context: contains the new and old record objects
* @return {Object} beforeSubmit
*/
function :createItemRecords(params){
var title = 'createItemRecords() :: ';
try {
var TrfLinesSrchRes = params.lineArr;
log.debug({
title: "create inventory"
});
// create inventory item records
var InvTRecord = record.create({
//@governance 2 Units
type: record.Type.INVENTORY_ITEM,
isDynamic: true,
});
InvTRecord.setValue({
fieldId: "class",
value: TrfLinesSrchRes.type,
});
InvTRecord.setValue({
fieldId: "subsidiary",
value: TrfLinesSrchRes.subsidiary,
});
InvTRecord.setValue({
fieldId: "location",
value:TrfLinesSrchRes.agence,
});
InvTRecord.setValue({
fieldId: "custitem_nom_clien",
value: TrfLinesSrchRes.nom,
});
InvTRecord.setValue({
fieldId: "custitem_cps_id",
value: rfLinesSrchRes.idcontrat
});
InvTRecord.setValue({
fieldId: "purchasedescription",
value: TrfLinesSrchRes.process,
});
InvTRecord.setValue({
fieldId: "itemid",
value: TrfLinesSrchRes.recordid,
});
InvTRecord.setValue({
fieldId: "displayname",
value: TrfLinesSrchRes.numeroTF,
});
}// END OF TRY FUNCTION
catch (e) {
log.error('Error in ' + title, e.message + " : " + e);
}
}
function itemsearch(params) {
var recId = params.id;
log.debug({
title: "recId",
details: recId
});
}
}