function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NyshaaNyshaa 

I created a lightning component but I am unable to fetching the record

I created a Lightning component in which i want to fetch my detail through my apex controller but I am not able to fetch my records on the current record Id.

Apex Controller:
public with sharing class ASDcompanyapex
{
    @AuraEnabled
    public static List<Service_Line__c> getServiceLine(string recordId) 
    {
        List<Service_Line__c> ServiceLineList =[Select Name, Status__c, Line_Type__c, Total_Effort_Taken__c,Total_Line_Cost__c  from Service_Line__c where Status__c = 'Completed' AND Service_Request__c =: recordId];
        {
            
            return ServiceLineList;
            
        }
    }
}
Lightning Component:
<aura:component controller="ASDcompanyapex"  implements="force:appHostable,force:hasRecordId,flexipage:availableForAllPageTypes" access="global">
 
	<aura:attribute name="recordId" type="Id" />
    <aura:attribute type="Service_Line__c" name="ServiceLineList"/>
    <aura:attribute name="mycolumns" type="List"/>
      
    <aura:handler name="init" value="{!this}" action="{!c.servicelist}"/>
      
    <lightning:datatable data="{! v.ServiceLineList }"
                         columns="{! v.mycolumns }"
                         keyField="{!v.recordId}"
                         hideCheckboxColumn="true"/>
      
</aura:component>

Lightning Controller:
({
    servicelist : function(component, event, helper) {
        var rid = component.get("v.recordId");
        
        helper.fetchServiceLineHelper(component, event, helper);
    }
})

Lightning Helper:
({
    fetchServiceLineHelper : function(component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Service Line Name', fieldName: 'Name'},
            {label: 'Status', fieldName: 'Status__c'},
            {label: 'Line Type', fieldName: 'Line_Type__c'},
            {label: 'Total Effort', fieldName: 'Total_Effort_Taken__c'},
           {label: 'Total Line Cost', fieldName: 'Total_Line_Cost__c'},
            
        ]);
            debugger;
        var action = component.get("c.getServiceLine");
        action.setParams({
        });
        action.setCallback(this, function(response){
                        debugger;
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.ServiceLineList", response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})

Help urgently needed.
Thanks in Advance!​​​​​​​​​​​​​​

 

{tushar-sharma}{tushar-sharma}
I suggest you to things here.
  1. Make sure you have data matching the criteria given in your SOQL
  2. As a best practicse you should print error part as well, may be you was getting an exception.
    else if (state === "ERROR") {
                    var errors = response.getError();
                    if (errors) {
                        if (errors[0] && errors[0].message) {
                            console.log("Error message: " + 
                                     errors[0].message);
                        }
                    } else {
                        console.log("Unknown error");
                    }
    If this answer helps you, please mark it as accepted.

    Regards,
    Tushar Sharma
    https://newstechnologystuff.com/
ANUTEJANUTEJ (Salesforce Developers) 
Hi Nyshaa,

I found the below link for the developer documentation can you please have a look at it:

>> https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/apex_records.htm


I hope this was helpful in your implementation and in case if this comes handy can you please choose this as the best answer so that it can be used by others in the future.
NyshaaNyshaa

Hi Tushar

I have the data matching to what I want to display in my component.
I appended the error condition it's not getting saved.
Below is my appended code:
 

({
    fetchServiceLineHelper : function(component, event, helper) {
        debugger;
        var action = component.get("c.getServiceLine");
        var rec = component.get("v.recordId");
        action.setParams({
            "recordId": rec
        });
        action.setCallback(this, function(response){
            debugger;
            var state = response.getState();
            
            debugger;
            if (state === "SUCCESS") {
               
                component.set('v.mycolumns', [
                    {label: 'Service Line Name', fieldName: 'Name'},
                    {label: 'Status', fieldName: 'Status__c'},
                    {label: 'Line Type', fieldName: 'Line_Type__c'},
                    {label: 'Total Effort', fieldName: 'Total_Effort_Taken__c'},
                    {label: 'Total Line Cost', fieldName: 'Total_Line_Cost__c'},
                    
                ]);
                    
                    debugger;
                    var res = response.getReturnValue();
                    component.set("v.data" , res);
                    debugger;
                    }
                    
                    else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
       
            debugger;
                    var res = response.getReturnValue();
                    component.set("v.data" , res);
                    debugger;
            
                    });
                    $A.enqueueAction(action);
                    }
                    })

My output of the component is as follow:
User-added image
There are no records generated.
 
{tushar-sharma}{tushar-sharma}
Why you added line agains at 42-45. When you say unable to save are you hgetting any error, share that message.