I have written a script that upon saving an Item Receipt it looks up the employee who is listed on the Purchase Requisition and sends them an email letting them know that their items have been received. (pretty simple right?) Well, the script works great except it stops the page from saving... I have the script setup to run the function "After Submit". It is deployed to the "Item Receipt" to "Execute as Admin" and "All Roles". Below is my script, what am I doing wrong?
:: Begin Code ::
function saveRecord ()
{
//get custom form name
var fieldValue = nlapiGetFieldValue('customform');
//only fire function off if the form is 121 (MDG & ESI Item Receipt)
if (fieldValue == '121') {
//get the current users id
var userId = nlapiGetUser();
//this gets the Vendor of the PO
var vendor = nlapiGetFieldText('entity');
//this gets the record id of the PO
var createdFrom = nlapiGetFieldValue('createdfrom');
//alert('Created From Value = ' + createdFrom);
//this loads the created from purchase order and them gets the employee field from that record
var poRec = nlapiLoadRecord('purchaseorder',createdFrom);
var employeeInternalId = poRec.getFieldValue('employee');
//this gets the tranid (PO #) who is listed on the PO (tried to get employee here, but it is not an available field)
var poFields = ['tranid'];
var createdFromColumns = nlapiLookupField('purchaseorder', createdFrom, poFields);
//this gets the email address of the employee by looking up the employee record
var empFields = ['email', 'entityid'];
var createdFromEmpColumns = nlapiLookupField('employee', employeeInternalId, empFields);
//this starts the body of the email to be sent out
var emailBody = createdFromEmpColumns.entityid + ',<br><br>There has been receipt activity on <strong>Purchase Order # ' + createdFromColumns.tranid + ' to ' + vendor + '.</strong><br>Below is a summary of what was received:<strong><br><br>';
emailBody += '<table border="1" cellpadding="2" cellspacing="0" width="680">';
emailBody += '<tr>';
emailBody += '<th width="140">Item No.</th>';
emailBody += '<th width="420">Description</th>';
emailBody += '<th width="60">Qty<br>Received</th>';
emailBody += '<th width="60">Qty<br>Remaining</th>';
emailBody += '</tr>';
//loop through the items and write out all the items being received in the body of email.
var linecount = nlapiGetLineItemCount('item');
for (var i = 1; i <= linecount; i++) {
nlapiSelectLineItem('item', i);
var beingReceived = nlapiGetLineItemValue('item', 'itemreceive', i);
if (beingReceived == 'T') {
var itemName = nlapiGetLineItemValue('item', 'sitemname', i);
var itemQty = nlapiGetLineItemValue('item', 'quantity', i);
var itemRemain = nlapiGetLineItemValue('item', 'quantityremaining', i);
var qtyDiff = itemRemain - itemQty;
var itemDesc = nlapiGetLineItemValue('item', 'itemdescription', i);
emailBody += '<tr>';
emailBody += '<td>' + itemName + '</td>';
emailBody += '<td>' + itemDesc + '</td>';
emailBody += '<td align="center">' + itemQty + '</td>';
emailBody += '<td align="center">' + qtyDiff + '</td>';
emailBody += '</tr>';
}
}
emailBody += '</table>';
emailBody += '</strong><br><br>Please use the link below to view your Purchase Order, and please let me know if you have any questions.';
emailBody += '<br><br><a href="https://system.netsuite.com/app/accounting/transactions/purchord.nl?id=' + createdFrom + '&whence=">View Record</a>';
emailBody += '<br><br>Thanks!<br><br><br>';
//Send email to employee letting them know that items have been received on this PO
var emailSubject = 'Material Received on Purchase Order # ' + createdFromColumns.tranid;
nlapiSendEmail(userId, employeeInternalId, emailSubject, emailBody, null, null, null, null);
//PHASE 2
//get list of job admins from projects listed on 'created from' record (PO)
//send email to job admins of the projects listed on the 'created from' record (PO) be sure only send one email per job admin
}
}
:: End Code ::