Concatenation of two fields in a table

Ashwin Umapathi
Tera Contributor

Hi All,

I need to concatenate 2 fields in a location table and copy the concatenated value to a custom field on the same table. So in future if those tables has a new value automatically the concatenated value should be updated to the custom field.

Below screen shot   Name + Full Name = Display Name(with a - between them)

find_real_file.png

Please Advise on this.

 

Many Thanks,

Ashwin Umapathi

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

make the 3rd field as calculated type

current.name + ' - ' + current.full_name;

find_real_file.png

For existing records you can run fix script

updateRecords();

function updateRecords(){
	try{
		var gr = new GlideRecord('tableName');
		gr.addEncodedQuery("thirdFieldISEMPTY");
		gr.query();
		while (grIncident.next()) {
			gr.thirdField = gr.name + " - " + gr.full_name;
			gr.setWorkflow(false);
			gr.update();
		}
	}
	catch(ex){
		gs.info(ex);
	}
}

Regards
Ankur

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

View solution in original post

3 REPLIES 3

Voona Rohila
Kilo Patron
Kilo Patron

Hi Ashwini

You can either use default calculated value in dictionary for your custom field

current.name + " - " + current.full_name;

or

You can write a before insert/update BR with below code (Include trigger conditions for your BR)

current.u_display_name = current.name + " - " + current.full_name;

 

For existing records you need to write a FixScript or background script.

//modify table,field names accordingly.
var gr= new GlideRecord('table');
gr.addEncodedQuery("u_display_nameISEMPTY");
gr.query();
while (grIncident.next()) {
gr.u_display_name = gr.name + " - " + gr.full_name;
gr.update();
}

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Community Alums
Not applicable

Hi @Ashwin Umapathi ,

You can use these scripts or approaches:

1. answer is already  a  reserved variable so you don't need to declare it .

answer = (function transformEntry(source) {

// Add your code here

var abc =source.fieldA;

var abc1 = source.fieldB

var op = abc+'- '+abc1;
return op; // return the value to be put into the target field

})(source);

 

 

2.

You can use calculated value. See the dictionary of Name on the sys_user table for reference.

 

find_real_file.png

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

make the 3rd field as calculated type

current.name + ' - ' + current.full_name;

find_real_file.png

For existing records you can run fix script

updateRecords();

function updateRecords(){
	try{
		var gr = new GlideRecord('tableName');
		gr.addEncodedQuery("thirdFieldISEMPTY");
		gr.query();
		while (grIncident.next()) {
			gr.thirdField = gr.name + " - " + gr.full_name;
			gr.setWorkflow(false);
			gr.update();
		}
	}
	catch(ex){
		gs.info(ex);
	}
}

Regards
Ankur

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