Sunday, November 25, 2018

Ability to set the default increment of Start Time and End Time for new Event record.

This article will show how to set the the default increment for the Start Time and End Time for new Event record.

A client script can be created to set the default increment the script will take number of minutes and will set the End Time.

function pageInit(type){

    var startTime = nlapiGetFieldValue('starttime');
    var endTime = addTime(startTime, 30);
    nlapiSetFieldValue('endtime', endTime, false, true);
}

function addTime(startTime, addMinutes) {
    var startHour = parseInt(startTime.substr(0, startTime.indexOf(':')));
    var startMinutes = parseInt(startTime.substr(startTime.indexOf(':')+1, 2));
    var startAMPM = startTime.substr(startTime.toLowerCase().indexOf('m') - 1, 2);
    var endHours;
    var endMinutes;
    var endAMPM;

    // Change hour to 24-hour clock format
    if (startAMPM.toLowerCase() == 'am' && startHour == 12) {
        startHour = 0;//converting hour to 0');
    }

    else if (startAMPM.toLowerCase() == 'pm' && startHour != 12) {
        startHour = startHour + 12;//adding 12hrs');
    }

    var newStartTime = new Date();
    newStartTime.setHours(startHour);
    newStartTime.setMinutes(startMinutes);
    newStartTime.setSeconds(0);
    var newEndTime = new Date(newStartTime);
    newEndTime.setMinutes(newStartTime.getMinutes() + addMinutes);

    // Convert the new date object back to a 12-hour clock format string
    if (newEndTime.getHours() < 12) {
        endAMPM = 'am';
    }

    else {
        endAMPM = 'pm';
    }

    if (newEndTime.getHours() == 0) {
        endHours = '12'; 
    }

    else if (newEndTime.getHours() > 12) {
        endHours = '' + (newEndTime.getHours() - 12);
    }

    else {
        endHours = newEndTime.getHours();
    }

    endMinutes = addzero(newEndTime.getMinutes());
    addTime = endHours + ':' + endMinutes + ' ' + endAMPM;

    return addTime;
}

function addzero(number) {
    var str = '' + number;
    if (str.length < 2) {
        str = '0' + str;
    }
    return str;
}

No comments:

Post a Comment