Can anyone spot the bug? It's attaching 1-4 but no...
# suitescript
r
Can anyone spot the bug? It's attaching 1-4 but not 5. There are 5 pdfs named 0.pdf, ... 4.pdf. The last one is not attaching. Sending 200 OK. It is creating the pdf and saving it in the file cabinet.
a
you're getting 5 logs for each log in the forEach?
or i guess presumably you're missing the last one for "Attached" ?
r
No, there are 5 messages saying attached. Only 4 are attaching
a
wait is that what the //x5 means? okay wth
r
Tried with another record that had 6 and its attaching 5
a
and no issue with manually attaching that one file afterwards manually?
r
nope
a
its not the code
like there's not an error in the code
m
Try without the promise
a
if you weren't getting that last log I'd say the promise wasn't working correctly and it was returning before actually attaching the last one... but if you're getting the .then logs ... i have no idea I'd probably still just wanna try adding a delay in there
m
Or map the pdf's to promises and use Promise.all()
☝️ 1
r
broken promises smh
😂 2
m
There isn't really any advantage to using promises for SuiteScript API calls and possibly even degrades performance
r
Promise.all() just made them show up scrambled on the record. Still only showing 4 of 5. Same result without promises.
Verified via saved search that the fifth is not being attached as well.
m
Copy code
pdfs.forEach((pdf, index) => {

	const fileId = file.create({
		name: `${index}.pdf`,
		fileType: file.Type.PDF,
		contents: pdf.getContents(),
		description: `Request for Quote for ${index}`,
		folder: getRFQFolder()
	}).save();
	
	record.attach({
		record: {type: 'file', id: fileId},
		to: {type: 'customrecord_request_for_quote', id: internalId}
	});
	log.debug({title: 'File', details: 'Attached'}); // x5
});
You mean this only attached 4 files but you got 5 logs?
r
yes
always the last one that doesn't get attached
tried with other records with more or less pdfs
a
your account is haunted ¯\_(ツ)_/¯
🤭 1
r
There is some kind of timing issue going on. If I add arbitrary processing time (load the record and get values again) before writing to the response, it succeeds. what the actual hell
a
probably some kind of disconnect between the GraalVM promise and what Java's actually doing to the database? that stuff is just a black box, add the record load (probably don't even need to get values again), and only do it after the forEach loop dont do it for all iterations
👍 1