- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2023 06:40 AM
I am creating a UI Action to duplicate a record. Is there a way to automatically capture all of the field names on a form so that I don't have to hard-code the field names? I would like to use this on multiple forms and don't want to go through each form and change the field names.
For example, this is a snippet of my current code:
var newUser = new GlideRecord("x_1234567_users");
newUser.newRecord();
newUser.first_name = current.first_name;
newUser.last_name = current.last_name;
action.openGlideRecord(newUser );
-----------------------
My idea would be to loop through each field and do newUser.(fieldname) = current.(fieldname);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2023 07:08 AM
something like this and you should exclude system fields
var newUser = new GlideRecord("x_1234567_users");
newUser.initialize();
var fields = current.getFields();
for(var i=0; i<fields.size(); i++) {
var field = fields.get(i);
var descriptor = field.getED();
var fieldName = descriptor.getName();
var fieldLabel = descriptor.getLabel();
if(fieldLabel != '' && !fieldName.toString().startsWith('sys')){
newUser[fieldName] = current[fieldName];
}
}
newUser.insert();
action.openGlideRecord(newUser);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2023 07:08 AM
something like this and you should exclude system fields
var newUser = new GlideRecord("x_1234567_users");
newUser.initialize();
var fields = current.getFields();
for(var i=0; i<fields.size(); i++) {
var field = fields.get(i);
var descriptor = field.getED();
var fieldName = descriptor.getName();
var fieldLabel = descriptor.getLabel();
if(fieldLabel != '' && !fieldName.toString().startsWith('sys')){
newUser[fieldName] = current[fieldName];
}
}
newUser.insert();
action.openGlideRecord(newUser);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2023 07:27 AM
Thank you, this is exactly what I was looking for. Only issues is that I receive a "Function getFields is not allowed in scope ...".
Is there any workaround for this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2023 07:30 AM
I don't think
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader