How do I prevent users from putting an earlier tim...
# suitescript
i
How do I prevent users from putting an earlier time in NetSuite's custom Time field? Hello, Please is it possible to restrict user from entering an earlier time in a netsuite field?
b
sounds like you want a client script
basics are that you want to keep track of the original field value
so that you can do a comparison during a validateField entry point
i
exactly but I don't know which function to use to validate the time field
I use the following script to prevent user to enter an earlier date`function validateField(scriptContext) {` var curRec = scriptContext.currentRecord; var fieldName = scriptContext.fieldId; if (fieldName === 'custrecord_date_souhaitee') { //replace "date" with your field id var recDate = curRec.getValue({fieldId: fieldName}); var today = new Date(); if (recDate < today){ alert('Vous ne pouvez pas entrer une date antérieure à la date du jour'); curRec.setValue({ fieldId: 'date', value: null }); } }
b
what have you tried? That code is basically what you want
with the exception that you wouldnt want to do a comparison against today
i
this code is for the date and I want something to compare time*
b
do a get on your time of day field and log its value
i
ok what is the suitescript function to get the current time please?
b
you probably want to better understand the code you shared
it already does that
i
the code I did is for date only if I'm not mistaken
Not time
b
its why i told you to log your time of day
you are under the belief that they are different
i
Can you share an example please Because in my custom form I have two differents fields: one for the date and another of the type "Time of the Day" which contain the time
b
as in an example of how to log a fields value?
i
and I want to trigger the values entered by the user in theses thwo fields
b
i dont think you understand how netsuite represent time of day fields
so im telling you to log the value in your time of day field
i
Copy code
SuiteScript 2.x
      var timeRecord = record.load({
        id: 1,
        type: 'customrecord_currenttime'
      });

      var timezone = config.load({type: config.Type.USER_PREFERENCES}).getValue({fieldId: "TIMEZONE"})

      var currentTime = timeRecord.getValue('custrecord_currenttime_now');

      var usersTime = format.format({
          value: usersTime,
          type: format.Type.DATETIME,
          timezone: format.Timezone.timezone
      });
Like this?
I found it on a blog
But it's seem like it's server side script type
b
that code is probably a waste of time
it looks like its designed to get a date and then turn it into a string
when it could have simply done timeRecord.getText('custrecord_currenttime_now');
you need to figure out how to get the value of a field
and then log it
i
My concern in fact is to be able to compare the time retrieved in this field with the current system time, so as to prevent the user from entering a time lower than the current time on his system
b
and to do that, you need to understand the value you get out of a time of day field
so far you have not, so thats the first thing you need to learn
i
Do you have any content to recommend to me to acquire this skill?
b
did you need help getting the value of a field?
or logging it
i
Yes please
b
pick one to start with
i
This is an example or I don't understand
b
as far as i can tell, you dont understand the code that you shared
im not giving you more examples that you dont understand
ill help you gain the knowledge to do your task, but i wont do it for you
i
Ok I understand
I'll try to better understand this by reading the netsuite docs about client script
b
thats not really where you would want to start
you need to learn how to log, which should involve using the console
generally not knowing how to log means you didnt learn javascript, so you should probably go find some javascript course
getting a field value is basically using the appropriate get related method, in this case CurrentRecord.getValue
i
I see Thanks for the valuable advice.
Copy code
Hello, here is the script I've definetly done:
Copy code
function validateField(scriptContext) {
        var curRec = scriptContext.currentRecord;
        var fieldName = scriptContext.fieldId;
        var today = new Date();

        if (fieldName === 'custrecord_date_souhaitee') { 
          var recDate = curRec.getValue(fieldName);
          if (recDate < today){
            alert('Vous ne pouvez pas entrer une date antérieure à la date du jour');
            curRec.setValue({
              fieldId: 'custrecord_date_souhaitee',
              value: null
            });
          }
          
        }

        if (fieldName === 'custrecord_heure_souhaitee') { 
            var recTime = curRec.getValue(fieldName);
            var curTime = today.getTime();
            if ((recTime < curTime) & (recDate = today)){
              alert('Votre date de planification étant ce jour, vous devez entrer une heure supérieure à l\'heure actuelle');
              curRec.setValue({
                fieldId: 'custrecord_heure_souhaitee',
                value: null
              });
            }
            
          }
    }
But I'm facing a problem. When the user enter a date which is previous to the current date the alert box is displayed but when you click on the Ok button it doesn't close the box and after certain time I get the
Error: JS_EXCEPTION
RangeError Maximum call stack size exceeded
error message
Please any idea
?
b
first off your validateField entrypoint needs to return true or false
with true allowing the field to be set and false not allowing
second, setting the field to null is triggering the validateField again
and again, and again, and so on
i
And what should I do there to reset the field value?
b
you can return true early before running your logic
or you can use ignoreFieldChange
i
Please take a look at the entire script. Because after deploying it I don't have any action when entring values on the various fields. A bit as if the script was not taken into account
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/ui/dialog'],

function(record, dialog) {
    
    function fieldChanged(scriptContext) {
        var currentRecord = scriptContext.currentRecord;
        var fieldName = scriptContext.fieldId;
        if(fieldName === 'custrecord_type_d_intervention'){
            var type_intervention = currentRecord.getValue({fieldId:fieldName});
            if(type_intervention == 'Maintenance'){
                alert('Veuillez renseigner le problème faisant office à cette maintenance');
                var problemField = currentRecord.getField({
                    id: 'custrecord_probleme'
                });
                problemField.updateDisplayType({
                    displayType: 'NORMAL'
                });
            }

        }

    }

    function validateField(scriptContext) {
        var curRec = scriptContext.currentRecord;
        var fieldName = scriptContext.fieldId;
        var today = new Date();

        if (fieldName === 'custrecord_date_souhaitee') { 
          var recDate = curRec.getValue(fieldName);
          if (recDate < today){
            alert('Vous ne pouvez pas entrer une date antérieure à la date du jour');
            curRec.setValue({
              fieldId: 'custrecord_date_souhaitee',
              value: null,
              ignoreFieldChange: true,
              forceSyncSourcing: true
            });
          }
          
        }

        if (fieldName === 'custrecord_heure_souhaitee') { 
            var recTime = curRec.getValue(fieldName);
            var curTime = today.getTime();
            if ((recTime < curTime) & (recDate = today)){
              alert('Votre date de planification étant ce jour, vous devez entrer une heure supérieure à l\'heure actuelle');
              curRec.setValue({
                fieldId: 'custrecord_heure_souhaitee',
                value: null,
                ignoreFieldChange: true,
                forceSyncSourcing: true
              });
            }
            
          }
          return true;
    }

    return {
        fieldChanged: fieldChanged,
        validateField: validateField,
    };
    
});
I don't know if I just misdeployed
b
custrecord_date_souhaitee
should still work
the other 2 fields not
Copy code
if ((recTime < curTime) & (recDate = today)){
is basically wrong in multiple places
i
But nothing is working now
b
the & operator is bitwise and
the = operator is for assignment
while recTime is a Date and curTime is a number
fair chance that custrecord_type_d_intervention is a select field, which means its value is not
Maintenance
and updateDisplayType is not a method of a currentRecord.Field
i
How to get the selected value then?
b
usually you want to check your browser's console for errors
and add logs to your entry points to tell when they run
i
For the display type should I use the serverWidget module?
b
you have no access to the serverWidget in client script
i
Ok how can I change the display field type then?
And for the recTime do I need to convert the value into a number?
b
your options for a field are documented on currentRecord.Field
you dont have to convert your date to a number, a Date to Date comparison would work as you expect
i
Copy code
function fieldChanged(scriptContext) {
        var currentRecord = scriptContext.currentRecord;
        var fieldName = scriptContext.fieldId;
        if(fieldName === 'custrecord_type_d_intervention'){
            var type_intervention = JSON.stringify(currentRecord.getSelectOptions({
                filter : 'Maintenance',
                operator : 'is'
            }));
            if(type_intervention === 'Maintenance'){
                alert('Veuillez renseigner le problème faisant office à cette maintenance');
                var problemField = currentRecord.getField({
                    id: 'custrecord_probleme'
                });
                problemField.isDisplay = true;
            }

        }

    }
This is the solution I've implemented for the selected field
b
i still wouldnt expect that to work
it looks worse than the first attempt honestly
i
Yeah I'm going back into the doc
b
its not really the documentation at this point
Copy code
var type_intervention = currentRecord.getValue({fieldId:fieldName});
you need to figure out what value is in type_intervention
the console is one of the more common tools to do so
the other choice is the debugger