Hello,
I have written a client script that based on a custom column field called Split (check box) that i added it will:
- Copy the Sales Order
- Remove the ticked lines on the original SO
- Remove everything except the ticked lines on the copy
- Mess around with a discount line to balance out the gross profit (this a sales request)
All well and good, and i am pleased that the script works, however it takes anywhere between three and ten minutes to run.
Has any body got any suggestions to make this run quicker (apart from buying i7 PCs with 8GB RAM).
Script is:
function SplitSO(type) { /* Determine process type */ var context = nlapiGetContext(); var exeType = context.getExecutionContext(); /* get current record id */ var originalSOid = nlapiGetRecordId(); /* set the array counter */ var j=0; var discount=0; var discountId=0; splitLines = new Array(); //Check that the SO is at Pending Fulfilment or Pending Approval before progressing if (nlapiGetFieldValue('status')=='Pending Approval' || nlapiGetFieldValue('status')=='Pending Fulfillment') { /* loop through line of SO */ for ( i=1 ; i<=nlapiGetLineItemCount('item') ; i++ ) { var split = nlapiGetLineItemValue('item','custcol_split_item',i); if (nlapiGetLineItemValue('item','custcol_split_item',i) == "T") { splitLines[j] = i; j = j+1; } var itemId = nlapiGetLineItemValue('item','item',i); /* Check for discounts in array of discount item internalIds */ if (itemId==5297 || itemId==5298 || itemId==5299 || itemId==5300 || itemId==5302) { discount = parseFloat(nlapiGetLineItemValue('item', 'amount', i)); var discountId = itemId; /* Ensure that the discount line is set as a value (not a percent) */ nlapiSelectLineItem('item', i); nlapiSetCurrentLineItemValue('item','price', -1, true, true); nlapiSetCurrentLineItemValue('item','rate', discount, true, true); nlapiCommitLineItem('item'); } } /* Calculate subtotal before discount */ var total = nlapiGetFieldValue('subtotal')-discount; //get the margin % var marginpercent = nlapiGetFieldValue('custbody_margin_percent_less_com'); marginpercent = parseFloat(marginpercent.substring(0,marginpercent.indexOf("%")-1)); //copy SO (if splits have been ticked) if (splitLines.length >= 1) { alert('Splitting out '+splitLines.length+' lines to a new Sales Order.n This could take up to a minute.'); var newSO = nlapiCopyRecord('salesorder', originalSOid); var newSOid = nlapiSubmitRecord(newSO, false, true); /* Delete all ticked lines on original SO */ for ( i=splitLines.length - 1 ; i>=0 ; i-- ) { nlapiSelectLineItem('item', splitLines[i]); nlapiRemoveLineItem('item'); } nlapiCommitLineItem('item'); /* on new SO delete all unticked lines */ var newSO = nlapiLoadRecord('salesorder', newSOid); //alert('loaded new SO '+newSOid); /* remove deposit details and invoice message*/ newSO.setFieldValue('custbodyrelated_inv', ""); newSO.setFieldValue('message',""); SOLineCount = newSO.getLineItemCount('item'); j=splitLines.length-1; for ( i=SOLineCount ; i>0 ; i-- ) { if (splitLines[j] == i) { /* Remove the split flag */ newSO.setLineItemValue('item', 'custcol_split_item', i, ""); j = j- 1; } else { newSO.removeLineItem('item', i); } } /* submit and reload record */ var id = nlapiSubmitRecord(newSO, false, true); var newSO = nlapiLoadRecord('salesorder', newSOid); var newSOnumber = newSO.getFieldValue('tranid'); var newSOSub = newSO.getFieldValue('subtotal'); var newSOCost = newSO.getFieldValue('custbody_cost_estimate'); var newSOnewDisc = -1 * (newSOSub - (newSOCost/(1-(marginpercent/100)))); if (discountId) { //Add subtotal line newSO.selectNewLineItem('item'); newSO.setCurrentLineItemValue('item', 'item', 5294); newSO.commitLineItem('item'); //add Discount line if one existed previously newSO.selectNewLineItem('item'); newSO.setCurrentLineItemValue('item', 'item', discountId); newSO.setCurrentLineItemValue('item', 'price', -1); newSO.setCurrentLineItemValue('item', 'rate', newSOnewDisc); newSO.commitLineItem('item'); var id = nlapiSubmitRecord(newSO, false, true); //Change the discount on original SO var newDiscount = discount - newSOnewDisc; nlapiSelectLineItem('item', nlapiFindLineItemValue('item', 'item', discountId)); nlapiSetCurrentLineItemValue('item', 'price', -1, true, true); nlapiSetCurrentLineItemValue('item', 'rate', newDiscount, true, true); nlapiCommitLineItem('item'); } alert('Created new Sales Order '+newSOnumber); } else { alert('No lines have been marked for splitting.'); } } else { alert('Unable to split this Sales OrdernOnly split sales orders at Pending Approval or Pending Fulfillment'); } }Thanks,Martyn