Wednesday, October 31, 2018

Customize the vendor sublist of Lot Numbered Assembly Item and Serialized Assembly Item


This article shows how to customize the vendor sublist of Lot Numbered Assembly Item and Serialized Assembly Item.
Customer may require to add field(s) on the vendor sublist of the item record or hide a field.
To add field on the sublist, there are two requirements: a custom entity field and a script.
A custom record is needed to store the information that is being entered on the vendor sublist of the item record. The script set/sgets the value of the custom field from/to the sublist, and create the column on the sublist as well.

Create a custom record that will store the values of the custom fields. Please see the image below:

Create the custom record field:
     Customization > Lists, Records & Fields> Record Types > New 
     Label: (name of custom record)
     Id: _multi_vendor_fields
     Follow other settings shown in the image above.
   Save

Create fields in custom record:
    Click the New Field button to create custom field that will link the custom record and item record
    Label: Item
    ID: _item
    Type: List/Record
    List/Record: Item
    Store Value: checked
    Save
    Click the New Field button to create custom field that will link the custom record and vendor record
    Label: Vendor
    ID: _vendor
    Type: List/Record
    List/Record: Vendor
    Store Value: checked
    Save
    Click the New Field button to create custom field that will store the value of the custom field.
    Label: (name of the field)
    ID: _custom_field_1
    Type:  Free-Form Text (you can choose any type depending on the requirement)
    Store Value: checked
    Save

Create the user event script:
   Customization > Scripting > Scripts > New > User Event
   Enter information needed:
   Name: (name of your script)
   Scripts > Script File: (upload the script below)
           > Before Load Function: beforeLoad 
           > After Submit Function: afterSubmit
   Deployements > Applies To: (choose the item record you require customize the vendor sublist).
                  Deployed: Yes
                  Status: Released
   Save

var mvfid;

function beforeLoad(){
    
addField();
    
hideSubtlist();
    
disableField();
    }

function afterSubmit(){
    
var vendors = nlapiGetLineItemCount('itemvendor'); 
    var itemid = nlapiGetRecordId();

    for (var i = 0; vendors != null && i < vendors; i++) {
        var vendorid = nlapiGetLineItemValue('itemvendor','vendor',i+1);
        var customfield = nlapiGetLineItemValue('itemvendor','custpagecustom',i+1);    

        if (checkMVF(itemid, vendorid)){
            nlapiSubmitField('customrecord_multi_vendor_fields',mvfid,'custrecord_custom_field_1',customfield);
        }
        else{
            var rec = nlapiCreateRecord('customrecord_multi_vendor_fields');
            rec.setFieldValue('custrecord_item', itemid);
            rec.setFieldValue('custrecord_vendor', vendorid);
            rec.setFieldValue('custrecord_custom_field_1', customfield);
            nlapiSubmitRecord(rec);
        }
    }
}

function addField(){
    var sublist = form.getSubList('itemvendor');
    sublist.addField('custpagecustom','text','Custom Field');
    var vendors = nlapiGetLineItemCount('itemvendor');
    var itemid = nlapiGetRecordId();

    for (var i = 0; vendors != null && i < vendors; i++) {
        var vendorid = nlapiGetLineItemValue('itemvendor','vendor',i+1);

        if (checkMVF(itemid, vendorid)) {
            var customfield = nlapiLookupField('customrecord_multi_vendor_fields',mvfid,'custrecord_custom_field_1');
            nlapiSetLineItemValue('itemvendor','custpagecustom',i+1,customfield);
        }
    }
}

function hideSubtlist() { 
    form
.getSubList('itemvendor').getField('vendor').setDisplayType('hidden');
}

function disableField(){
    form.getSubList('itemvendor').getField('purchaseprice').setDisplayType('disabled');
    form.getSubList('itemvendor').getField('vendor').setDisplayType('disabled');
}

function checkMVF(item, vendor){
    var itemFilters = new Array();
    itemFilters[0] = new nlobjSearchFilter('custrecord_item', null, 'is', item);
    itemFilters[1] = new nlobjSearchFilter('custrecord_vendor', null, 'is', vendor);
    var itemColumns = new Array();
    itemColumns[0] = new nlobjSearchColumn('internalid', null, null);
    var searchresults = nlapiSearchRecord('customrecord_multi_vendor_fields', null, itemFilters, itemColumns);

    if (numRows(searchresults) > 0) {
        mvfid = searchresults[0].getValue('internalid');
        return true;
    }
    else{
        return false;
    }
}

function numRows(obj){
    var ctr = 0;

    for (var k in obj){
        if (obj.hasOwnProperty(k)){
            ctr++;
        }
    }
    return ctr;
}

No comments:

Post a Comment