Search code examples
netsuitesuitescriptsuitescript2.0

How to create Time Entry records in NetSuite using SuiteScript?


I am trying to create a Time Entry record on a project task using suitescript. But it returns a FEATURE_DISABLED error: "type":"error.SuiteScriptError","name":"FEATURE_DISABLED","message":"The feature 'Timesheets' required to access this page is not enabled in this account.","id":"","stack":["anonymous(N/serverRecordService)","execute(/SuiteScripts/time_entry_test.js:47)"]

I wrote this code. The error is thrown when executing rec.save() line. I would also like to know how to use setValue() method for the field "Duration" (field ID : hours). I tried with values like 4:00 and 4:00:00 but it doesn't accept them. How can I enter hours and minutes to this field?

var rec = record.create(
            {
                type: record.Type.TIME_ENTRY, 
                isDynamic: true
            }
        );
        
        rec.setValue({
            fieldId: 'employee',
            value: 1962,
            ignoreFieldChange: true
        });
        
        rec.setValue({
            fieldId: 'customer',
            value: 2069,
            ignoreFieldChange: true
        });
        
        rec.setValue({
            fieldId: 'casetaskevent',
            value: 1227,
            ignoreFieldChange: true
        });
        
        
        rec.setValue({
            fieldId: 'hours',
            value: 5,
            ignoreFieldChange: true
        });
        
        var trackTime = rec.save();

Solution

  • Try with the following code, you need to use 'timebill' and for the duration/hours you need to use a string with the format: 'hh:mm' and minutes can only be in multiple of 15 mins, NetSuite automatically round the time if you enter something different.

    require(['N/action', 'N/record'], function(action, record) {
    
        var rec = record.create({
            type: 'timebill',
            isDynamic: true
        });
        rec.setValue({
            fieldId: 'employee',
            value: 104
        });
        rec.setValue({
            fieldId: 'location',
            value: 312
        });
        rec.setValue({
            fieldId: 'hours',
            value: '2:15'
        });
    
        //set the rest of your fields
    
        var recordId = rec.save();
    
    
    });