Wednesday, March 13, 2019

Adding Months on to a date field

When creating a transaction a transaction date gets assigned to a field on that transaction. This sample will help you grab that transaction date and add months to it, you can even set it for multiple years. This is a great solution for contract expiration.

// Load the sales order
var record = nlapiLoadRecord('salesorder',291);

// Get the transaction date of the sales order.
var soDate = record.getFieldValue('trandate');

//Converts the string to date and returns the date object.
var tDate = nlapiStringToDate(soDate, 'datetz');

//Adds Months to that date. if you want to add years change the value from 6 to 12, 24, 36, etc
var nDate = nlapiAddMonths(tDate, 6);

//Convert the date object back to a String
var postDate = nlapiDateToString(nDate, 'datetz');

//Set the custom field with the date that you just created.
var field = record.setFieldValue('custbody2',postDate);

No comments:

Post a Comment