0

I am new to Salesforce. Now I am trying to solve this problem. I want to open it for viewing by clicking on the name of the contact. I did not find such an example in the documentation. Hope it is possible to implement it. Here's what I've managed to do so far: I made a data type like link, but I don't know how I can determine the path that I should follow in order to open the object for viewing by clicking on the link? For example, when I click on the name of the first contact on the screenshot, I follow this link jack%20johns.

enter image description here

How can I get into contacts, i.e. over here?

enter image description here

Here is the code for my init method:

init : function (component, event, helper) {
    component.set('v.columns', [
       { label: 'Name', fieldName: 'Name', type: 'url', typeAttributes: {label: {fieldName: 'Name'}}, sortable: true  },
       { label: 'Email', fieldName: 'Email', type: 'email', sortable: true  },
       { label: 'Contact Level', fieldName: 'Contact_Level__c', sortable: true  },
    ]);
    helper.getDataFromDB(component, helper);
},
3

You need to synthesize an attribute in your data with the record URL desired, and use that instead of Name as the fieldName for this column. Your label field remains Name to present that to the user.

I wrote a post about this long ago. Here's the summary:

When you load your data, you do something like this to add a URL field to each record:

records.forEach(function(item) {
    item['URL'] = '/lightning/r/Contact/' + item['Id'] + '/view';
}

and then you configure your columns to consume that data:

{
    label: 'Name', 
    fieldName: 'URL',
    type: 'url', 
    typeAttributes: { 
        label: {
            fieldName: 'Name'
        },
        target: '_self'
    },
    sortable: true 
}

Then you get the Name displayed, and a click takes you to the right page for the record.

You could also generate the URLs with the Lightning navigation service if you don't want the structure hard-coded in your component. That might add a bit of latency in the UI; it's worth checking.

3
  • Small world! I just posted an answer with a link to your article at the same time you did. @unique user, this should be the accepted answer rather than mine – Nathanael Schmolze Mar 24 at 15:19
  • Cool!!! You helped me a lot, this is a very useful knowledge. There is also a small clarification: now my sorting in the table is a little broken, because does it work by ID instead of Name? I understand correctly? – unique user Mar 24 at 16:04
  • Yeeesss, I think it's probably sorting by the field value rather than the label, so you may want to make this column non-sortable. – David Reed Mar 24 at 16:49
0

I haven't personally used datatable, but from what I'm reading online, it seems that the fieldName specified in the top-level column object should be a field with the URL path in it, whereas typeAttributes.label.fieldName should be the name of a field containing the link's human-readable text.

In your code, the 'Name' field is being provided for both of these. To correct this, you'd need to replace the first fieldName with a field containing the URL. This could be a custom formula field, or (according to the following documentation) even a non-existent field that you "create" in JavaScript on the client side:

https://www.ktema.org/2018/10/22/lightning-data-table-record-link/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.