`record.create.promise` lists compatibility with o...
# suitescript
m
record.create.promise
lists compatibility with only Client Scripts. However in testing it works in user event scripts
afterSubmit
. Does anybody have insight into this?
r
It works but there is no difference between with promise and without promise in a server side script.
m
It seems to work differently in testing, because what I was testing was the creation of multiple record and when using a loop to create them consecutively and then moving to a loop
create.record.promise
/
await Promise.all(promises)
there is a dramatic speedup in the logs. From averaging 5 seconds to within the same second.
b
you can use them serverside, and they will act like promises in that your code will run asynchronously
they will also suffer greatly in that the normal error handing in scripts (system level logs for errors and email notifications) will not work
m
Am I understanding that they will run asynchronously but the await Promise.all() might not properly be catching failures or waiting? Do you know if the specifics are documented anywhere?
b
Promise.all doesn't catch any errors by itself. Its just that more normal runtimes will tell you when a promise is rejected at the top level
for example, most environments would have an error logged in the console for the following code
Copy code
Promise.reject(new Error("im an error"))
serverside suitescript will not do so. Nor will it follow the normal process of system level logs for uncaught errors or email notifications
m
I think I get what you mean now.
Copy code
const results = await Promise.all(createPromises);
log.debug({
                title: "scriptname->afterSubmit->results",
                details: `Successfully created ${results.filter((r) => r).length} records out of ${createPromises.length} attempts`
            });
This would always report
n
of
n
the same regardless of failures?
b
i dont know what your create promise function looks like, but it would only ever log n out of n if it was all successful and would never log otherwise since netsuite doesnt handle the async error gracefully
though to be honest, that code wouldnt work in other javascript environments. it would reject at the first error and you would never know the proper count
m
Thanks! This gives me some test cases to do so I can better understand how promises are handled server side. It does sound like it provides an acceptable trade-off in this particular use case though.
Our create is pretty basic.
Copy code
const typePromise = record.create
                    .promise({ type: "customrecord_recordtype" })
                    .then(function (type_record) {
                        type_record.setValue({ fieldId: "custrecord_customfield", value: customer_id });
                        return type_record.save.promise({ ignoreMandatoryFields: true });
                    })
                    .catch(function (e) {
                        log.error({ title: "Error creating Record Type", details: `customer: ${customer_id}, Error: ${e.message}` });
                    });

                createPromises.push(typePromise);
b
my advice is to actually test unexpected cases, for example, creating so many records that you run out of points