Hi all, Is it possible to update a record using a ...
# suitescript
k
Hi all, Is it possible to update a record using a sql statement? something like "update employee set comments = 'SOME COMMENT' where entityid = '10800'"
a
no, suiteql only supports read operations, you can effectively do that using the record module, but without more details of your use-case I wouldn't really know what to recommend
for your example you could just do
Copy code
record.submitFields({
    type: record.Type.EMPLOYEE,
    id: 10800,
    values: {
        'comments': 'SOME COMMENT'
    }
});
k
Thanks Antohony, I explain what we are trying to do, in netsuite I have not found a way to link the item in an invoice with the discount item that corresponds natively, so we added a field to specify the uniquekey of the item that applies to the discount item level, however when we save the information to generate the invoice, it gives us the uniquekey that corresponds to the object of the sales order and not the invoice. So we were trying to update that field after creating the invoice, however with suitescript 1 it allows it, but with suitescript 2.1 it sends errors.
b
what did the code look like for both versions
k
Esta es con la version 2.1
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 *
 */

define(["N/record","N/log"],(record,log)=>{
    const afterSubmit = (context)=>{
        if (context.type === context.UserEventType.CREATE) {
            const customerRecord = context.newRecord;
            let order = nlapiLoadRecord('invoice',customerRecord.id,{recordmode:'dynamic'})
            let uniquekey = 0;
            order.lineitems.item.forEach((item,index)=>{
              if(item.itemtype == 'Discount'){
                order.selectLineItem('item',index+1)
                order.setCurrentLineItemValue('item','custcol_dpk_fk_item_discount',uniquekey);
                order.commitLineItem('item')
              }else{
                uniquekey = item.lineuniquekey;
              }
            });
            try {
              nlapiSubmitRecord(order,true);  
            } catch (error) {}
            
        }
    }
    return {afterSubmit};
  });
Y con la version 1.0
Copy code
var STOIC = STOIC || {};

STOIC.example = (function(){
    var exports = {};
    function aftersubmit(type){
      if(type=='create'){
        var order = nlapiLoadRecord('invoice',nlapiGetRecordId(),{recordmode:'dynamic'}), uniquekey='';
        for (var index = 1; index <= order.getLineItemCount('item'); index++) {  
          if(order.getLineItemValue('item','itemtype',index) == 'Discount'){
            order.selectLineItem('item',index);
            order.setCurrentLineItemValue('item','custcol_dpk_fk_item_discount',uniquekey);
            order.commitLineItem('item');
          }else{
            uniquekey = order.getLineItemValue('item','lineuniquekey',index);
          }
        }
        nlapiSubmitRecord(order,true)
      }
    }
    exports.aftersubmit = aftersubmit;
    return exports;
})();
a
let order = nlapiLoadRecord('invoice',customerRecord.id,{recordmode:'dynamic'})
this isn't valid SS 2.x you'd need
Copy code
let order = record.load({
    type: record.Type.INVOICE,
    id: customerRecord.id
    isDynamic: true
});
nlapiSubmitRecord also isn't SS 2.x and pretty sure all the line item references inside the forEach should be using options object parameter with key:value pairs, rather than an array of params
k
If an attempt was made to load the record in this way record.load({type:'invoice',id55519,isDynamictrue}), however it does not save the value after the record.save() although it does not send any error.
b
you make the general mistake of mixing suitescript 1 and 2
not an option available to you, you must only use suitescript 1 or 2
you have also changed how the looping mechanism works
Copy code
order.lineitems.item.forEach((item,index)=>{
you are trying to iterate over undefined keys
the script you are working with is short enough that you can translate one line at a time and then check that it does what you expect
k
Thanks battk and Anthony, we will debug the code and consider your comments.