This topic contains 3 replies, has 0 voices, and was last updated by ERPExperts 6 years, 8 months ago.

  • Author
    Posts
  • #18011

    ajramos

    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 This is a cached copy. Click here to see the original post.

  • #18012

    Olivier Gagnon NC

    “Putting the nlapiSetLineItemValue in the curlies completely ignores the first loop, thus, it doesn’t work.”

    I don’t understand what you mean here. Why would setting the line right after fetching the value “ignore the loop”?

    Not to muddy the waters, but in any case, you should never, ever call a search within a for loop that traverses all line items. That is a great way to bring your account’s performance to it’s knees. You should use a loop to gather all values in an array, and pass that to a SINGLE search that returns all values, then cycle through those results and set lines. You may need some extra command to map your search values back to the lines that consume them, but that extra processing is way, way, way faster than calling multiple searches.

  • #18013

    JohnCCole

    The variable declaration var text does not need to be declared in the for loop. Perhaps you missed typing your example but I don’t see you running your search. nlapiCreateSearch does not run the search it returns a search object.

    Olivier, is correct to point about running searches in a for loop. You would be better off building an array of the items you’re searching for and using those values in the criteria of one search it means iterating the lines twice but it’s far lost costly from a NetSuite perspective to run one search apposed to a search per line.

  • #18014

    ERPExperts

    function getX()

    {

    var text = ”;

    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++)

    {

    text = searchResult[0].getValue(columns[1]);

    //Issue – you are overwriting the value of text in every iteration, therefore ONLY THE LAST VALUE will remain.

    //Move the SLIV code here, and delete it from below.

    nlapiSetLineItemValue('line','joinedrecord_text',i ,text); //Add Me

    }

    //take search result value, enter in journal entry's custom column field text

    nlapiSetLineItemValue('line','joinedrecord_text',i ,text); //Delete Me

    }

    }

You must be logged in to reply to this topic.