We have to set the shipping method “To be routed” only for placing order from the webstore. Shipping method currently in the customer record not required to be updated or override when we set the default shipping to “to be routed”.
For that we are adding the changes by keeping the value of the current shipping method in another field for customer record and then check conditions and set it back if needed in the shipping method field during the confrimation section.
define(
'JJ.shipmethod.shipmethod'
, [
'OrderWizard.Module.Shipmethod',
'JJ.shipmethod.shipmethod.Model',
'Profile.Model',
'OrderWizard.Step',
'OrderWizard.Module.Confirmation',
'OrderWizard.Module.ShowShipments'
]
, function (
OrderWizardModuleShipmethod,
shipmethodModel,
ProfileModel,
OrderWizardStep,
OrderWizardModuleConfirmation,
OrderWizardModuleShowShipments
) {
'use strict';
return {
mountToApp: function mountToApp(container) {
_.extend(OrderWizardStep.prototype, {
initialize: _.wrap(OrderWizardStep.prototype.initialize, function initialize(fn) {
fn.apply(this, _.toArray(arguments).slice(1));
this.model = new shipmethodModel();
this.model.fetch({
}).done(function (result) {
console.log("result", result);
});
})
});
_.extend(OrderWizardModuleShipmethod.prototype, {
getContext: _.wrap(OrderWizardModuleShipmethod.prototype.getContext, function getContext(fn) {
var context = fn.apply(this, _.toArray(arguments).slice(1));
var shippingMethodId = SC.CONFIGURATION.SetDefaultMethod.shipid;
var currentCustomerShipmethod = this.model.attributes.shipmethod;
if (!!currentCustomerShipmethod) {
var selected_method = this.model.get('shipmethods').findWhere({ internalid: currentCustomerShipmethod });
if (!selected_method) {
// Set the default shipping method from configuration record.
if (!!shippingMethodId) {
this.model.attributes.shipmethod = shippingMethodId;
}
else {
// Set the default shipping method from as "to be routed".
this.model.attributes.shipmethod = '4888';
}
}
}
else {
// Set the default shipping method from configuration record.
if (!!shippingMethodId) {
this.model.attributes.shipmethod = shippingMethodId;
}
else {
// Set the default shipping method from as "to be routed".
this.model.attributes.shipmethod = '4888';
}
}
return context;
})
});
_.extend(OrderWizardModuleShowShipments.prototype, {
getContext: _.wrap(OrderWizardModuleShowShipments.prototype.getContext, function getContext(fn) {
var context = fn.apply(this, _.toArray(arguments).slice(1));
var shippingMethodId = SC.CONFIGURATION.SetDefaultMethod.shipid;
var currentCustomerShipmethod = this.model.attributes.shipmethod;
if (!!currentCustomerShipmethod) {
var selected_method = this.model.get('shipmethods').findWhere({ internalid: currentCustomerShipmethod });
if (!selected_method) {
// Set the default shipping method from configuration record.
if (!!shippingMethodId) {
this.model.attributes.shipmethod = shippingMethodId;
}
else {
// Set the default shipping method from as "to be routed".
this.model.attributes.shipmethod = '4888';
}
}
}
else {
// Set the default shipping method from configuration record.
if (!!shippingMethodId) {
this.model.attributes.shipmethod = shippingMethodId;
}
else {
// Set the default shipping method from as "to be routed".
this.model.attributes.shipmethod = '4888';
}
}
return context;
})
});
_.extend(OrderWizardModuleConfirmation.prototype, {
getContext: _.wrap(OrderWizardModuleConfirmation.prototype.getContext, function getContext(fn) {
var context = fn.apply(this, _.toArray(arguments).slice(1));
this.shipModel = new shipmethodModel();
this.shipModel.fetch({ data: {
message: "confirm"
},
}).done(function (result) {
console.log("resultin confirm", result);
});
return context;
})
});
}
};
});
Suitescript
// JJ.shipmethod.shipmethod.js
// Load all your starter dependencies in backend for your extension here
// ----------------
define('JJ.shipmethod.shipmethod'
, [
'shipmethod.ServiceController',
'SC.Model',
'SC.Models.Init',
'Configuration'
]
, function (
shipmethodServiceController,
SCModel,
ModelsInit,
Configuration
) {
'use strict';
return SCModel.extend({
setMethod: function () {
try {
var customerId = nlapiGetUser();
var shipValue = Configuration.get().SetDefaultMethod.shipid;
if(shipValue === ""){
shipValue = '4888';
}
var currentCustomer = nlapiLoadRecord('customer', customerId);
var currentShipMethod = currentCustomer.getFieldValue('shippingitem');
if (currentShipMethod !== shipValue && currentShipMethod !== "") {
currentCustomer.setFieldValue('custentity_jj_default_delivery_method', currentShipMethod);
var customEntityRecordId = nlapiSubmitRecord(currentCustomer, true);
}
if(currentShipMethod === shipValue){
console.error("currentShipMethodsdsd", currentShipMethod);
currentCustomer.setFieldValue('custentityjj_is_ship_method_set', "T");
var customEntityRecordId = nlapiSubmitRecord(currentCustomer, true);
}
return (currentCustomer);
} catch (error) {
console.error(JSON.stringify(error));
return {
code: "500",
message: "error"
};
}
},
setCustomMethod: function () {
try {
var customerId = nlapiGetUser();
var currentCustomer = nlapiLoadRecord('customer', customerId);
var currentShipMethod = currentCustomer.getFieldValue('custentity_jj_default_delivery_method');
if (currentShipMethod !== "") {
currentCustomer.setFieldValue('shippingitem', currentShipMethod);
var customEntityRecordId = nlapiSubmitRecord(currentCustomer, true);
}
return (currentCustomer);
} catch (error) {
console.error(JSON.stringify(error));
return {
code: "500",
message: "error"
};
}
}
});
});