Komagain
Komagain

Reputation: 388

NetSuite SS 2.0: Salesorder Custom address

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);

Upvotes: 2

Views: 3294

Answers (2)

rncm
rncm

Reputation: 1

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);

Upvotes: 0

Kane Shaw
Kane Shaw

Reputation: 374

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

Upvotes: 4

Related Questions