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
fiona gentryfiona gentry 

How Apex can be used to Send Case to Lightning cmp

Hi,
i need to send current case to lightning component and wrote below apex but seems i am not able to send current case record  id to lightning component,any solution
 
public static Case getCaseFromId(Id caseID) { 


    if(caseID == null) {
            return [SELECT ID, Subject, Description, STATUS from Case LIMIT 1];
        }
    
     List<Case> cases = [ SELECT Id, Subject, Description, Status from CASE where ID = :caseID ];
        
        if(cases.size() == 0) {
            return [SELECT ID, Subject, Description, STATUS from Case LIMIT 1];
        } else {
            return cases[0];
        }        

}
cmp file
<aura:attribute name="record" type="case[]"/>

controller file
var action = component.get("c.getCaseFromId");
    action.setCallback(this, function(response){
        var state = response.getState();
        if (state === "SUCCESS") {
          
            var navEvent = $A.get("e.force:navigateToSObject");
             alert("hello there!"+ component.get("v.record",response.getReturnValue())); 
            navEvent.setParams({

               "recordId": component.get("v.record",response.getReturnValue())

            });
            navEvent.fire();

Problem is alert shows "undefined" in browser that means something is wrong,any suggestions

Fiona
Best Answer chosen by fiona gentry
sreenath reddy 21sreenath reddy 21
Hi floina
When we use Apex Class in lightning it must be declared as @AuraEnabled annotation
@AuraEnabled
public static Case getCaseFromId(Id caseID) {
if(caseID == null) {
     return [SELECT ID, Subject, Description, STATUS from Case LIMIT 1];
}
  List<Case> cases = [ SELECT Id, Subject, Description, Status from CASE where ID = :caseID ];
if(cases.size() == 0) {
     return [SELECT ID, Subject, Description, STATUS from Case LIMIT 1];
}
else { return cases[0];
}
}