how could I print original document when printing first time, and a copy whenever I print after the first time?
Asked
Active
Viewed 117 times
1 Answers
1
When printing a document, a UserEvent is triggered.
You can store a timestamp when it's first printed (or a simple checkbox) on the document.
You can either print the original document or the copy depending on the timestamp/checkbox.
/**
*@NApiVersion 2.x
*@NScriptType UserEventScript
*/
define(['N/record'], function(record) {
function beforeLoad(context) {
var UserEventType = context.UserEventType;
var contextType = context.type;
var newRecord = context.newRecord;
if (contextType == UserEventType.PRINT) {
var fieldId = 'custrecord_is_printed' // fieldId of your custom field / checkbox (or use a datetimestamp)
var isPrinted = newRecord.getValue({ fieldId: fieldId })
if (!isPrinted) {
var myRecord = record.load({ type: newRecord.type, id: newRecord.id }) // in the beforeLoad, editing the newRecord is not allowed, so you need to load the record first, edit and save.
myRecord.setValue({ fieldId: fieldId, value: true })
myRecord.save()
}
}
}
return {
beforeLoad: beforeLoad,
}
});
in your advanced html template use a condition like
<#if record.custrecord_is_printed>
<p>COPY</p>
<#else>
<p>ORIGINAL</p>
</#if>

W.S.
- 1,401
- 1
- 7
- 9
-
Are you familiar with SuiteScript? see edit above. – W.S. Aug 24 '22 at 08:41
-
https://stackoverflow.com/questions/42961018/module-does-not-exist-suitescript – W.S. Aug 24 '22 at 09:57
-
Thank you a lot bro! let me check it out – L.A Aug 24 '22 at 10:29
-
[Look at this error!, the checkbox is checked when i reload the record by editing it and save it and print it but i got this error while i am trying to access the advance template of this transaction (Journal)][1] [1]: https://i.stack.imgur.com/6SscL.png – L.A Aug 25 '22 at 07:01
-
The error says, you're missing a required argument 'id' in line 17 of your script. You can use the N/log module to figure out what's causing the error. – W.S. Aug 25 '22 at 08:55
-
I got it now, but how to check in the template if the checkbox is true or false? – L.A Aug 25 '22 at 09:55
-
In your HTML template just include an if statement, like mentioned in my answer. – W.S. Aug 25 '22 at 10:33
-
I think there is an issue in rendering – L.A Aug 29 '22 at 08:43