2

I am trying to create case table in Community with first column which will show Case Number and will be link to record page.

I created formula field Show_Case__c [formula: "/"&id]. Also I reference it :

 {label: 'Case Number', fieldName: 'Show_Case__c', type: 'url',sortable: true,typeAttributes: {label: { fieldName: 'CaseNumber'}}}

Component

<aura:attribute name="columns" type="List"/>
<aura:attribute name="data" type="Object"/>

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

<aura:if isTrue="{!not(empty(v.columns))}">
    <lightning:datatable aura:id="myTable" columns="{!v.columns}" data="{!v.data}" keyField="Id"  hideCheckboxColumn="false"  />
</aura:if>

Apex

@AuraEnabled
public static List<Case> getCases1(){
    List<Case> cases = [SELECT  Show_Case__c, Id, CaseNumber FROM Case];
    return cases;
}

JavaScript

tableset : function(component,event,helper) {

        var queryaction = component.get("c.getCases1");
         queryaction.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {

                component.set('v.data',response.getReturnValue());

                var columns = [{label: 'Show_Case__c', fieldName: 'CaseNumber', type: 'url',sortable: true,filterable: true,typeAttributes: {label: { fieldName: 'CaseNumber'}}}];
                component.set('v.columns',columns);
            }


        });
        $A.enqueueAction(queryaction); 
    }, 

The error which I receive is Cannot read property 'type' of undefined.

I used Hyperlink a Record in lightning:datatable for reference.

How to get this work?

I will appreciate any help. Thank you!

2
  • Can you provide few extra details like code snippet? Is it exact replica of code you mentioned above? When do you receive this error on load or click of link? Does it provide you with stack trace? – Pranay Jaiswal May 3 '18 at 10:39
  • I receive the error on load. I can see from console.log the fields are passed with correct values as shown in the reference link. If I change the places It is showing the id, but not the number as well as it is not working on click of the link. (of course) – Jane May 3 '18 at 10:47
1

I found the way to show case number in community as link to record detail!

  1. Create Custom formula field.

Formula:

LEFT($Api.Partner_Server_URL_260, FIND( '/services', $Api.Partner_Server_URL_260)) 
+ 's/detail/' + Id]
  1. Put the field in Apex SOQL query

  2. Then this code will be working

{ label: 'Case Number', fieldName: 'Show_Case__c', type: 'url', sortable: true,typeAttributes: {label:{ fieldName: 'CaseNumber'}} }

0

I hope the issue is somewhere else. I tried the same code and it is working for me. Another thing I noticed, you don't have to explicitly parse the JSON (JSON.parse(JSON.stringify(response.getReturnValue()))).

You can set the response directly cmp.set('v.mydata', response.getReturnValue()); in the datatable.

Component Markup

<aura:component controller="lightningTableController" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:appHostable" access="global">
    <aura:attribute name="title" type="string" default="My Cases" />
    <aura:attribute name="mydata" type="Object"/>
    <aura:attribute name="mycolumns" type="List"/>
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>    
    <lightning:card title="{!v.title}">
        <lightning:datatable data="{!v.mydata}" 
                             columns="{!v.mycolumns}" 
                             keyField="Id"
                             hideCheckboxColumn="true"/>
    </lightning:card>
</aura:component>

Component Controller

({
    init: function (cmp, event, helper) {
        var action = cmp.get('c.getCases');
        action.setCallback(this, function (response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set('v.mydata', response.getReturnValue());
                cmp.set('v.mycolumns', [
                    {label: 'Case Number', fieldName: 'Show_Case__c', type: 'url',sortable: true,typeAttributes: {label: { fieldName: 'CaseNumber'}}},
                    {label: 'Subject', fieldName: 'Subject', type: 'text'},
                    {label: 'Status', fieldName: 'Status', type: 'text'}
                ]);                
            } else if (state === "ERROR") {
                var errors = response.getError();
                console.error(errors);
            }
        });
        $A.enqueueAction(action);
    }
})

Apex

public class lightningTableController {
    @AuraEnabled
    public static List<Case> getCases() {
        List<Case> cases = [SELECT Id, CaseNumber,  Subject, Status, Show_Case__c FROM Case];
        //Add isAccessible() check
        return cases;
    }
}

Screen Capture

enter image description here

1
  • For me is working in lightning but not in community – Jane May 3 '18 at 13:24

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.