2

We are currently importing Salesorder from 3rd party, The Salesorder creation is done fine. We are struggling with the shipping address, we need to create a custom shipping address at each new salesorder.

when we use the following code, only the shipzip is shown in the salesorder, do you see any reason for that?

              var shippingDetails = order[k].shipping_address;
          log.debug('shipping',shippingDetails);
          salesOrder.setValue('shipaddresslist', null);

          salesOrder.setValue('shipcountry', shippingDetails.country_iso_code.substring(0,2));
          log.debug('shipcountry',
                    salesOrder.getValue({
            fieldId: 'shipcountry'
          })
                   );
          salesOrder.setValue('shipisresidential', 'T');
          salesOrder.setValue('shipattention', 'Adresse de livraison');
          log.debug('shipattention',
                    salesOrder.getValue({
            fieldId: 'shipattention'
          })
                   );
          salesOrder.setValue('shipaddressee', shippingDetails.civility +' '+shippingDetails.firstname+' '+shippingDetails.lastname);
          log.debug('shipaddressee',
                    salesOrder.getValue({
            fieldId: 'shipaddressee'
          })
                   );
          salesOrder.setValue('shipaddrphone', shippingDetails.phone);
          salesOrder.setValue('shipaddr1', shippingDetails.street_1);
          salesOrder.setValue('shipaddr2', shippingDetails.street_2);
          salesOrder.setValue('shipcity', shippingDetails.city);
          //salesOrder.setValue('shipstate', 'CA');
          salesOrder.setValue('shipzip', shippingDetails.zip_code);           

as a workaround we try to use instead the below code, how can we have the carriage return?:

              salesOrder.setValue('shipaddress', shippingDetails.civility +' '+shippingDetails.firstname+' '+shippingDetails.lastname +'\n'+shippingDetails.street_1+'\n'+shippingDetails.zip_code+' '+shippingDetails.city);
Komagain
  • 378
  • 1
  • 10
  • the carriage return is '\n' – Komagain Jun 09 '20 at 08:40
  • Hi @Komagain, Have you been able to try my suggested answer? Did it work for you? – Kane Shaw Jun 12 '20 at 16:04
  • hi @kane Shaw, unfortunately the same behaviour, only one field is visible in the shipping zone (country, before it was zip code), but when I debug a getvalue, the correct value are brought. – Komagain Jun 27 '20 at 17:32
  • Hi @Komangain. Check my answer again, I just added a working example video at the bottom of it. The code I provided definitely works. Please check exactly how you are implementing it. – Kane Shaw Jun 28 '20 at 15:49
  • Hello @KaneShaw, Thank you for the video, it works in the debugger with your values, I am going to check it doesnt work with mines. – Komagain Jun 29 '20 at 14:47
  • I have seen where the error was, I was prefixing all the fieldId with ship: shipcountry...shipzip. Now based on your help, it is fixed. Thank you @KaneShaw – Komagain Jun 29 '20 at 15:10
  • Awesome! Glad I could help :) – Kane Shaw Jun 30 '20 at 10:29

2 Answers2

4

To correctly set a Billing or Shipping address on a Sales Order using SuiteScript 2.0 you must use the subrecord method.

Here is a full working example of creating a sales order with the specified Customer Internal ID and a single valid Item Internal ID

You can adapt it to suit your needs.

