Hi Question, I selectLine, then try to write to it...
# suitescript
m
Hi Question, I selectLine, then try to write to it, no error's or anything. I am commit, and saving.
b
share the code
k
Yes
We need to know whether this is what you using while loading record/create in dynamic or standard.
m
dynamic
define(['N/search', 'N/record', 'N/log' , 'N/runtime', 'N/format'],
    function(search, record, log, runtime, format) {   function afterSubmit(scriptContext) {     var scriptType = scriptContext.type;     var newInvoice = scriptContext.newRecord;     var exContext = runtime.executionContext;     var recordType = newInvoice.type;     var inv_Id = newInvoice.id;          try{       if((exContext == runtime.ContextType.WEBSERVICES || exContext == runtime.ContextType.USER_INTERFACE) && (scriptType == 'create' || scriptType == 'edit') && (recordType == 'invoice')){         log.debug('BS recordType is '+recordType, 'inv_Id is '+inv_Id);         var newInvoice = record.load({             type: record.Type.INVOICE,                 id: inv_Id,               isDynamic: true,         });         var itemLines = newInvoice.getLineCount({           sublistId: 'item'         });         log.debug('itemLines is', itemLines);                  if(itemLines > 0){           for( var itemLinesLen = 0 ; itemLinesLen < itemLines ; itemLinesLen++ ){             var curItemName = newInvoice.getSublistText({               sublistId: 'item',               fieldId: 'item',               line: itemLinesLen             });             log.debug('curItemName is', curItemName);             var item = newInvoice.getSublistValue({                sublistId: 'item',                fieldId: 'item',               line: itemLinesLen                  });             var rate = newInvoice.getSublistValue({                sublistId: 'item',                fieldId: 'rate',               line: itemLinesLen                  });             var quantity = newInvoice.getSublistValue({                sublistId: 'item',                fieldId: 'quantity',               line: itemLinesLen                  });               if(itemLines == 1){                 var previousItemDescription = newInvoice.getSublistValue({                   sublistId: 'item',                   fieldId: 'description',                   line: itemLinesLen                 });                 log.debug('previousItemDescription is', previousItemDescription);                 var lineNum = newInvoice.selectNewLine({                   sublistId: 'item'                 });                 newInvoice.setCurrentSublistValue({                   sublistId: 'item',                   fieldId: 'item',                   value: item                 });                 newInvoice.setCurrentSublistValue({                   sublistId: 'item',                   fieldId: 'pricelist',                   value: -1                 });                 newInvoice.setCurrentSublistValue({                   sublistId: 'item',                   fieldId: 'rate',                   value: rate*-1                 });                 newInvoice.setCurrentSublistValue({                   sublistId: 'item',                   fieldId: 'quantity',                   value: quantity                 });                 newInvoice.setCurrentSublistValue({                   sublistId: 'item',                   fieldId: 'description',                   value: 'Discount-' + curItemName                 });                 newInvoice.commitLine({                 sublistId: 'item'                 });               }            if(item == 57375){                 log.debug('item is ', item);                 var lineNum1 = newInvoice.selectLine({                   sublistId: 'item',                   line: 2                 });               newInvoice.setCurrentSublistText({                   sublistId: 'item',                   fieldId: 'description',                   value: 'Discount-' + curItemName                 });                 newInvoice.commitLine({                 sublistId: 'item'                 });             }               newInvoice.save();             }           }         }       }      catch(afterSubmitError) {       log.error('afterSubmitError is ', afterSubmitError.message);     }   }   return {     afterSubmit: afterSubmit   }; });
just the bottom if statement if(item == 57375)
s
why are you using setText on a field that is just text? Have you tried using setCurrentSublistValue?
✔️ 1
It's also had to know what else is going on here, it seems strange that you hardcoded to
line: 2
inside a conditional checking what the itemId is
m
oh set value
damnit
oh now im getting after submit error, record has been changed.
Even though I am saving.
Line 2 is the line of the item.
b
record has been changed means that the record was changed between when you loaded it and when you saved it
its common in accounts in a downward performance spiral that have asynchronous code that runs on the current record
m
first time i ran into this issue
the script was running fine before
but with the same logic
Oh i see.
How to work around?
Do I have to commit/save for the description side?
@battk
b
rewrite the logic for beforeSubmit instead
m
oh jeez
it worked fine on aftersubmit
before i added this one part
b
its what should be done in the first place
m
record isnt dynamic on beforesubmit?
b
any logic that resaves the record in afterSubmit is the cause of performance problems
m
oh because of double save
this is a mes
s
isnt standard just the old way of doing it?
and with scripts like mine where i have to continously change the logic, doesnt aftersubmit/dynamic record, make sense?
from the code you shared, none of it uses dynamic mode's sourcing