define(["N/record", "N/log", "N/https", "N/search"...
# suitescript
s
define(["N/record", "N/log", "N/https", "N/search", "N/format", "N/http", "N/runtime", "N/ui/serverWidget", "N/redirect"], function(record, log, https, search, format, http, runtime, serverWidget, redirect) { function beforeSubmit(context) { setHsnCode(context); } function setHsnCode(context) { try { if (context.type !== context.UserEventType.DELETE) { var rec = context.newRecord; var lc = rec.getLineCount('item'); for (var i = 0; i < lc; i++) { var item = rec.getSublistValue({ sublistId: 'item', fieldId: 'item', line: i }); itemSavedSearch1(item, rec, i) } } } catch (e) { log.debug(e.name, e.message); } } function itemSavedSearch1(item, rec, i) { var itemSearchObj = search.create({ type: "item", filters: [ ["type", "anyof", "Subtotal", "Service", "OthCharge", "Payment", "NonInvtPart", "Markup", "Kit", "Group", "InvtPart", "Discount", "Description"], "AND", ["internalid", "anyof", item] ], columns: [ search.createColumn({ name: "internalid", label: "Internal ID" }), search.createColumn({ name: "custitem_in_hsn_code", label: "HSN or SAC Code" }), search.createColumn({ name: "custitem_in_nature", label: "Nature" }) ] }); var internalId; var hsnCode; var nature; var columns = itemSearchObj.columns; var searchResultCount = itemSearchObj.runPaged().count; log.debug("itemSearchObj result count", searchResultCount); itemSearchObj.run().each(function(result) { internalId = result.getValue(columns[0]); log.debug('internalId', internalId); hsnCode = result.getValue(columns[1]); log.debug('hsnCode', hsnCode); nature = result.getValue(columns[2]); log.debug('nature==>', nature); var k = 0; if (searchResultCount > 0) { rec.setSublistValue({ sublistId: 'item', fieldId: 'custcol_in_hsn_code', value: hsnCode, line: i, ignoreFieldChange: true }); rec.setSublistValue({ sublistId: 'item', fieldId: 'custcol_in_nature_of_item', value: nature, line: i, ignoreFieldChange: true }); return true; } }); } return { beforeSubmit: beforeSubmit }; });
p
Unless i'm missing something, you don't need any code. You can source the item field automatically by changing the settings on your custom line field?
šŸ‘ 1
s
If you really need code for whatever reason, you avoid governance issues by looping the sublist, create array of id's you need information for, and running one search. Modify you search filter to take
["internalid", "anyof", itemIdArray]
instead
šŸ‘ 1
b
i approve of both of these answers
šŸ’Æ 2
s
but this is a standard field i cant directly source
beside that I have to set the value also.
s
Then do my response, run one search and you no longer have governance problem.
b
keep in mind that the code you share looks like @PNJ’s option of using sourcing will work
the only restriction about using sourcing is that if you store value, it wont work on edits
if you dont store value, then it will change whenever that field on the item changes
s
@battk But I have to store the value and want to trigger on every event eit/create
@Sandii but through this can I store the value ? Can you provide a sample code?
s
Copy code
var lineCount = rec.getLineCount();
var itemIdArray = [];
for (var index = 0; index < lineCount; index++){
    itemIdArray.push(rec.getSublistValue('item', 'item', index));
}

var itemHSNCodeObj = createAndRunSearch(itemIdArray);
//loop sublist again and read values from object above
//set values if they are in the object

function createAndRunSearch(itemIdArray){
    var itemHSNCodeObj = {};
    search.create({
        type: 'item',
        filters: ['internalid', 'anyof', itemIdArray],
        columns: []
    }).run().each(function (result){
        //get item id, hsn, and nature
        //put them in the object like 
        //{
        //    itemid: {hsn: x, nature: y},
        //    itemid2: {hsn: x, nature: y}
        //}
    })
    return itemHSNCodeObj;
}
it's the same thing you are already doing, except you are running one search for all the information you need, instead of 500 searches
šŸ‘ 1
s
Thank you so much
@Sandii need your one more help
@Sandii I have pinged you personally please do check