VicDid
VicDid

Reputation: 175

Automatically Filtering Items By Vendor During Creation of a Record

I have a set of scripts that I'm using that interact with each other. I use a client, user event and suitelet script to create a button that, when pressed, opens a popup with a list of items filtered by vendor.

It works fine when I'm in edit however when I use it while creating a record problems arise. Since the record to be created has no vendor or id I can't retrieve an item by vendor. What I'm trying to do is to have the Suitelet retrieve the info from the vendor field that is entered prior to it being saved. Therefore I can filter all the items by vendor and add the necessary items in one go. Is this possible? Am I able to access the info before it is submitted.

Below are the Client and Suitelet. The User Event is just a call to the suitelet so for the sake of brevity I left it out.

Client Script

function addItemButtonCallback(data){
    nlapiSelectNewLineItem('item');
    nlapiSetCurrentLineItemValue('item', 'item', data);
    nlapiCommitLineItem('inventoryitem');
}

function addItemButton() {
    var id = nlapiGetFieldValue('id');
    if (id != "") {
        var url = nlapiResolveURL('SUITELET', 'customscript_val', 'customdeploy1') + '&poId='+id;
        window.open(url, '_blank', 'width=500,height=500'); 
    } 
}

Suitelet

function suitelet(request, response){
    if(request.getMethod() == 'GET') {
        var form = nlapiCreateForm('Add Item');
        form.addSubmitButton('Submit');

        var itemfield = form.addField('custpage_val', 'select', 'Item');
        var id = request.getParameter('id');

        var rec = nlapiLoadRecord('purchaseorder', id);
        var vend = rec.getFieldValue('entity');
        var search = nlapiSearchRecord(...search parameters...);

        for (result in search){
          if (search[result].getValue('vendor') == vend){
            itemfield.addSelectOption(search[result].id, nlapiLookupField('inventoryitem', search[result].id, 'itemid'));
          }
        }
        response.writePage(form);
    } else {
        var data = request.getParameter('custpage_item');
        response.write('<html><body><script>window.opener.addItemButtonCallback("'+data+'"); window.close();</script></body></html>');
    }
}

Upvotes: 1

Views: 511

Answers (1)

Adolfo Garza
Adolfo Garza

Reputation: 3029

Use nlapiGetFieldValue('entity') on the clientscript and pass it to the Suitelet using a query parameter just like you are doing with poId (if you do this you might not even need poId after all + no need to load the record on the suitelet).

Also, you might want to optimize your code by running one search passing an array of itemids instead of calling nlapiLookupField for each item.

You might need to modify your beforeLoad so the entity is inserted dynamically when the button is pressed (I cant remember if clientscript button does this) . Something like this:

var suiteletURL = nlapiResolveURL('SUITELET', 'customscript_val', 'customdeploy1');
var script = "var entity = nlapiGetFieldValue('entity'); var url = '" + suiteletURL + "'&entityId=' + entity;window.open(url, '_blank', 'width=500,height=500')";
var button = form.addButton('custpage_addItemButton', 'Add Item', script);

Upvotes: 3

Related Questions