Copying records and inheriting views

bonsai
Mega Sage

I am copying records using the following script.

However, the new record form that appears after copying is the default view.

Is it possible to copy a record while maintaining the view?

I would like it to have the same functionality as copying an incident record.

<Requirements>

- Display a new record form with the specified fields copied
- Display the new record form with the view before copying

 

var new_rec = new GlideRecord("incident");
new_rec.initialize();
new_rec.setValue("u_field1", current.getValue("u_field1"));
new_rec.setValue("u_field2", current.getValue("u_field2"));
action.openGlideRecord(new_rec);
3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@bonsai 

view is just to display the form fields and copying/inserting is a database backend logic

Both are different things.

but you can try to do something but I haven't tested this

// Server-side UI Action script

// Get the current view
var view = gs.action.getGlideURI().get('sysparm_view') || 'default';

// Build the redirect URL with field values and view
var url = '/incident.do?sys_id=-1' +
    '&u_field1=' + encodeURIComponent(current.u_field1) +
    '&u_field2=' + encodeURIComponent(current.u_field2) +
    '&sysparm_view=' + encodeURIComponent(view);

// Redirect to the new record form with the same view and pre-filled fields
action.setRedirectURL(url);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@bonsai 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

 

 

// Server-side UI Action script

// Get the current view
var view = gs.action.getGlideURI().get('sysparm_view') || 'default';

// Build the redirect URL with field values and view
var url = '/incident.do?sys_id=-1' +
    '&short_description=' + encodeURIComponent(current.short_description) +
    '&caller_id=' + encodeURIComponent(current.caller_id) +
    '&sysparm_view=' + encodeURIComponent(view);

// Redirect to the new record form with the same view and pre-filled fields
action.setRedirectURL(url);

Thank you for your reply.
But unfortunately this didn't work.