RE: Convert quote to SO line by line when status changes on line item field
Hi all,
below is a code written when a User edits or create a quote, and the custom field is set to approved a quote gets converted to an order.
when a user edits the quote again, how do I trigger so that the updated field triggers another order to be created? and so on?
function(record) { function afterSubmit(context) { var quote = context.newRecord; if ((context.type == context.UserEventType.EDIT) || (context.type == context.UserEventType.CREATE)) { var approvedCount = quote.getLineCount({ sublistId : 'item' }); for (var i = 0; i < approvedCount; i++) { var approved = quote.getSublistValue({ sublistId: 'item', fieldId: 'field', line: i }); if (approved == '1234') { var order = record.transform({ fromType: record.Type.ESTIMATE, fromId: quote.id, toType: record.Type.SALES_ORDER, isDynamic: true }) } var orderLine = order.getLineCount({ sublistId : 'item' }); for (var j = 0; j < orderLine; j++ ){ var approved = quote.getSublistValue({ sublistId: 'item', fieldId: 'field', line: j }); var selectLine = order.selectLine({ sublistId: 'item', line: i }) order.setCurrentSublistValue({ sublistId: 'item', fieldId: 'field', value: approved }) order.commitLine({ sublistId: 'item' }) } } order.save(); } } return { beforeLoad: beforeLoad, beforeSubmit: beforeSubmit, afterSubmit: afterSubmit };
You have a lot of logic dedicated to making the `custcol_dts_field` field match between the estimate and the sales order. That logic shouldn’t be ncesssary, the transformed sales order will source its value from the original estimate.
Your looping logic also appears flawed, it looks like it tries to create an order for each line that has `custcol_dts_field` set to a certain value. You probably only want 1 sales order, not 1 per line. That said, you also don’t continue onto the next iteration of the loop when `custcol_dts_field` does not have a matching value, which should throw errors.
Ignoring the weird `custcol_dts_field` logic and the looping mechanics, your code should create new sales orders whenever an estimate is saved or created. That honestly does not sound like safe behavior to me, its an easy way to get duplicate sales orders.