```async function createOrder(orderBody:any){ ...
# suitescript
k
Copy code
async function createOrder(orderBody:any){
    const { tranid, entity, items } = orderBody;
    const orderRec = record.create({
        type: record.Type.SALES_ORDER,
        isDynamic: true
    });
    orderRec.setValue({
        fieldId: 'tranid',
        value: tranid
    });
    orderRec.setValue({
        fieldId: 'entity',
        value: entity
    });
    items.forEach((item:any, index:number) => {
        orderRec.setSublistValue({
            sublistId: 'item',
            fieldId: 'item',
            line: index,
            value: item.item
        });
        orderRec.setSublistValue({
            sublistId: 'item',
            fieldId: 'location',
            line: index,
            value: item.location
        });
        orderRec.setSublistValue({
            sublistId: 'item',
            fieldId: 'quantity',
            line: index,
            value: item.quantity
        });
    });
    orderRec.save();
}
Is there a way for me to use external id to set a line item?
e
Nope; you have to lookup the
internalid
k
hmm its going to be slow if I am trying to find internal id of 900 line items? and might get limit @erictgrubaugh
e
Shouldn't be too slow if you build the list of all the externalids and use that to filter a single query
k
ohh like pull a saved search of externalID vs Internal
Then use that
?
e
You could either make a Search or a Query. Personally I'd make a query like
Copy code
`SELECT id FROM item WHERE externalid IN (${externalids.join(',')})`
k
ohh, i never used N/query module before, i'll definitely take a look at it
e
The concept would be similar for a search except that
externalid
is a text field, so you need one filter per externalid, then
OR
them all together
k
okay i will try query first , only thing I am worried about is hitting Suitescript limit when its an order with huge amount of datas
e
One search or query can return 1000 results (or more), so if 900 is the max, then you're only spending 10 units
k
👍 okay
e
What script type is this?
k
Restlet
e
So I believe your limits are 5,000 units, 15 seconds of time to connect, and 45 seconds of runtime. I'd be much more worried about hitting the runtime limit than the governance limit with large orders
k
okay, these are the operations my suite script is doing Order comes in from store front (Single order, not batch of order) 1. Check if Order exist 2. If it does not, return 3. if it does, check if customer exist, if it does not create customer 4. Search a list of External vs Internal ID (items) 5. Create Sales object with the mapped informations 6. Create sales order
e
Impossible for anyone else to say how long that will take because your runtime will be impacted by all other Workflows and User Events deployed to the Sales Order record with a Restlet Execution Context
I haven't leveraged
async
in Restlets enough to know whether that will improve things; also consider
orderRec.save.promise()
k
noted, thank you for your feedback
s
the trouble with ending with a
.save.promise()
is the caller doesn't know if the save was successful or not without building other async mechanisms to convey such.
e
Isn't that already true because
createOrder
is
async
?
(I'm assuming the intention there is that the caller does not wait for it to finish)
s
I didn't even notice that Eric but indeed! I guess my point was if you're doing things async and don't wait for things to complete, NS won't wait either - it will finish and not return anything to the caller.
e
I see
async
so rarely, I'm surprised I caught it 🙂 but yes I'm still in agreement with you. You still (likely) need to close the loop with the originator of the request somehow
s
The only use case for those async calls would be if you were maybe creating multiple records at once, not related to one another? I agree - I don't think I've seen a legit use case for
async
in server SuiteScript yet
e
I worked on a team recently wherein we had a time-sensitive restlet running a series of three lengthy queries. We tried to parallelize them with
Promise.all()
and it actually made things slow down. Not to say that will happen in all cases, but I suppose be wary
n
If you offload processes to a SuiteLet and might hit the limits on that SuiteLet (where you are processing large volumes of data), using Promise.all() with chunked data I found made things quicker and allowed me to use governance efficiently on the originating script. Also it was significantly more efficient than looping and calling a SuiteLet many times having to wait for each response before the next call.