I am trying to write a script that will update the...
# suitescript
m
I am trying to write a script that will update the "Ship Via" field on an Item Fulfillment. I have the text name of the Ship Item that I want to use, but get an error when I try to set the field by the name, by the internal id (hard coded or variable), or a ship item object. Any thoughts?
b
usually you need to share the error message and name, potentially the code
m
I guess my question is not necessarily about my particular code, but rather a question about whether or not it is possible (and how) to use the text of a shipping item to update the shipping method (Ship Via) field on an item fulfillment.
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/record','N/search'],
function (record,search) {
function afterSubmit(context) {
try {
var itemFulfillment = context.newRecord;
//Pull ShipHawk service text
var shipHawkCarrierService = itemFulfillment.getValue('custbody_shiphawk_carrier_service');
if(shipHawkCarrierService != ''){
log.debug({
title: 'ShipHawk Carrier Service',
details: shipHawkCarrierService
})
//Run saved search - mappings of ShipHawk Shipping Methods and their NetSuite counterparts
var mapSearch = search.load({
id: 'customsearch_sh2ns_shipping_method_map'
})
var resultSet = mapSearch.run();
resultSet.each(function(result){
var shipHawkCarrier = result.getValue(result.columns[0]);
var shipHawkService = result.getValue(result.columns[1]);
var netSuiteShipMethod = result.getValue(result.columns[2]);
//Find the mapping where the current ShipHawk method matches
if (shipHawkCarrierService == shipHawkService){
var methodId;
log.debug({
title:'Equal',
details: netSuiteShipMethod
})
//Search for NetSuite ship_item record to return internalID
var methodSearch = search.load({
id:'customsearch_ship_items_shiphawk'
})
var methodResultSet = methodSearch.run();
methodResultSet.each(function(methodResult){
var internalId = methodResult.getValue(methodResult.columns[0]);
var methodName = methodResult.getValue(methodResult.columns[1]);
if(methodName == netSuiteShipMethod){
methodId = internalId;
}
return true;
})
//Load ship_item object by internalID
var methodObject = record.load({
type:'shipitem',
id:methodId,
isdynamic:true
})
var carrier = (shipHawkCarrier == 'UPS' ? 'UPS' : 'FedEx/USPS/More');
//Update fields within Item Fulfillment
var id = record.submitFields({
type: 'itemfulfillment',
id: itemFulfillment.id,
values: {
'shipcarrier':carrier,
'shipmethod':methodObject
}
})
return;
}
return true;
})
}
} catch (e) {
log.debug({
title: 'Error Details',
details: e
})
}
}
return {
afterSubmit: afterSubmit
};
}
);
b
there are multiple things that can go wrong while trying to change the ship method on a fulfillment
for example your code looks like it could fail by trying to change between different ship carriers
it could also be because you are trying to use record.submitFields wrong
it could be both
it could be others
its very hard to tell without the error name and message
at the very least, the code you shared doesnt match your description of trying to set a ship method by text
in fact, the text part of it looks arbitrary, you also get the internal id of the ship method
setting fields by internal id is usually better than text
m
Thanks for the replies....the code that I dropped was my 4th or 5th try at getting the shipmethod to change. I DID figure it out this morning. I changed the script to "beforeSubmit" and used context.newrecord.setValue() for the carrier first and the shipmethod second. It turns out that I only needed the internalID (not the entire shipitem object), so I just passed that to the shipmethod field.
s
Hey @Micah Related question - is it possible to join transaction search with Shipping Item? based on internal id WHen I use {shipitem.id} it says "invalid field"