function defaultCard()
{
var rec = nlapiGetRecordId();
nlapiLogExecution('DEBUG', 'Got CUSTOMER record id of:', rec);
if(rec==160618)
{
var count = nlapiGetLineItemCount('creditcards'); //grab CC count, some customers might have multiple credit card information
nlapiLogExecution('DEBUG', 'Got line item count of:', count);
var result = [];
for(i=1;i<=count;i++)
{
result.push(nlapiGetLineItemValue('creditcards', 'ccexpiredate', i));
nlapiLogExecution('DEBUG', 'Got CC expiration date on line:', i);
}
function max_index(dates) {
var ms_dates = millisecond_dates(dates);
var max = Math.max.apply(null, ms_dates);
return ms_dates.indexOf(max);
}
function millisecond_dates(dates) {
var ms_dates = [];
dates.forEach(function(one_date) {
ms_dates.push(millisecond_date(one_date));
});
return ms_dates;
}
function millisecond_date(datestring) {
return Number(realdate(datestring));
}
function realdate(datestring) {
return new Date(year(datestring), month(datestring));
}
function month(datestring) {
return Number(datestring.substring(0,2)) - 1;
}
function year(datestring) {
return Number(datestring.substring(3,7));
}
}
nlapiSetLineItemValue('creditcards', 'ccdefault', max_index(result)+1);
nlapiLogExecution('DEBUG', 'Set the following line to DEFAULT CREDIT CARD', max_index(result)+1);
}