Hi everyone,
I'm trying to figure out how to take a value of a variable outside of a 'for' loop when it's declared inside of the loop. I know this could be easily done by doing everything inside the curlies as the value of a variable does not exist outside of it, but it can't be avoided when trying to get a joined record field in a transaction column, thus, a nested loop.
Say for example you have a transaction record, with a list/record column field 'joinedrecord_name' where a joined record is selected (id shows in the field). Then, you have another column textfield in the transaction column called "joinedrecord_text" where I want to populate the value from one of joinedrecord_name's item list field. Let's call this field "text_field_in_joined_record".
I would think the script that runs on user event Before Submit on the transaction (journal entry) will have two 'for' loops- one to focus on the line item in the transaction record, the other to track the joined record's text field value by search:
function getX(){
var joinedrecord_text = '';
var lineCount = nlapiGetLineItemCount('line');
for (var i = 1; i <= lineCount;i++){
var joined_record = nlapiGetLineItemValue('line','joinedrecord_name',i );
//search for joined record
var filters = new Array();
filters[0] = new nlobjSearchFilter('joined_record_id', null, 'is', joined_record);
var columns = new Array();
columns[0] = new nlobjSearchColumn('joined_record_id');
columns[1] = new nlobjSearchColumn('text_field_in_joined_record');
var searchResult = nlapiCreateSearch('joinedrecord_record_type', filters, columns);
for (var a = 0; a < searchResult.length; a++) {
var text = searchResult[0].getValue(columns[1]);
}
//take search result value, enter in journal entry's custom column field text
nlapiSetLineItemValue('line','joinedrecord_text',i ,text);
}
}
So the problem here is- I need to get the 'text' value from outside the search's 'for' loop. For now, running this script returns null. Putting the nlapiSetLineItemValue in the curlies completely ignores the first loop, thus, it doesn't work. I can't use nlapiLookupField in this case, as it appears it's not applicable to the specific fields mentioned here for some reason. Declaring "text = '';" on top to make it global doesn't help either.
I'm really thinking I'm just missing a fundamental concept in scripting here being new to SuiteScripts.... hoping I've explained it clearly. Thanks in advance for all your help!
Cheers!