The CreatorCon Call for Content is officially open! Get started here.

how to concatenate two reference fields in string field?

alvatindra
Giga Contributor

Can two reference fields are concatenated? If so how this can be achieved?

4 REPLIES 4

The SN Nerd
Giga Sage
Giga Sage

If your reference fields are the same you can use a List field that references the desired table.


This will allow you to store multiple records in one field.



The watch list field is an example of this, where multiple users can be added.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

I guess what I'm getting at is there may be a better technical solution than just concatenating two sys_id's in a string field.


What outcome are you actually trying to achieve here? Have you considered using a List field?



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

veeresh3
Tera Expert

Hi,



Take two values of the reference field. and convert it to String using toString() function.



  concatewith string inbuilt function or '+' and assign it to the one more string variable.



Example:


var string1;


var string2=<reference fieldname>.toString();


var string3=<reference fieldname>.toString();


var string1=string2+string3;//or var string1=concate(string2,string3);//please check the cancate function syntax.



Thanks,


Veeresh


Please mark as correct , if it's helpfull.


mayank_goyal_at
Kilo Explorer

instead of toString(), we can use getDisplayValue() function in a very similar fashion.

 

var final_str = ref_field_1.getDisplayValue() + ":" +  ref_field_2.getDisplayValue() ;

In a table, if you want to concatenate 2 fields of reference type into a new field with string type

- Add a new field for eg. Full Name

- edit this field and got to Calculated value tab

- Put this script there

(function calculatedFieldValue(current) {


var fullname = current.u_first_name.getDisplayValue() + ':' + current.u_last_name.getDisplayValue() ;

return fullname;

})(current);