/**
*@NApiVersion 2.x
*/
require(['N/record'], function (record) {

    var VALID_CUSTOMER_ID = 14907; // A valid Customer Internal ID for testing
    var VALID_ITEM_ID = 7006; // A valid Item Internal ID for testing

    // Example address details
    var shippingDetails = { "country_iso_code": "USA", "civility": "civilty", "firstname": "firstname", "lastname": "lastname", "phone": "0123456789", "street_1": "Street 1", "street_2": "Street 2", "city": "Bell", "shipstate": "California", "zip_code": "90201" };
    var billingDetails = { "country_iso_code": "USA", "civility": "civilty", "firstname": "firstname", "lastname": "lastname", "phone": "0123456789", "street_1": "Street 1", "street_2": "Street 2", "city": "Bell", "shipstate": "California", "zip_code": "90201" };

    function CreateSalesOrder() {

        // Create our new sales order
        var salesOrder = record.create({
            type: record.Type.SALES_ORDER,
            isDynamic: true,
            defaultValues: { entity: VALID_CUSTOMER_ID }
        });

        // Add a line item (just for testing / example)
        salesOrder.selectNewLine({sublistId: 'item'});
        salesOrder.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'item',
            value: VALID_ITEM_ID
        });
        salesOrder.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'quantity',
            value: 1
        });
        salesOrder.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'amount',
            value: 1
        });
        salesOrder.commitLine({sublistId: 'item'});

        // Set our billing address
        salesOrder.setValue({
            fieldId: 'billaddresslist',
            value: null // Needed to override default address
        });
        var billaddrSubrecord = salesOrder.getSubrecord({fieldId: 'billingaddress'});
        billaddrSubrecord.setValue({
            fieldId: 'country',
            value: billingDetails.country_iso_code.substring(0,2)
        });
        billaddrSubrecord.setValue({
            fieldId: 'isresidential',
            value: 'T'
        });
        billaddrSubrecord.setValue({
            fieldId: 'attention',
            value: 'Adresse de livraison'
        });
        billaddrSubrecord.setValue({
            fieldId: 'addressee',
            value: billingDetails.civility +' '+billingDetails.firstname+' '+billingDetails.lastname
        });
        billaddrSubrecord.setValue({
            fieldId: 'addrphone',
            value: billingDetails.phone
        });
        billaddrSubrecord.setValue({
            fieldId: 'addr1',
            value: billingDetails.street_1
        });
        billaddrSubrecord.setValue({
            fieldId: 'addr2',
            value: billingDetails.street_2
        });
        billaddrSubrecord.setValue({
            fieldId: 'city',
            value: billingDetails.city
        });
        billaddrSubrecord.setValue({
            fieldId: 'state',
            value: billingDetails.state
        });
        billaddrSubrecord.setValue({
            fieldId: 'zip',
            value: billingDetails.zip_code
        });

        // Set our shipping address
        salesOrder.setValue({
            fieldId: 'shipaddresslist',
            value: null // Needed to override default address
        });
        var shipaddrSubrecord = salesOrder.getSubrecord({fieldId: 'shippingaddress'});
        shipaddrSubrecord.setValue({
            fieldId: 'country',
            value: shippingDetails.country_iso_code.substring(0,2)
        });
        shipaddrSubrecord.setValue({
            fieldId: 'isresidential',
            value: 'T'
        });
        shipaddrSubrecord.setValue({
            fieldId: 'attention',
            value: 'Adresse de livraison'
        });
        shipaddrSubrecord.setValue({
            fieldId: 'addressee',
            value: shippingDetails.civility +' '+shippingDetails.firstname+' '+shippingDetails.lastname
        });
        shipaddrSubrecord.setValue({
            fieldId: 'addrphone',
            value: shippingDetails.phone
        });
        shipaddrSubrecord.setValue({
            fieldId: 'addr1',
            value: shippingDetails.street_1
        });
        shipaddrSubrecord.setValue({
            fieldId: 'addr2',
            value: shippingDetails.street_2
        });
        shipaddrSubrecord.setValue({
            fieldId: 'city',
            value: shippingDetails.city
        });
        shipaddrSubrecord.setValue({
            fieldId: 'state',
            value: shippingDetails.state
        });
        shipaddrSubrecord.setValue({
            fieldId: 'zip',
            value: shippingDetails.zip_code
        });

        // Save our new sales order
        salesOrder.save({ignoreMandatoryFields: true});
    }

    CreateSalesOrder();
});

Working Example Video

Animated GIF Video showing code running in Debugger and created Sales Order

Kane Shaw
  • 314
  • 1
  • 8
  • I just tested creating an SO in dynamic mode and I didn't need to set the null values before setting the shipping and billing address fields; are you sure that's a required step? The key thing is to set the country field first from what I can see. – csilk Aug 06 '21 at 00:21
  • @csilk I haven't revisited this in quite a while (originally posted in Jun 2020,) it's possible that one of NetSuite's updates have removed this requirement. – Kane Shaw Aug 07 '21 at 12:22
  • Sales Order is fine, but in return authorization, you can set a billing address like this, but not a shipping address, as NetSuite has not exposed anything like a shippingAddress or a ny Address field representing a shipping address. Am I right? What is the workaround for return authorization? – Samar Rizvi Jul 04 '22 at 20:04
0

After an hour of fighting here's a 2021 update. When in doubt, RTM: https://6396621-sb1.app.netsuite.com/app/help/helpcenter.nl?fid=section_4704101831.html

//Create the custom shipping address
let shipaddr = so.getSubrecord('shippingaddress');
shipaddr.setValue('country', order.shippingAddress.country.id);
shipaddr.setValue('phone', order.deliveryphone);
shipaddr.setValue('isresidential', 'T');
shipaddr.setValue('attention', '');
shipaddr.setValue('addressee', order.shippingAddress.addressee);
shipaddr.setValue('addr1', order.shippingAddress.address1);
shipaddr.setValue('city', order.shippingAddress.city);
shipaddr.setValue('state', order.shippingAddress.state);
shipaddr.setValue('zip', order.shippingAddress.postcode);
log.debug('Shipping',shipaddr);

//Create the custom billing address
let billaddr = so.getSubrecord('billingaddress');
billaddr.setValue('country', order.billingAddress.country.id);
billaddr.setValue('phone', order.billingAddress.phone);
billaddr.setValue('isresidential', 'T');
billaddr.setValue('attention', '');
billaddr.setValue('addressee', order.billingAddress.addressee);
billaddr.setValue('addr1', order.billingAddress.address1);
billaddr.setValue('city', order.billingAddress.city);
billaddr.setValue('state', order.billingAddress.state);
billaddr.setValue('zip', order.billingAddress.postcode);
log.debug('Billing',billaddr);
rncm
  • 1
  • Hey @rncm! What exactly is functionally different in your answer compared to mine? I had a look over it but nothing stands out immediately as being different. – Kane Shaw Oct 09 '21 at 13:30