I have a button that copies a credit memo and then...
# suitescript
b
I have a button that copies a credit memo and then sends the user to the newly created CM in edit mode like so:
var sendUser = url.resolveRecord({
recordType: 'creditmemo',
recordId: cmId,
isEditMode: true
});
window.location = sendUser;
works great, the problem is when the user saves the new Credit Memo they are redirected back to the one that they pressed the copy button on, any idea how I can prevent that redirect?
a
Copy code
window.location.href
b
How do I use window.location.href to solve the problem?
a
window.location.href = sendUser; Try it.
b
thank you, I tried that with the same result
c
Maybe a context guard to only perform the redirect on a copy action
b
hi creece, I am not sure what you mean by that?
In the client script function that the button points to, I do record.copy and then save the new CM so that I can get the URL using the new CM id then redirect, so when they do their final edits and click save, it is no longer in the copy context
c
Sorry i thought you were doing the redirect.
I believe what alien was saying is redirect the user in the UE back to the same record its on? Your script creates the Credit Memo and then redirects to an already created record. If you add a redirect in the UE of the Credit Memo to just redirect back to the record, that should solve it.
a
@barinvon Why not using the native copy function?
c
Doesnt' exist for credit memos
💯 1
mindblow 1
b
use the params parameter to set the whence query parameter
the whence query parameter controls which url to redirect to after saving the record
c
Its more of a process question IMO. Once you've created a credit memo, its there. Credit memos can mess w/ inventory so its already in a bad spot if the data isn't correct.
a
@barinvon You should not need this but this would solve your problem, there may be a better/easier way: • UserEvent Script deployed to the Credit Memo record. • Triggering only on context.type EDIT
Copy code
redirect.toRecord({
    type: 'creditmemo',
    id: scriptContext.newRecord.id,
});
When the user saves the record it would be redirected to the record being saved.
c
That solves the issue of the redirect but the root issue of creating a bad Credit Memo is still there.
It just smells funny. There should be a better process for it.
1000 1
b
Customer wants to be able to copy a credit memo with a button and edit it immediately in a window, if it were possible I would not save it before redirecting them but can't get the url without CM id.
@battk what would using the whence query parameter look like?
url.resolveRecord({
recordType: 'creditmemo',
recordId: cmId,
isEditMode: true,
params: { 'whence': [what would go here?] }
});
a
@barinvon Another approach you may want to try is to redirect the user via a Suitelet: • Client Script resolve the URL of a Suitelet and do http.get with a parameter sending the credit memo id, the suitelet creates the copy and re-direct the user to the new record in EDIT mode.
b
use url.resolveRecord twice'
once to get the url to redirect to
another to get the url to redirect to after the record is saved
theoretically you can get it once if you are confident you know how to properly escape a query parameter
c
You can also just redirect to a new credit memo w/ some parameters filled out. Its not documented this way but the ID is optional:
require(['N/url', 'N/record'], function(url, record) {
function doRedirect() { var redirectUrl = url.resolveRecord({ recordType: record.Type.CREDIT_MEMO, isEditMode: true, params: { entity: 4779 originalCreditMemoId: 12345 } }); window.open(redirectUrl); } doRedirect(); });
b
I would still need to copy all the fields from the original CM over
c
You can pass the original credit memo you are copying ID to the redirected page, load the credit memo (read the url params and get the ID) and copy whatever you need field wise.
Creating the credit memo and THEN editing is a bad idea
b
if you didnt actually want to save your record, then you should take a look at how netsuite generates the url to copy a record
and do the same thing to your credit memo's url
c
If you resolve the record you want to copy and add "&whence=&e=T&memdoc=0" that should put you into a new Credit Memo that copies the original and is in edit mode and hasn't been saved yet
var sendUser = url.resolveRecord({
recordType: 'creditmemo',
recordId: cmId,
isEditMode: true
});
sendUser += '&whence=&e=T&memdoc=0";
window.location = sendUser;
b
bad @creece, dont teach him to treat the url as a string
your example sends 2 e query parameters
c
you can't copy credit memos like normal transactions so you're kinda limited here
b
use the params parameter in resolveRecord, or at least use URLSearchParams
c
I tried and couldn't get it working but i didn't spend much time tryin to get it to work correctly w/ params. You're right though.