Hi !! We are trying to Copy sales orders from a su...
# suitescript
j
Hi !! We are trying to Copy sales orders from a subsidiary to another. We are using record.copy API of the N/record module. but when we try to set the value of the subsidiary during copy we are getting the error "An unexpected SuiteScript error has occurred" . We already tried doing it with isDynamic true and false ``````
here's the snippet
Copy code
var objNewSo = record.copy({
    type: record.Type.SALES_ORDER,
    id: objData.salesOrder,
    isDynamic: true
   // isDynamic: false
})
var intEntity = objNewSo.getValue({fieldId: 'entity'})
log.debug('intEntity', intEntity)
var currentSubsidiary = objNewSo.getValue({fieldId: 'subsidiary'})
log.debug('currentSubsidiary', currentSubsidiary)
// objNewSo.setValue({fieldId: 'entity', value: ''})
objNewSo.setValue({fieldId: 'entity', value: intEntity})

var itemLineCount = objNewSo.getLineCount({sublistId: 'item'})
var  newSubsidiary = mappingSubsidiary[currentSubsidiary]
log.debug('newSubsidiary', newSubsidiary)
objNewSo.setValue({fieldId: 'subsidiary', value: newSubsidiary})
log.debug(' mappingSubsidiary[currentSubsidiary]', mappingSubsidiary[currentSubsidiary])

var currentLocation = objNewSo.getValue({fieldId: 'location'})
log.debug('currentLocation', currentLocation)
objNewSo.setValue({fieldId: 'location', value: locationMapping[currentLocation]})
log.debug(' locationMapping[currentLocation]', locationMapping[currentLocation])

var poNUmber = objNewSo.getValue({fieldId: 'otherrefnum'})
log.debug('poNUmber', poNUmber)
objNewSo.setValue({fieldId: 'otherrefnum', value: poNUmber + ' new'})
//get item line count

for (var i = 0; i < itemLineCount; i++) {
    var currentLine = objNewSo.SelectLine({sublistId: 'item', line: i})
    currentLine.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'inventorylocation',
        value: locationMapping[currentLocation]
    })
    currentLine.commitLine({sublistId: 'item'})
    // objNewSo.setSublistValue({
    //     sublistId: 'item',
    //     fieldId: 'inventorylocation',
    //     line: i,
    //     value: locationMapping[currentLocation]
    // })
}

var newSOId = objNewSo.save({
    enableSourcing: false,
    ignoreMandatoryFields: true
})
log.debug('newSOId', newSOId)
t
One thing I have learned from a great man is to do in UI. If it works in UI, it can be replicated in Script. Not everything, though. I tried doing that in UI, but the Subsidiary becomes a static field for me; it’s not changeable. That means copy might be limited to the same subsidiary only. One thing you can still do is get all data from One Sales order. Instead of copying, create another one and source the information.
j
Yeah Ive Tried that as well and you are correct. in the standard UI option you will not be able to change the subsidiary but with the help of the chrome extension "Advance Field help" we are able to change the value and save it from there. And that gave us a little bit of hope 🙂
n
You changed it using the plugin and the change you made definitely "stuck"? 🧐
j
So Subsidiary on a transaction sources from the entity of the transaction. So I'm assuming either you are using multi-subsidiary customers or that you have separate customer records in each subsidiary representing the same customer. If the latter, when you set the new customer record on the sales order the Subsidiary should auto-source - especially in dynamic mode. Just know that changing the entity of the sales order causes a re-sourcing of all customer-related things like item pricing and terms and addresses and such from the new customer. If the former, as Vikas said, if you can do it natively in the UI you should be able to do it via script. If not, then probably not. It may be that you have some segmentation conflict with the new subsidiary (i.e. a department, class, location, item, account that is not supported in the new subsidiary). What may be a better option is rather than using copy, just create a new order and use the original as the source for setting fields on the new. newRec.setValue({ fieldId: 'location', value: oldRec.getValue({'location'}) }). Just an idea.
n
@NElliott I'm working with ST Dev on this one. We have multi-subsidiary for the customer. Changing the subsidiary in UI definitely works. What is interesting is that the script fails at the time we update the object not when we do the record.save().
@Jordan Patterson the segmentation conflict is a good prompt so although we aren't sourcing new customer values we might be having restrictions on other fields. Again the issue that it happens before we save the object.