+ Start a Discussion
Mike Tol 1Mike Tol 1 

A simple concatenation question

Hi everyone!
I have contact table and need to get Id + Name as concatenation of Id '0032v000034aSJyAAM' and string 'Name' like on the image.

User-added image

Please tell me how can I do that.

Here is code:

Html:
<template>
    <lightning-card title="Contacts">
        <lightning-layout multiple-rows="true" vertical-align="end">
           
            <lightning-layout-item size="12" padding="around-small">
                <lightning-datatable key-field="id" data={contacts} columns={columns}></lightning-datatable>
            </lightning-layout-item>
           
        </lightning-layout>
    </lightning-card>
</template>

Js:
import { LightningElement , wire} from 'lwc';
import getContacts from '@salesforce/apex/Qqq.getContacts';
let N = 'Name';
const columns = [    
    { label: 'idName', fieldName: 'Id' + N, type: 'text' },
    { label: 'Name', fieldName: 'recordLink', type: 'url',
    typeAttributes: {label: {fieldName: "ContactName"}, tooltip: "Name", linkify: true} }  
];
export default class ContactTable extends LightningElement {
    columns = columns;
    contacts;
   
    @wire(getContacts)
    wiredContacts(value) {
        const {error, data} = value;
        if (data) {
            let contactData = JSON.parse(JSON.stringify(data));  
            contactData.forEach(record => {
                                record.recordLink = "/" + record.Id;  
                                record.ContactName = record.Name;
                        });  
            this.contacts = contactData;
        } else if (error) {
            this.error = error;
        }
    }
}

Apex:
public with sharing class Qqq {
    @AuraEnabled(cacheable=true)
    public static List<Lead> getLeads() {
        List<Lead> leads = [
            SELECT Id, Name, Title, Phone
            FROM Lead         
            WHERE Name Like 'Lisa%'
            Limit 3
        ];
        System.debug('leads = ' + leads);
        return leads;                
    }   
    
    @AuraEnabled(cacheable=true)
    public static List<Contact> getContacts() {
        List<Contact> contacts = [
            SELECT Id, Name
            FROM Contact     
            //WHERE LastName Like 'Apex%'
            Limit 3            
        ];        
        System.debug('contacts = ' + contacts);
        return contacts;    
    }   
}

 
Best Answer chosen by Mike Tol 1
ravi soniravi soni
hi ,
try below code.
import { LightningElement , wire} from 'lwc';
//import getContacts from '@salesforce/apex/Qqq.getContacts';

import getContacts from '@salesforce/apex/fetchLead.getContacts';

let N = 'Name';
const columns = [    
    { label: 'IdName', fieldName: 'IdWithName', type: 'text' },
    { label: 'Name', fieldName: 'recordLink', type: 'url',
    typeAttributes: {label: {fieldName: "ContactName"}, tooltip: "Name", linkify: true} }  
];
export default class Test extends LightningElement {
   columns = columns;
   contacts;
   
    @wire(getContacts)
    wiredContacts(value) {
        const {error, data} = value; 
        if (data) {
            let contactData = JSON.parse(JSON.stringify(data));  
            contactData.forEach(record => {
                                 record.IdWithName =  record.Id + record.Name;//If you want to display Id with contact name
                                //record.IdWithName =  record.Id + 'Name';//If you want to display Id with Custom  Name
                                record.recordLink = "/" + record.Id;  
                                record.ContactName = record.Name;
                        });  
            this.contacts = contactData;
        } else if (error) {
            this.error = error;
        }
    }
}
================================================
<template>
    <lightning-card title="Contacts">
        
        <lightning-layout multiple-rows="true" vertical-align="end">
           
            <lightning-layout-item size="12" padding="around-small">
                <lightning-datatable key-field="id" data={contacts} columns={columns}></lightning-datatable>
            </lightning-layout-item>
           
        </lightning-layout>
    </lightning-card>
</template>
=========================================

don't forget to mark it as best answer.
Thank you​​​​​​​

All Answers

mukesh guptamukesh gupta
HI Mike, 

Please below code:-

.JS
 
import { LightningElement , wire} from 'lwc';
import getContacts from '@salesforce/apex/Qqq.getContacts';
const columns = [    
    { label: 'idName', fieldName: 'IdName', type: 'text' },
    { label: 'Name', fieldName: 'recordLink', type: 'url',
    typeAttributes: {label: {fieldName: "ContactName"}, tooltip: "Name", linkify: true} }  
];
export default class ContactTable extends LightningElement {
    columns = columns;
    contacts;
   
    @wire(getContacts)
    wiredContacts(value) {
        const {error, data} = value;
        if (data) {
            let contactData = JSON.parse(JSON.stringify(data));  
            contactData.forEach(record => {
                                record.recordLink = "/" + record.Id;  
                                record.ContactName = record.Name;
								record.IdName = record.Id+record.Name;
                        });  
            this.contacts = contactData;
        } else if (error) {
            this.error = error;
        }
    }
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

 
ravi soniravi soni
hi ,
try below code.
import { LightningElement , wire} from 'lwc';
//import getContacts from '@salesforce/apex/Qqq.getContacts';

import getContacts from '@salesforce/apex/fetchLead.getContacts';

let N = 'Name';
const columns = [    
    { label: 'IdName', fieldName: 'IdWithName', type: 'text' },
    { label: 'Name', fieldName: 'recordLink', type: 'url',
    typeAttributes: {label: {fieldName: "ContactName"}, tooltip: "Name", linkify: true} }  
];
export default class Test extends LightningElement {
   columns = columns;
   contacts;
   
    @wire(getContacts)
    wiredContacts(value) {
        const {error, data} = value; 
        if (data) {
            let contactData = JSON.parse(JSON.stringify(data));  
            contactData.forEach(record => {
                                 record.IdWithName =  record.Id + record.Name;//If you want to display Id with contact name
                                //record.IdWithName =  record.Id + 'Name';//If you want to display Id with Custom  Name
                                record.recordLink = "/" + record.Id;  
                                record.ContactName = record.Name;
                        });  
            this.contacts = contactData;
        } else if (error) {
            this.error = error;
        }
    }
}
================================================
<template>
    <lightning-card title="Contacts">
        
        <lightning-layout multiple-rows="true" vertical-align="end">
           
            <lightning-layout-item size="12" padding="around-small">
                <lightning-datatable key-field="id" data={contacts} columns={columns}></lightning-datatable>
            </lightning-layout-item>
           
        </lightning-layout>
    </lightning-card>
</template>
=========================================

don't forget to mark it as best answer.
Thank you​​​​​​​
This was selected as the best answer
Mike Tol 1Mike Tol 1
Thank you mukesh!
Thank you ravi!
I would be very grateful if you could more help me solve this problem:
https://developer.salesforce.com/forums/ForumsMain?id=9062I000000Ucm9QAC