1
votes

We have a custom script on our Sales Order that sets the Shipping Address to match the Location field in the header when the user submits the order. We're not using Cash Sales (long story) or POS. These are standard sales orders with Netsuite Basic. We have a decent amount of counter sales and our employees need the ability to quickly enter an order for a cash customer without having to add a shipping address every time. They choose "Customer Pick Up" from the Shipping Method dropdown, click Save, the script sees that option was chosen and it updates the address. This has been working great. We just installed Avatax, and it does NOT like this setup. It needs the Shipping Address selected on the order BEFORE we click Save on the order. I was hoping I could use a workflow that would trigger on field change/sourcing and run that script instead of waiting for the Save. But I can't figure out how to do it. I would be fine with a button or other mechanism as well - I just need a way to quickly set the Shipping Address to match the Location's address prior to saving the order. I started looking into converting this script to a Workflow Action Script so I can call it from a workflow, but I'm lost..

var salesOrderUE = {

    afterSubmit :function (type) {
        if (type == 'create' || type == 'edit') {
            var salesOrder = nlapiLoadRecord(nlapiGetRecordType(), nlapiGetRecordId(), { recordmode: 'dynamic'});
            var customerPickUp = salesOrder.getFieldValue('shipmethod')
            if (customerPickUp == '16853') {
                var location = salesOrder.getFieldValue('location');
                var locationRec = nlapiLoadRecord('location', location);
                salesOrder.setFieldValue('shipaddresslist', -2);
                var subrecord = salesOrder.createSubrecord('shippingaddress');
                subrecord.setFieldValue('country', locationRec.getFieldValue('country'));
                subrecord.setFieldValue('isresidential', 'F');
                subrecord.setFieldValue('attention', locationRec.getFieldValue('attention'));
                subrecord.setFieldValue('addressee', locationRec.getFieldValue('addressee'));
                subrecord.setFieldValue('addr1', locationRec.getFieldValue('addr1'));
                subrecord.setFieldValue('addr2', locationRec.getFieldValue('addr2'));
                subrecord.setFieldValue('city', locationRec.getFieldValue('city'));
                subrecord.setFieldValue('state', locationRec.getFieldValue('state'));
                subrecord.setFieldValue('zip', locationRec.getFieldValue('zip'));
                subrecord.commit();
                salesOrder.setFieldValue('shipaddress', locationRec.getFieldValue('addr1') +
                        '\n' + locationRec.getFieldValue('city') + ' ' + locationRec.getFieldValue('state') + ' ' + locationRec.getFieldValue('zip') + '\n'+
                        locationRec.getFieldValue('country'));

                nlapiSubmitRecord(salesOrder, true, true);
            }
        }
    }
}
1

1 Answers

3
votes

Huge shoutout to @battk on Slack. They walked me through the whole process of converting this to a Client script, and using the Field Changed Function to run it.

Here's the updated code: (I still need to edit the if statement to check for location == null)

function setCPUshipto(type, name, linenum) {
  if (name !== "shipmethod" || nlapiGetFieldValue("shipmethod") !== "16853")
  {
        return;
  }

  var locationId = nlapiGetFieldValue("location");
  var locationRec = nlapiLoadRecord("location", locationId);
  var subrecord = nlapiCreateSubrecord("shippingaddress") || nlapiEditSubrecord("shippingaddress")
  subrecord.setFieldValue("country", locationRec.getFieldValue("country"));
  subrecord.setFieldValue("isresidential", "F");
  subrecord.setFieldValue("attention", locationRec.getFieldValue("attention"));
  subrecord.setFieldValue("addressee", locationRec.getFieldValue("addressee"));
  subrecord.setFieldValue("addr1", locationRec.getFieldValue("addr1"));
  subrecord.setFieldValue("addr2", locationRec.getFieldValue("addr2"));
  subrecord.setFieldValue("city", locationRec.getFieldValue("city"));
  subrecord.setFieldValue("state", locationRec.getFieldValue("state"));
  subrecord.setFieldValue("zip", locationRec.getFieldValue("zip"));
  subrecord.commit();

}