Saturday, January 12, 2019

Get the Item's Record Type dynamically within a Transaction

Note that this can be used when an item used in a transaction was inactivated afterwards. Editing the transaction later on may throw an error. This solution validates the item sublist wiwithin a transaction to make sure these are all active. There are conditions in which you would need to check the item records in the item sublist but items within the sublist uses different record types. (e.g. Inventory item, Non-Inventory Item)

The sample script is a client side script triggered on save that would prohibit users to save a record with any inactive items on a transaction.

1. Create sample script.

function mySaveRecord(){

    for (var i = 1; i <= nlapiGetLineItemCount('item'); i++) {        // Loop through each of the items in the sublist
        
        var itype = nlapiGetLineItemValue('item', 'itemtype', i); // Get the item type
        var recordtype = '';
        
        switch (itype) {   // Compare item type to its record type counterpart
            case 'InvtPart':
                recordtype = 'inventoryitem';
                break;
            case 'NonInvtPart':
                recordtype = 'noninventoryitem';
                break;
            case 'Service':
                recordtype = 'serviceitem';
                break;
            case 'Assembly':
                recordtype = 'assemblyitem';
                break;
                
            case 'GiftCert':
                recordtype = 'giftcertificateitem';
                break;
            default:
        }
        
    var isinactive = nlapiLookupField(recordtype, nlapiGetLineItemValue('item', 'item', i), 'isinactive');
    var item_name = nlapiLookupField(recordtype, nlapiGetLineItemValue('item', 'item', i), 'itemid');
        
     if (isinactive == 'T') {
            alert('Unable to save record. Item ' + item_name + ' is inactive.');
            return false;
           }        
     }
    
    return true;
    
}

2. Save this as a JS file and deploy
3. Navigate to Customization > Scripting > Scripts > New
4. Upload Script File
5. Select Client
6. On Script record:
- Enter Name
- Save Record Function: mySaveRecord
7. Under Deployments subtab:
- Applies To: Sales Order
- Deployed: Yes
- Status: Release
8. Save.

DISCLAIMER: The sample code described herein is provided on an"as is" basis, without warranty of any kind, to the fullest extentpermitted by law. Oracle + NetSuite Inc. does not warrant or guarantee theindividual success developers may have in implementing the sample code on theirdevelopment platforms or in using their own Web server configurations.

Oracle +NetSuite Inc. does not warrant, guarantee or make any representations regardingthe use, results of use, accuracy, timeliness or completeness of any data orinformation relating to the sample code. Oracle + NetSuite Inc. disclaims allwarranties, express or implied, and in particular, disclaims all warranties ofmerchantability, fitness for a particular purpose, and warranties related tothe code, or any service or software related thereto.

Oracle + NetSuite Inc. shall notbe liable for any direct, indirect or consequential damages or costs of anytype arising out of any action taken by you or others related to the samplecode.

No comments:

Post a Comment