I'm having an JavaScript Global variable called itemId( this itemId is an Object with Key Value Pairs),this Object will store Standard Item Record Details.
I'm able to store the values for this at "User Event Script Before-Load Event" and trying to access the global variable "itemId" at the "Before-Submit Event" but this itemId(Object) is getting empty values.
when is try to get values on this object using the Key it is showing the Error :" :Cannot read property "ItemQuantityTolerance" from undefined"
Code:
SuiteScript Version:2.0
var itemId = new Object();
function beforeLoad(scriptContext) {
try {
var IR_Record = scriptContext.newRecord;
var form = scriptContext.form;
form.clientScriptFileId = 50137;
if (IR_Record != null && IR_Record != '' && IR_Record != undefined) {
var ItemsId = new Array();
var IR_LineItemCount = null;
var lineNumber = null;
IR_LineItemCount = IR_Record.getLineCount('item');
log.debug('Value', 'IR_LineItemCount : ' + IR_LineItemCount);
if (IR_LineItemCount != null && IR_LineItemCount != '' && IR_LineItemCount != undefined) {
for (var i = 0; i < IR_LineItemCount; i++) {
lineNumber = i;
log.debug('Value', 'LineNumber : ' + lineNumber);
var IR_ItemId = IR_Record.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: i
});
ItemsId[lineNumber] = IR_ItemId;
log.debug('Value', 'IR_ItemId : ' + IR_ItemId);
}
if (ItemsId != null && ItemsId != '' && ItemsId != undefined) {
log.debug('Value:', 'ItemsId:' + ItemsId);
getItemToleranceValues(ItemsId);
}
}
}
} catch (exception) {
log.debug("Value :", "Before-Load Error :", exception.message);
throw exception.message;
}
}
function getItemToleranceValues(ItemsId) {
try {
var ItemDetialsSearch = "";
var ItemSearchResults = "";
var Item_Id = null;
var Item_Name = "";
ItemDetialsSearch = search.create({
type: 'item',
filters: ['internalid', 'anyof', ItemsId],
columns: ['internalid', 'itemid']
});
if (ItemDetialsSearch != null && ItemDetialsSearch != '' && ItemDetialsSearch != undefined) {
ItemSearchResults = ItemDetialsSearch.run().getRange(0, 200);
if (ItemSearchResults != null && ItemSearchResults != '' && ItemSearchResults != undefined) {
for (var i = 0; i < ItemSearchResults.length; i++) {
var itemDetails = new Object();
Item_Id = ItemSearchResults[i].getValue('internalid');
Item_Name = ItemSearchResults[i].getValue('itemid');
if (Item_Name != null && Item_Name != '' && Item_Name != undefined) {
itemDetails['ItemName'] = Item_Name;
}
itemId[Item_Id] = itemDetails;
}
log.debug("itemId:", itemId);
}
}
} catch (exception) {
log.debug("Value:", "Item-Tolerance-Value Error:" + exception.message);
throw exception.message;
}
}
function beforeSubmit(scriptContext) {
try {
var IR_Record = scriptContext.newRecord;
var itemName = "";
if (IR_Record != null && IR_Record != '' && IR_Record != undefined) {
if (itemId != null && itemId != '' && itemId != undefined) {
log.debug("itemId:", itemId);
itemName = itemId[IR_ItemId]['ItemName']; // Getting Empty Values at this point, eventhough it is global variable with values
log.debug('Value', 'itemName : ' + itemName);
}
}
}catch(exception){
throw exception.message;
}
Example Values for the itemId object:
{
337: {
ItemName: "Apple",
},
359: {
ItemName: "Orange"
}
}
how to access the global Variable itemId get the value using it property "ItemName".
thanks in advance.
I'm having an JavaScript Global variable called itemId( this itemId is an Object with Key Value Pairs),this Object will store Standard Item Record Details.
I'm able to store the values for this at "User Event Script Before-Load Event" and trying to access the global variable "itemId" at the "Before-Submit Event" but this itemId(Object) is getting empty values.
when is try to get values on this object using the Key it is showing the Error :" :Cannot read property "ItemQuantityTolerance" from undefined"
Code:
SuiteScript Version:2.0
var itemId = new Object();
function beforeLoad(scriptContext) {
try {
var IR_Record = scriptContext.newRecord;
var form = scriptContext.form;
form.clientScriptFileId = 50137;
if (IR_Record != null && IR_Record != '' && IR_Record != undefined) {
var ItemsId = new Array();
var IR_LineItemCount = null;
var lineNumber = null;
IR_LineItemCount = IR_Record.getLineCount('item');
log.debug('Value', 'IR_LineItemCount : ' + IR_LineItemCount);
if (IR_LineItemCount != null && IR_LineItemCount != '' && IR_LineItemCount != undefined) {
for (var i = 0; i < IR_LineItemCount; i++) {
lineNumber = i;
log.debug('Value', 'LineNumber : ' + lineNumber);
var IR_ItemId = IR_Record.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: i
});
ItemsId[lineNumber] = IR_ItemId;
log.debug('Value', 'IR_ItemId : ' + IR_ItemId);
}
if (ItemsId != null && ItemsId != '' && ItemsId != undefined) {
log.debug('Value:', 'ItemsId:' + ItemsId);
getItemToleranceValues(ItemsId);
}
}
}
} catch (exception) {
log.debug("Value :", "Before-Load Error :", exception.message);
throw exception.message;
}
}
function getItemToleranceValues(ItemsId) {
try {
var ItemDetialsSearch = "";
var ItemSearchResults = "";
var Item_Id = null;
var Item_Name = "";
ItemDetialsSearch = search.create({
type: 'item',
filters: ['internalid', 'anyof', ItemsId],
columns: ['internalid', 'itemid']
});
if (ItemDetialsSearch != null && ItemDetialsSearch != '' && ItemDetialsSearch != undefined) {
ItemSearchResults = ItemDetialsSearch.run().getRange(0, 200);
if (ItemSearchResults != null && ItemSearchResults != '' && ItemSearchResults != undefined) {
for (var i = 0; i < ItemSearchResults.length; i++) {
var itemDetails = new Object();
Item_Id = ItemSearchResults[i].getValue('internalid');
Item_Name = ItemSearchResults[i].getValue('itemid');
if (Item_Name != null && Item_Name != '' && Item_Name != undefined) {
itemDetails['ItemName'] = Item_Name;
}
itemId[Item_Id] = itemDetails;
}
log.debug("itemId:", itemId);
}
}
} catch (exception) {
log.debug("Value:", "Item-Tolerance-Value Error:" + exception.message);
throw exception.message;
}
}
function beforeSubmit(scriptContext) {
try {
var IR_Record = scriptContext.newRecord;
var itemName = "";
if (IR_Record != null && IR_Record != '' && IR_Record != undefined) {
if (itemId != null && itemId != '' && itemId != undefined) {
log.debug("itemId:", itemId);
itemName = itemId[IR_ItemId]['ItemName']; // Getting Empty Values at this point, eventhough it is global variable with values
log.debug('Value', 'itemName : ' + itemName);
}
}
}catch(exception){
throw exception.message;
}
Example Values for the itemId object:
{
337: {
ItemName: "Apple",
},
359: {
ItemName: "Orange"
}
}
how to access the global Variable itemId get the value using it property "ItemName".
thanks in advance.
Share Improve this question asked Nov 27, 2016 at 17:29 Deepan MuruganDeepan Murugan 7813 gold badges22 silver badges42 bronze badges1 Answer
Reset to default 9You are mis-understanding the script execution lifecycle. On the server side the before load, before submit and after submit executions are all pletely separate. It looks like the only place you are loading itemId is via the call to getItemToleranceValues
in your beforeLoad
script but you are accessing the empty value in your beforeSubmit
script.
If there is some reason to load the value in your beforeLoad script you could cache the value in a session variable and then retrieve it in your beforeSubmit script. See Session.get/set in the N/runtime module