Hi Guys, I'm setting up today's date to a custom D...
# suitescript
n
Hi Guys, I'm setting up today's date to a custom Date field on a form
Copy code
newLead.setValue({fieldId : 'custentity12',
                                value : new Date() ,
                            ignoreFieldChange : true,
                            fireSlavingSync : true
                        });
But the date is not getting set to the field, Please help me here!!!
b
looks reasonable
does the field get set with a value and its not saving or something like that?
or is there an error
n
no error but when I run this UE script the field is not getting set with the Date!
I can see the date value is the logs though! when i do getValue from that field where i'm settin the date field it is showing me logs rightly but not setting the date to field!
b
although i doubt its the problem, you should remove
Copy code
fireSlavingSync : true
thats client script related
otherwise my guess is that your field does not store value
n
It is storing value since I have checked the STORE check box for that field and even if this fireSlavingSync : true is removed still it doesn't set the value to the field!
b
depending on your code, you may want to check if there are other scripts or workflows setting the field
n
nope, since i have just created that field!
b
whats your code?
p
My guess is that you’re doing it in an afterSubmit, so it’s working as designed
n
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/record','N/email','N/search','N/runtime','N/format'], function(record,email,search,runtime,format) {
  function afterSubmit(context) {
    try{
		if (context.type == context.UserEventType.CREATE || context.type == context.UserEventType.EDIT){
          log.debug('aa');
			var oldLead = context.oldRecord;
         
			var id = context.newRecord.id;
            var newLead = record.load({
                  type: record.Type.LEAD,
                  id: id,
                  isDynamic: true,
              });
			  var newDate;
			  var oldOwner = oldLead.getValue({fieldId:'custentity9'});
			  var newOwner = newLead.getValue({fieldId:'custentity9'});
          	  log.debug('oldo',oldOwner);
          	  log.debug('newo',newOwner);
			  
			  if(oldOwner != newOwner){
			 
                var d =   new Date();
                
			  newLead.setValue({fieldId : 'custentity12',
                                value : d ,
                            ignoreFieldChange : true
                        });
             
                
			  newDate = newLead.getValue({fieldId:'custentity12'});
			  }
			  log.debug('date updated',newDate);
			
		
		
		}
      }catch(errorMain){
      log.debug({
          title: 'Error in afterSubmit() Function', 
          details: errorMain
        });
    }
  }

  return {
      afterSubmit: afterSubmit
  };
});
this is my code
b
you dont save the record
n
In the User event do we need to save record?
p
you do in aftersubmit, hence my previous guess
b
there are 2 save related events on user event scripts
beforeSubmit which occurs before the record has been saved in the database
and afterSubmit, which occurs after the record has been saved in the database
in beforeSubmit, you can make changes to the newRecord without saving, since netsuite hasnt saved the record yet
in afterSubmit, you need to load the record again and save is since netsuite has already saved the record
n
how can i save it again in afrersubmit?
b
avoid making changes to the current record in afterSubmit, it will esentially double the time required to save the record
you can save it again in afterSubmit if you dont care about performance
n
how can i save it again in the aftersubmit?
p
Do it in beforeSubmit instead, if logic permits
b
i agree with pnj, do it beforeSubmit
you can take a look at the documentation for beforeSubmit and afterSubmit and best practices for more details
n
it won't work as per my logic in beforeSubmit since I use OldVal and NewVal of a field present on that form it will only work in aftersubmit for checking old and new values of a field!
b
the old record and the new record both exist in beforeSubmit
👍 1
if you think you still need to save the record in afterSubmit, go through the docs for the N/record module to see how to work with records.
n
if the old record and new record exist then my code should work if only I just change the afterSubmit name to beforeSubmit right?
b
it will work in the sense of no errors
it will not do what you want it to do
you need to rewrite your code