I'm trying to get a massupdate script to email all customers with a balance > 0. As far as I know I can't use the "Sending Collection Letters in Bulk" in the help because we also need to send the statements to the contacts on that customer. We have a checkbox to control if a contact receives statements or not. The mass update criteria is as follows:
Do not email statement (custom) is false
Balance > 0
I've tried to run the mass update and it seems to send to 5 customers or so before failing with the error sss_invalid_cc_email. I then thought we could be attaching empty email addresses in the cc array. I tried to check for blank strings but it doesn't seem to be working. Here is our script (sorry for all the debug statements just trying to figure out why it's not working):
function scheduled(type, id) {
var rec = nlapiLoadRecord(type,id);
var count = rec.getLineItemCount('contactroles');
var custNum = rec.getFieldValue('entityid');
var salesRep = nlapiLoadRecord('employee',rec.getFieldValue('salesrep'));
var salesRepEmail = salesRep.getFieldValue('email');
var carbonCopy = new Array();
//If there are customers in a list
if(count>0){
var j=0;
for(i = 1; i<=count;i++){
var contact = nlapiLoadRecord('contact',rec.getLineItemValue('contactroles','contact',i));
//Add contact to the Carbon Copy List
if(contact.getFieldValue('custentity_es_email_invoice')=='T'){
nlapiLogExecution("Debug","what is i?",i);
nlapiLogExecution("Debug","j before increase",j);
if(contact.getFieldValue('email') !="" || contact.getFieldValue('email') !=null){
nlapiLogExecution("Debug","cc email address",contact.getFieldValue('email'));
carbonCopy[j]=contact.getFieldValue('email');
j++; }
nlapiLogExecution("Debug","j after increase",j);
}else{
//nlapiLogExecution("Debug",'Do no email them an invoice','I just wanted the else to be safe.');
}
}
sendEmail(id, rec.getFieldValue('email'), carbonCopy, salesRepEmail,custNum);
}else{
sendEmail(id, rec.getFieldValue('email'),"", salesRepEmail,custNum);
}
//nlapiLogExecution("Debug", "ID",id);
}
function sendEmail(id, email, cc, salesrep,custNum){
nlapiLogExecution("Debug","Email Sent",email);
//print the statement to a PDF file object
var records = new Object();
records['entity']=id;
var file1 = nlapiPrintRecord('STATEMENT', id, 'PDF');
var bcc = new Array();
bcc[0] = salesrep;
//send the PDF as an attachment
//nlapiSendEmail(author, recipient, subject, body, cc, bcc, records, attachments)
nlapiSendEmail('75169', email, 'Account Statement ' + custNum, '<HTML><HEAD></HEAD><BODY style="font-family:Verdana,Arial,Helvetica,sans-serif;font-size:10pt;">Dear Valued Customer,<p>Attached is your most recent customer statement of all open items.</p><p>If you have any questions with our records, please contact us and a representative will be happy to discuss them with you.</p><p>Sincerely, <br>Accounts Receivable Department</p></BODY></HTML>', cc, bcc, records, file1);
}
Any ideas? I am testing with a blank email which causes a different error but i'd still like to be able to check for blank emails. It could also be adding invalid email addresses to the cc array but I'm not sure how to check for those. Also, maybe there is a much easier way to do what we want? Any help is appreciated.