I currently run a User Event script with a Before Submit function that populates custom column fields for every transaction. Because it's a User Event script, it doesn't fire on transactions imported through Web Services, like POS Invoices.
Edit: Per the comment below, this is a misunderstanding on my part. User Event scripts do run on Web Services.
What's the best kind of script to use if I want to modify custom fields on every imported transaction? Would a Workflow Action script triggered on After Record Submit work? Could that be problematic if I'm importing several transactions at once?
If there isn't a good way to do this when the transaction is imported, I'll probably just run a Scheduled or Map/Reduce script to update these records after the fact.
Below is the UE script. It looks at each line in a transaction, starting at the bottom, to add discount information to the line above it, if applicable.
/**
*@NApiVersion 2.0
*@NScriptType UserEventScript
*/
define([], function() {
return {
beforeSubmit : function(context) {
// get record and line count
var currentRecord = context.newRecord;
var count = currentRecord.getLineCount({
sublistId:'item'
});
// init discount amount
var discountAmount = 0;
// for each line, starting from the bottom
for(var i = (count-1); i >= 0; i--) {
// fetch data
var type = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'itemtype',
line: i
});
var amount = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'amount',
line: i
});
var quantity = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i
});
if (type == 'Discount') {
// add to current discount amount if discount
discountAmount += amount;
} else {
// parse data
amount = parseFloat(amount);
quantity = parseInt(quantity);
discountAmount = parseFloat(discountAmount);
// set variables
calculatedAmount = (amount + discountAmount).toFixed(2);
calculatedRate = ((amount + discountAmount) / quantity).toFixed(2);
calculatedDiscount = (discountAmount).toFixed(2);
// update sublist
currentRecord.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_calculated_amount',
line: i,
value: calculatedAmount
});
currentRecord.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_calculated_rate',
line: i,
value: calculatedRate
});
currentRecord.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_calculated_discount',
line: i,
value: calculatedDiscount
});
log.debug('discountAmount', discountAmount);
// reset current discount amount
discountAmount = 0;
}
}
}
}
})
question from:
https://stackoverflow.com/questions/65924634/what-is-the-recommended-method-for-running-suitescripts-on-imported-web-services 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…