<@U5TF1GQ20> Yes i want to start with a scheduled ...
# suitescript
h
@battk Yes i want to start with a scheduled script that updates a single sales order record
b
asking the same question without providing additional information means i just point you at the previous answer: https://netsuiteprofessionals.slack.com/archives/C29HQS63G/p1616021676057100?thread_ts=1616019482.054300&amp;cid=C29HQS63G
h
@battk i am working inn SS 1.0. using nlapiLoadRecord to load a specific record.is it Ok??.and guide me how to put date into that specific field of specific record
h
@battk thanks
b
its usually favorable performancewise to use nlapiSubmitField to update body level fields
h
function updateField() { if(type == 'scheduled') { var context = nlapiGetContext(); nlapiLogExecution('Debug', 'updateField', 'ScheduledScript Started!'()); var rec = nlapiLoadRecord('salesorder', 36209); var currentDate = sysDate(); // returns the date var currentTime = timestamp(); // returns the time stamp in HHMMSS var currentDateAndTime = currentDate + ' ' + currentTime; rec.nlapiSetFieldValue('memo', currentDateAndTime); nlapiLogExecution('DEBUG', 'Schedule Script', currentDateAndTime); nlapiSubmitRecord(rec, true); } } function sysDate() { var date = new Date(); var tdate = date.getDate(); var month = date.getMonth() + 1; // jan = 0 var year = date.getFullYear(); return currentDate = month + '/' + tdate + '/' + year; } function timestamp() { var str = ""; var currentTime = new Date(); var hours = currentTime.getHours(); var minutes = currentTime.getMinutes(); var seconds = currentTime.getSeconds(); var meridian = ""; if (hours > 12) { meridian += "pm"; } else { meridian += "am"; } if (hours > 12) { hours = hours - 12; } if (minutes < 10) { minutes = "0" + minutes; } if (seconds < 10) { seconds = "0" + seconds; } str += hours + ":" + minutes + ":" + seconds + " "; return str + meridian; }
@battk plz have a look at this
i have to only update one field with current date/time.i am using nlapiSubmitRecord for this
s
what’s the issue? If there is a problem (the code doesn’t work, or gives an error) then you should says that as well. otherwise, one might assume the code above is functioning as intended.
b
you have a bunch of date formatting code that i doubt is really necessary
💯 1
if you really needed it, moment js would probably have been more appropiate
if(type == 'scheduled')
is inappropiate
s
Also, what is the use case for setting Memo on a Sales Order to be the current date/time in a scheduled script?
b
it will work because there is an undocumented global with that name, but you should be comparing it against a parameter from the updateField function
h
@scottvonduhn just to test through a schedule script
b
for a simple test, i would have simply gone with new Date().toISOString()
👍 1
💯 1
h
if(type == 'scheduled') is inappropriate then what should i write.
s
You don’t need to check if the type of a scheduled script is scheduled, unless you are using this script file for multiple Script records (which is entirely possible in SS 1.0)
It’s just extra code
👍 1
👎 1
h
ok got it
it serves a purpose
but you are referring to a global variable that you dont control
you should instead be using the parameter to your function
👍 1
s
Is updateField your entry point function?
h
@scottvonduhn yes it is with a parameter (type)
s
function updateField()
has no input parameter, though
h
what did you suggest me the solution?
s
Solution to what problem? I think you should take a step back and perhaps explain what you are trying to accomplish (in plain words, not in code). It always helps to understand the problem before writing any code. I’m not clear on what the goal is.
h
@scottvonduhn i want to write a schedule script that runs every 15 mins and updates the memo field of sales order form with the current time of a single sales order test record.
s
I assume this is just for practice, then? I don’t see much business application for anything like this.
h
@scottvonduhn yes it's for practice only
s
Also, are you absolutely set on using SuiteScript 1.0? If you are just learning the API (which it sounds like you are) I would not invest your time in an older/deprecated API like 1.0, unless you are working for a company that uses it heavily.
But, regardless of the version you use, you should start from a good template of a Scheduled Script. If you are using the SuiteCloud IDE, it can generate a base template for you, which creates a function with the correct input parameters. It is especially helpful when you are getting started.
h
@scottvonduhn actually i am learning and practicing this to work in future with a company that uses SS 1.0
@scottvonduhn the above code which i shared is useless then?
s
Well, no, I never said it was useless.
h
Ok, then what is the problem with that
s
Wait, take a step back.
h
Ok
s
There are really two issues here: your script, and your overall goal to learn SuiteScript 1.0
h
actually i am learning on my own, so i came here to have some directions
s
Getting a script to work, especially if it’s with a lot of help from others and you don’t understand what is wrong with the code, doesn’t necessarily mean you have learned how the API works, or how to write effective Suite Scripts in the future
That is perfectly fine, but the very first thing you should do is to understand the different types of suite scripts and their entry point functions.
Secondly, when it comes to a script not working, you need to know how to debug it. There is a SuiteScript debugger, and you should definitely learn how to use it to test code and set breakpoints / inspect variables.
h
Ok fine.if you have any link from where i can study then it will be helpful for me
s
Basically, SuiteAnswers and the NetSuite help center are the very best resources. Keep in mind, very little documentation and examples are being added for Suite Script 1.0 anymore. It is truly a deprecated API for almost everything (except for Plug-in scripts, which still require it). As long as you understand that, and are certain that the company you want to get a job at is definitely using the very old API, and not anything newer, that’s fine. The majority of NetSuite developer jobs would expect some exposure to the newer Suite Script API’s, though.
Back to your script at hand: What is wrong with it? Does it give an error? Does it not run?
When I saids that the type check was extra code, it does in fact serve a purpose, but since this is a learning exercise, it’s just additional complexity that you don’t need to worry about yet.I would stick to the basics, a simple script with a few lines of code. My guess is, for your learning purposes, you aren’t really trying to make sure your script only runs in certain contexts and not others. Again, if you are starting out and new to SuiteScript entirely, keep things as simple as possible, and only add conditional logic if you are certain of what you are trying to do with it. Remember that any code inside an if statement may not run at all if the condition is false, so you should be certain that is what you intend when writing it.
h
ok
@scottvonduhn it did not give any error when i tested in debugger
s
In other words, checking if the type i scheduled doesn’t make this a scheduled script. It’s a way to prevent the code from running when it is triggered in different ways. for example, if you are using the Save & Execute button, then type will not be equal to
scheduled
, it will be
userinterface
instead.
b
my objection to type is that you are ignoring your editor's warning about type not being defined
h
@battk what about userinterface, if i add parameter.
s
Yes, and as @battk has said, you are using a global variable, instead, you want to use the entry point function input parameters.
Again, unless you know what the type variable is, or how it gets set, why are you even using it? It is potentially a source of failure here. Do away with it.
h
ok i am removing it.