Hi,
I have a user event script which executes right after a Quote is saved. This script does 3 things:
- Calculate total amount in words and stores it in a custom field
- Calculates sales rep contribution according to each line item value and who the sales rep is.
- Populate the class field by combining a "Region" field on the header with a "Business Unit" field on the item record itself. Combines those 2 and that's what makes up the class field for each line item.
Right now we have a quote with over 200 items, and it seems that on the 190th item the script's usage limit is exceeded.
The following is the Quote after save function
function asQuote(type)
{
T = 'Quote';
if(type != 'create' && type != 'edit')
{
return;
}
var recordType = nlapiGetRecordType();
recordId = nlapiGetRecordId();
var record = nlapiLoadRecord(recordType, recordId);
var subsidiary = record.getFieldValue('subsidiary');
//Calculate Total in Words:
var submit1 = totalInWords(record, recordId)
//Assign Sales Rep to sales team lines from item line
var submit2 = salesContribution(record, recordId);
//set class on line
var submit3 = populateLineBusinessUnit(record, recordId);
if(submit1 || submit2 || submit3)
{
//debug(A, T, 'Submit Record');
Submit(record, recordType);
}
}
It's also occurred to me that the usage limit exceeded is coming from the populateBusinessUnit() function that is executed in the code above.The following is my populateBusinessUnit function
function populateLineBusinessUnit(record, recordId)
{
var count = record.getLineItemCount('item');
var _class = record.getFieldValue('class');
var context = nlapiGetContext();
if(_class)
{
var className = record.getFieldText('class');
debug(D, 'class', 'class id: '+_class+' name: '+className);
for (var i=1 ; i<= count ;i++)
{
debug(D, 'remaining usage', nlapiGetContext().getRemainingUsage());
// Check Remaining Usage before proceeding
if (context.getRemainingUsage() < 50 && (i+1) < count) {
debug(D, 'scriptId', context.getScriptId());
debug(D, 'deploymentId', context.getDeploymentId());
var status = nlapiScheduleScript(context.getScriptId(), context.getDeploymentId());
debug(D, 'status', status);
if(status == 'QUEUED')
break;
}
//record.selectLineItem('item', i);
var busUnit = record.getLineItemValue('item','custcol_bu', i);
debug(D, 'class', 'bu: '+busUnit);
if(busUnit)
{
var classresults = nlapiSearchRecord('classification', null, new nlobjSearchFilter('name', null, 'is', className+' : '+busUnit));
if(classresults)
{
record.setLineItemValue('item', 'class', i, classresults[0].getId());
}
else
{
record.setLineItemValue('item', 'class', i, _class);
}
}
else
{
record.setLineItemValue('item', 'class', i, _class);
}
}
return true;
}
else
{
return false;
}
}
After reading through some posts on this forum I understood that I need to call a nlapiScheduleScript. I have done this in the above but it seems that the "status" always returns blank and the script doesn't get scheduled. I end up having only 189 items with the class field populated correctly, anything after that does not get filled.Can anyone help ?Thanks