0

Continuation of my previous question: Suitescript Code stops for no apparent Reason

The child record (sublist items) are not being saved/created and linked to the parent record. The code is below.

try {
    var vendorid = nlapiGetRecordId();              console.dir('Vendor ID: #'+vendorid);
    var vprRecordID = create_VPR_record(vendorid);  console.dir('Record Created: #'+vprRecordID);
    var newVprRecord = nlapiLoadRecord('customrecordvendorpricereview', vprRecordID);
    var vendorItems = getVendorItems(vendorid);
    var numberItems = vendorItems.length;
    for (var i=1; i<numberItems; i++ ) {
        newVprRecord.selectNewLineItem(SUBLIST_Items);
        var itemID = parseInt(vendorItems[i].getId());
        var iType = itemType(itemID);
        newVprRecord.setCurrentLineItemValue(SUBLIST_Items,'custrecordvpri_item',itemID);
        var avgCost  = Round(nlapiLookupField(iType,itemID,'averagecost'),4);
        var stdCost  = Round(nlapiLookupField(iType,itemID,'custitem_costrepl'),4);
        var lastCost = Round(nlapiLookupField(iType,itemID,'lastpurchaseprice'),4);
        if (isNaN(avgCost))  { avgCost  = '' };
        if (isNaN(stdCost))  { stdCost  = '' };
        if (isNaN(lastCost)) { lastCost = '' };
        newVprRecord.setCurrentLineItemValue(SUBLIST_Items,'custrecordvpri_costavg',  avgCost );
        newVprRecord.setCurrentLineItemValue(SUBLIST_Items,'custrecordvpri_costlast', lastCost );
        newVprRecord.setCurrentLineItemValue(SUBLIST_Items,'custrecordvpri_coststd',  stdCost );
        newVprRecord.setCurrentLineItemValue(SUBLIST_Items,'custrecordvpri_vendorcurrency',vendorItems[i].getValue('vendorpricecurrency'));
        newVprRecord.setCurrentLineItemValue(SUBLIST_Items,'custrecordvpri_currentprice',vendorItems[i].getValue('vendorcost'));
        newVprRecord.commitLineItem(SUBLIST_Items);
        log('Commit Line #'+i+'  Item ID: '+itemID+'  '+vendorItems[i].getValue('itemid'));
    }
    var linecount = newVprRecord.getLineItemCount(SUBLIST_Items);   console.dir('Line Count #'+linecount );
    nlapiSubmitRecord(newVprRecord);  console.dir('Resubmitting Record #'+vprRecordID+'\n\n'+newVprRecord );
    var url = nlapiResolveURL('record', 'customrecordvendorpricereview', vprRecordID, 'edit');
    window.open(url, "New Vendor Price Review");
} catch (err) { 
    logError(err,'Create_VPR_Main')
}

The field linking the child records to the main record is custrecordvpri_header, and entered as the sublist name recmachcustrecordvpri_header.

linecount after the for loop is showing -1

Community
  • 1
  • 1
Steve Reeder
  • 980
  • 16
  • 42
  • I know my code is a little untidy, but I'll fix that once I get the crux of it functional. – Steve Reeder Dec 30 '15 at 11:14
  • There are also no "sub" records being created in the child record. I don't currently have nlapiAttachRecord in use, and it has a 10 point cost, so I'm hoping there's another way to link them. – Steve Reeder Dec 30 '15 at 11:35
  • You mean you have applied a custom sublist to standard vendor record and using sublist APIs you are unable to insert its records on vendor? – prasun Dec 30 '15 at 15:08
  • The "parent" record is `customrecordvendorpricereview` and the parent of this record is the vendor, so the lines are not directly linked to the standard vendor record – Steve Reeder Dec 31 '15 at 01:19
  • on which record type are you running this client script? It looks like you are setting sublist on current client record opened on browser and you are doing submit on a `nlobjRecord`. – prasun Dec 31 '15 at 05:39
  • The script is running on-click of a custom button (user-event) which is triggering the client script. Browser has not graduated to the new record/sublist at this point. I'm hoping the script can create the parent, and the subliist lines prior to opening the whole record/sublist in edit mode in a new window. – Steve Reeder Dec 31 '15 at 05:40
  • The initial user event is triggered from the Vendor screen. The client script is currently running on the vendor record too, but I think that might need to be the parent record `customrecordvendorpricereview`, but if I do that, it doesn't get the vendor id – Steve Reeder Dec 31 '15 at 05:46
  • please check the solution, let me know if it worked – prasun Dec 31 '15 at 06:07

1 Answers1

1

It looks like you are setting sublist on current client record opened on browser and you are doing submit on a nlobjRecord

//This is the nlobjRecord

var newVprRecord = nlapiLoadRecord('customrecordvendorpricereview', vprRecordID);

//To set line items on this record use :
newVprRecord.selectNewLineItem(SUBLIST_Items);
...
//to set line item value use
newVprRecord.setCurrentLineItemValue(SUBLIST_Items,'custrecordvpri_costavg',  avgCost);
...
//to commit use 
newVprRecord.commitLineItem(SUBLIST_Items);
...
//finally after line items loop is done
nlapiSubmitRecord(newVprRecord,true);
prasun
  • 7,073
  • 9
  • 41
  • 59
  • OK it's cycling through the lines OK, and linecount is now correct (8 in this vendor's case). But when it gets to the submit record, it's generating an error: `Invalid custrecordvpri_item reference key 16325`. `custrecordvpri_item` is sublist field ID for the item which is a list/record type of `item`. I've tried to insert the itemid, which gives the same error. – Steve Reeder Dec 31 '15 at 06:54
  • I'll amend the code in the main question so it contains the current(new) code. – Steve Reeder Dec 31 '15 at 06:55
  • I think I might have found the reason for the error... the item in question is marked _inactive_ so I will add the inactive filter to the item search and re-test. – Steve Reeder Dec 31 '15 at 07:00
  • is this the correct filter to remove inactive items? `filters[0] = new nlobjSearchFilter('isinactive', null, 'is', 'F' )` because I'm still getting the inactive items in the result – Steve Reeder Dec 31 '15 at 07:11
  • 1
    must've typed it wrong somehow..... It is all working PERFECTLY!!!!!!!! Thank you so much for your help, just wish I could tick your answers a dozen times!!!!! – Steve Reeder Dec 31 '15 at 07:20