This topic contains 1 reply, has 0 voices, and was last updated by JacksonP 7 years, 9 months ago.

  • Author
    Posts
  • #1272

    JacksonP

    So I’m having issues when I try to edit the shipping address on a Purchase Order, created from a Sales Order. I am getting the shipping address values from the Sales Order, and checking the length on each one of the fields. If they are too long I edit them. The problem is, when I try to commit / save these new values on the Purchase order, I get an Unexpected Error. This script is running on a user event beforeSubmit, and I’ve also tried afterSubmit. Has anyone tried to edit a shipping address on a PO created from an SO? Here is my code:

    Code:
    /**
    * @NApiVersion 2.x
    * @NScriptType UserEventScript
    * @NModuleScope Public
    */
    define([‘N/record’, ‘N/search’],
    /**
    * @param {record} record
    * @param {search} search
    */
    function(record, search) {
    function beforeSubmit(scriptContext) {
    if(scriptContext.type != ‘create’){
    return;
    }
    var poRecord = scriptContext.newRecord;

    // var poRecord = record.load({
    // type: record.Type.PURCHASE_ORDER,
    // id: 27180627
    // });

    var form = poRecord.getValue({
    fieldId: ‘customform’
    });

    if(form != 145){
    return;
    }

    var createdFrom = poRecord.getValue({
    fieldId: ‘createdfrom’
    });

    if(!createdFrom){
    return;
    }

    var salesOrderSearchObj = search.create({
    type: “salesorder”,
    filters: [
    [“type”,”anyof”,”SalesOrd”],
    “AND”,
    [“internalidnumber”,”equalto”, createdFrom]
    ],
    columns: [
    “shipaddressee”,
    “shipaddress1”,
    “shipaddress2”,
    “shipcity”,
    “shipstate”,
    “shipzip”
    ]
    });

    var results = salesOrderSearchObj.run().getRange(0, 1);
    log.debug(‘results’, results);
    if(!results.length){
    return;
    }

    // Address Name (N102) โ€“ 1/60
    // Address1 (N301) โ€“ 1/55
    // Address2 (N302) โ€“ 1/55
    // City (N401) โ€“ 2/30
    // State (N402) โ€“ 2/2
    // Postal Code (N403) โ€“ 3/15
    // Country (N404) โ€“ 2/3

    var addressName = results[0].getValue( salesOrderSearchObj.columns[0] );
    var subaddressName = addressName.substring(0, 60);

    var address1 = results[0].getValue( salesOrderSearchObj.columns[1] );
    var subaddress1 = address1.substring(0, 55);

    var address2 = results[0].getValue( salesOrderSearchObj.columns[2] );
    var subaddress2 = address2.substring(0, 55);

    var city = results[0].getValue( salesOrderSearchObj.columns[3] );
    var subcity = city.substring(0, 30);

    var state = results[0].getValue( salesOrderSearchObj.columns[4] );
    var substate = state.substring(0, 2);

    var postalCode = results[0].getValue( salesOrderSearchObj.columns[5] );
    var subpostalCode = postalCode.substring(0, 15);

    log.debug(‘gettingFields: ‘, ‘AddressName: ‘ + subaddressName + ‘ address1: ‘ + subaddress1 + ‘ address2: ‘ + subaddress2 + ‘ city: ‘ + subcity + ‘ state: ‘ + substate + ‘ postalCode: ‘ + subpostalCode);

    poRecord.setValue({
    fieldId: ‘shipaddresslist’,
    value: null
    });

    // GET PO’s subrecord
    var shipAddress = poRecord.getSubrecord({
    fieldId: ‘shippingaddress’
    });
    log.debug(‘shipaddressfirst’, shipAddress);
    shipAddress.setValue({
    fieldId: ‘addressee’,
    value: subaddressName
    });

    shipAddress.setValue({
    fieldId: ‘addr1’,
    value: subaddress1
    });

    shipAddress.setValue({
    fieldId: ‘addr2’,
    value: subaddress2
    });

    shipAddress.setValue({
    fieldId: ‘city’,
    value: subcity
    });

    shipAddress.setValue({
    fieldId: ‘state’,
    value: substate
    });

    shipAddress.setValue({
    fieldId: ‘zip’,
    value: subpostalCode
    });

    shipAddress.commitLine({
    sublistId: ‘shippingaddress’
    });

    log.debug(‘shipAddressafter’, shipAddress);

    }
    return {
    beforeSubmit: beforeSubmit
    };
    });
    This is a cached copy. Click here to see the original post.

  • #1273

    JacksonP

    So after quite a while of testing, and working with netsuite support, we finally figured out that if you change this script to afterSubmit. Get the ID and load the record in script, then comment out the piece about setting ‘shippingaddresslist’ to null. The script works properly. Just know that the documentation for setting shippingaddresslist to null, in this context is wrong. It might still be right on the SO.

You must be logged in to reply to this topic.