UI Action - Capture all fields on a form

ChadLee4
Tera Contributor

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); 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ChadLee4 

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.

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@ChadLee4 

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.

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

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?

@ChadLee4 

I don't think

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