Does anything seem odd about this script? It's not...
# suitescript
j
Does anything seem odd about this script? It's not setting the correct value on a date field and it sets 12/21/1969. I did not write this script my eyes are on those 2 lines with {setLastContactDate} Thanks,
Copy code
/**
 * @NApiVersion 2.0
 * @NScriptType userEventScript
 * @NModuleScope Public
 *

 */

 define(['N/search','N/record'], function(search,record) {

    function afterSubmit(context) {
        var stLogStatus = 'afterSubmit';
        try{
            var stRecordType = context.newRecord.type;

            if(stRecordType == 'note'){
                var recUserNote = context.newRecord;
                var stCustomerId = recUserNote.getValue('entity');
                var dtLastNoteDate = getLatestNoteDate(stCustomerId);
                setLastContactDate(stCustomerId, dtLastNoteDate)
            }
            if(stRecordType == 'customer'){
                var stCustomerId = context.newRecord.id;
                var dtLastNoteDate = getLatestNoteDate(stCustomerId);
                setLastContactDate(stCustomerId, dtLastNoteDate);
            }
        }catch(error){
            log.error(stLogStatus, error);
        }
    }

    function setLastContactDate(stCustomerId, dtContactDate){
        
        if(!isEmpty(dtContactDate)) // 2024-06-01 CAW
            record.submitFields({
                type: record.Type.CUSTOMER,
                id: stCustomerId,
                values: {
                    'custentity_last_contact_date' : dtContactDate,
                }
            });
    }

    function getLatestNoteDate(stCustomerId){
        var objSearchResults = search.create({
            type: "note",
            filters:
            [
               ["customer.internalid","anyof",stCustomerId]
            ],
            columns:
            [
               search.createColumn({
                  name: "notedate",
                  sort: search.Sort.DESC,
                  label: "Date"
               })
            ]
         });
         var latestDate = null;
         objSearchResults.run().each(function(result){
             latestDate = result.getValue('notedate');
            return false;
         });

         if(!isEmpty(latestDate)) latestDate = new Date(latestDate);

         return latestDate;
    }
    
    function isEmpty(value) {
        var logTitle = 'isEmpty';
        try {
            if (value == null || value == '' || (!value) || value == 'undefined') {
                return true;
            }
            return false;
        } catch (error) {
            log.error(logTitle, error);
        }
    }

    return {
        afterSubmit: afterSubmit
    }
});
c
would throw a log in there after it reads the date and see what value its getting. It comes back as a string in a search so the new Date() looks ok it must be getting a weird value.
👍 1
s
can you try changing the script version to 2.1? if I remember correctly there is some slight difference between date handling
👍 1