Back to this issue.... I am trying to use a restl...
# suitescript
s
Back to this issue.... I am trying to use a restlet to create a Canadian sales order.  For Canada, tax codes are used at the item level and I need to be able to change the default tax code (selected by the tax schedule and based on the ship to province) to another that I have for that same province.  (I cannot use the default as it is different for our retail and wholesale customers).  But when I use... rec.setCurrentSublistValue({                 sublistId: 'item',                 fieldId: 'taxCode',                 value: '1444'             }); ...in the line items attached to the sales order at create in my restlet, it either is ignored or overwritten by the default tax code. I can change it manually in the sales order after creation but no matter what I have tried, I cannot get it to work in the restlet code. Last night, I thought I had a resolution by removing the pst exempt from the customer but I found that it was then using the normal tax with PST and that I still could not overwrite it in script. Strangely, I can get it to work with a SOAP call but not in a restlet. I am thinking that maybe the canadian tax province is determined on submit and that it overwrites what was set in the restlet script but that the SOAP call must do something different. Unfortunately I cannot use SOAP for this order creation. Any ideas?
p
your restlet is creating the record in dynamic mode, right? have you tried standard mode?
Because in standard mode the order you set things doesn’t matter as much. In dynamic mode you have to care about order of fields being set and dependencies
For example setting shipping address and nexus before tax codes.
s
As a general rule I always use standard mode unless I must use dynamic mode. That seems to create less headache all 'round
s
Perhaps, save the record then once you have the IDs fire a submitfields to update the tax code on the line items, this would get a round the tax code being set on submit maybe?
s
I have read that submitfields does not work with sublists like the item list. For Canadian taxes, the tax code is kept on the line item
I have tried, after the SO create, to reload the record in the same script and set the taxcode on line items but that also does not seem to work.
s
I think you are right that Submitfields will not work on sublists. What about an unscheduled scheduled script that your User event script calls thats comes along after and fixes the tax code with ignore fieldchanged set?.
s
I have this that runs after the SO create...
// ****************************************
        //* change tax code on line items after order create */         function changeTaxCode(SOID) {                     var Rec = record.load({                 "type": record.Type.SALES_ORDER,                 "id": SOID,                 isDynamic: false             });             var Count = Rec.getLineCount("item");                       for (var i = 0; i < Count; i++) {            Rec.setSublistValue("item", "taxcode", i, 1387);             Rec.save;     }         }
s
are you missing a commit ?
s
...and it STILL does not change the tax code.
do you do a commit in standard? I am not familiar with working in standard
s
I am not sure off the top of my head but I think so
nope no commit in standard
p
Rec.save should be Rec.save()
s
I might try this is dynamic mode, 1. Record.selectLine(options) 2. Record.setCurrentSublistValue(options) 3. Record.commitLine(options)
@PabloZ Good call
try that first
also that Capital R on REC is making my brain hurt!
Rec
s
Thanks... added that but now get... "error": {         "code": "RCRD_HAS_BEEN_CHANGED",         "message": "{\"type\":\"error.SuiteScriptError\",\"name\":\"RCRD_HAS_BEEN_CHANGED\",\"message\":\"Record has been changed\
does that mean that something is modifying the record while I have it open?
s
Its because you are modify that same record in the aftersubmit event. I bet when you get that error. if you reopen the record in a new tab its actually updated correctly
you are saving, editing and saving all in the same event.
Maybe after your Rec.save() add a redirect to the same record to force it to reload?
or I might go back to looking at calling an unscheduled script to do the update?
s
You are correct that it DID in fact change the tax code... yay! Now... I currently have the record open in dynamic when I create the sales order. After so.save(), I call the function above passing the returned internal id that I then use to record.load the SO for the change. is that necessary or is the record still open after the so.save() ?
s
aftersubmit you cannot modify values so you wold need to load it. But that is causing the Record Changed error
s
So I should just try/catch my save after the standard record change and ignore that message?
...or is there a better way to prevent that error?
s
try it! I dont think it will work tough.
I think it is the outter function Aftersubmit that is failing. But that could be because you are trigger and after submit with the aftersubmit on the same record
s
Ignore that last... found that I had my save inside the for/next that was going through each item in the sublist to change the code... moved that and it completes without the error (and all items are updated instead of only the first)
I think that fixes everything! Thank you to all of you BRILLIANT people!
s
Nice!!!
Glad it works