This topic contains 5 replies, has 0 voices, and was last updated by Olivier Gagnon NC 6 years ago.

  • Author
    Posts
  • #21274 Score: 0

    Brandon
    • Contributions: 0
    • Level 1

    So I am trying to have the items of a purchase order populate the items field on a new transfer order via a button on the PO. From there the user can make any changes they want to the record before submitting it and creating the TO. The main issue is I don't know how to populate a blank TO from script. I have it redirect there via a url string, but I'm sure there is a better way to do it.

    In summary.

    -User clicks "create TO" button on a PO

    -takes user to the "create TO" page where all the items (and some various info) is pre populated depending on the PO.

    -User edits the record and then submits it.

    ———-

    //create_to_button

    var newId ;

    var newType ;

    function beforeload(type)

    {

    if(nlapiGetContext().getRole() == '3')

    {

    if(type =='view' || type == 'edit')

    {

    newId = nlapiGetRecordId();

    newType = nlapiGetRecordType();

    if(newType == 'purchaseorder')

    {

    var strURL = "https://system.na2.netsuite.com/app/accounting/transactions/trnfrord.nl"

    var scriptbutton = 'window.open(' + String.fromCharCode(39) + strURL + String.fromCharCode(39) + ')' ;

    //nlapiLogExecution('DEBUG','<Before Load Script> type: '+type, 'URL: '+strURL + '&id=' + newId);

    form.addButton('custpage_createpo', 'Create TO', scriptbutton);

    }

    }

    }

    }

    function loadTO() //(request, response)

    {

    nlapiLogExecution('DEBUG','<Before Load Script> type: '+type, 'hello');

    nlapiLoadRecord(newType, newId);

    }

    ——–

    Any ideas or advice is appreciated.

    -Brandon
    This is a cached copy. Click here to see the original post.

  • #21275 Score: 0

    Olivier Gagnon NC
    • Contributions: 0
    • Level 1

    So you can do this the easy way, or the hard way.

    In the easy way, your button would create a new TO, add all the line items, SUBMIT IT, then redirect the user, in edit more (if desired), on that newly created record.

    In the hard way, your button redirects the user to a blank TO, perhaps passing in the URL a custom param. Then, a second script – a client side script running on init – kicks in, detects your custom param (it's cue to execute), and populates the needed items at that point. In order to do so, the client-side script probably needs to load the PO (or call a search) to see its items, to know what to add.

    So you see the difference between the two is that it is much, much easier to create the record then send a user to it, then to try to create it in an "unsaved" mode.

  • #21276 Score: 0

    Brandon
    • Contributions: 0
    • Level 1

    Thanks for the response. Creating a TO and saving it seems like the path i'd want to go. I seem to be running into two major issue.

    1. The script that is executed with the button will trigger on page load instead of when the button is clicked.

    form.addButton('custpage_purchaseorder', 'Create TO', createTO());

    2. When having the script run the TO creation and submitting it I keep getting "you must enter at least one transaction" error. Even though it seems like i've entered the item correctly.

    function createTO() //(request, response)

    {

    nlapiLogExecution('DEBUG','<Before Load Script> type: '+type, '22');

    // cc transferorder id is 128

    var record = nlapiCreateRecord ('transferorder');

    var recordID = record.getId();

    record.setFieldValue('customform',128);

    //subsidiaries CC bedford id is 2

    record.setFieldValue('subsidiary',2);

    //testing for location and transfer location, 144 & 145

    record.setFieldValue('location',144);

    record.setFieldValue('transferlocation',145);

    nlapiLogExecution('DEBUG','<Before Load Script> type: '+type, '23');

    // add new lines to a sublist

    nlapiSelectNewLineItem('item');

    // set the item and location values on the currently selected line

    nlapiSetCurrentLineItemValue('item', 'item', 10318);

    nlapiSetCurrentLineItemValue('item', 'location', 6);

    // commit the line to the database

    nlapiCommitLineItem('item');

    nlapiLogExecution('DEBUG','<Before Load Script> type: '+type, '24');

    var TOResult = nlapiSubmitRecord(record, false, true);

    var TOTranID= nlapiLookupField('transferorder', TOResult, 'tranid');

    nlapiLogExecution('DEBUG','<Before Load Script> type: '+type, '25');

    var TOResult = nlapiSubmitRecord(record, false, true);

    var poURL = nlapiResolveURL('RECORD', 'transferorder', TOResult);

    //window.location = poURL;

    nlapiSetRedirectURL('RECORD','transferorder', TOResult);

    nlapiLogExecution('DEBUG','<Before Load Script> type: '+type, 'URL: ' + '&id=');

    //nlapiLoadRecord(newType, newId);

    }

    Any idea why i'm getting these issues?

    Thanks.

  • #21277 Score: 0

    Olivier Gagnon NC
    • Contributions: 0
    • Level 1

    You have "var TOResult = nlapiSubmitRecord(record, false, true); " twice in there – the second one is probably erroring out, since you're submitting an – at this point – invalid record object.

  • #21278 Score: 0

    Brandon
    • Contributions: 0
    • Level 1

    Im not entirely sure why my script is erroring out, I add a new item, set it and commit it and im still getting the error of "You must enter at least one line item for this transaction."

    function OLDcreateTO() //(request, response) { for ( var i = 1; i < lines + 1 ; i++ ) { nlapiLogExecution('DEBUG','<Before Load Script> type: '+type,"line # " + i); arrayName[i] = PORecord.getLineItemValue('item', 'item', i ); nlapiLogExecution('DEBUG','<Before Load Script> type: '+type, arrayName[i]); } nlapiLogExecution('DEBUG','<Before Load Script> type: '+type, lines + ' lines'); var TOrecord = nlapiCreateRecord ('transferorder'); var TOrecordID = TOrecord.getId(); TOrecord.setFieldValue('customform',128); //subsidiaries CC bedford id is 2 TOrecord.setFieldValue('subsidiary',2); //testing for location and transfer location, 144 & 145 TOrecord.setFieldValue('location',144); TOrecord.setFieldValue('transferlocation',145); nlapiLogExecution('DEBUG','<Before Load Script> type: '+type, 'break 4'); // add new lines to a sublist nlapiSelectNewLineItem('item'); // set the item and location values on the currently selected line nlapiSetCurrentLineItemValue('item', 'item', arrayName[1]); nlapiSetCurrentLineItemValue('item', 'location', 6); // commit the line to the database nlapiCommitLineItem('item'); nlapiLogExecution('DEBUG','<Before Load Script> type: '+type, 'break 5'); var TOResult = nlapiSubmitRecord(TOrecord, true, true); var TOTranID= nlapiLookupField('transferorder', TOResult, 'tranid'); nlapiLogExecution('DEBUG','<Before Load Script> type: '+type, 'break 6'); var poURL = nlapiResolveURL('RECORD', 'transferorder', TOResult); nlapiSetRedirectURL('RECORD','transferorder', TOResult); }

  • #21279 Score: 0

    Olivier Gagnon NC
    • Contributions: 0
    • Level 1

    Sorry, I hadn't noticed before: when you are adding the line (nlapiSetCurrentLineItemValue) you are using the wrong API. You reverted to local-record coding, as opposed to object coding. You need to switch to TOrecord.setLineItemValue() kind of APIs

You must be logged in to reply to this topic.