- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2022 07:41 AM
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)
Please Advise on this.
Many Thanks,
Ashwin Umapathi
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2022 08:04 AM
Hi,
make the 3rd field as calculated type
current.name + ' - ' + current.full_name;
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2022 07:53 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2022 07:55 AM
Hi
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.
Mark my answer correct & Helpful, if Applicable.
Thanks,
Sandeep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2022 08:04 AM
Hi,
make the 3rd field as calculated type
current.name + ' - ' + current.full_name;
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader