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
Prashant singh 16Prashant singh 16 

Create a Lightning Component to display Case information

<aura:component controller="DisplayCaseController" implements="force:appHostable">
    <aura:attribute name="record" type="Case[]"/>
    <ui:inputNumber label="Case ID" aura:id="CaseID"/><br/><br/>
    <ui:button label="Get Case" press="{ !c.getCaseFromId }"/><br/><br/>
    <aura:iteration var="c" items="{!v.record}">
         <p>{!c.Subject} : {!c.Description} : {!c.Status}</p>
    </aura:iteration>
</aura:component>

 

 

client-side controller:-

({
    getCaseFromId : function(component) {
        var caseID = component.find("CaseID").get("v.value");
        var action = component.get("c.getCaseFromId");
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.record", response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})

 

 

public class DisplayCaseController {
@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];
}
}
}

 

Challenge not yet complete... here's what's wrong: 
The client side controller does not refer to the 'getCaseFromId' method of the Apex controller class

Thnaks In Advance

Prashant

 

 

James LoghryJames Loghry
Have you tested that your component actually works?  I'm guessing it doesn't.  You're not specifying the caseId parameter in your getCaseFromId method, so it's trying to call getCaseFromId() instead of getCaseFromId(Id caseId).  Try adding the following after the var action = line in your component's controller:
 
action.setParams(
            { 
                caseID : component.find("CaseID").get("v.value"); 
            }
        );


 
JannisBottJannisBott
It's a bit like trying to find the needle in the haystack. I got it working in many ways but trailhead eventually accepted the below:

Component:
<aura:component controller="DisplayCaseController">
    <aura:attribute name="record" type="Case"/>
    <aura:attribute name="caseId" type="String" default="50028000002mTc1"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
        {!v.record.Subject} : {!v.record.Description} : {!v.record.Status}
</aura:component>
Controller:
({
    doInit : function(component, event, helper) {
    var action = component.get("c.getCaseFromId");
    action.setParams({
        caseId : component.get("v.caseId")
    });

    action.setCallback(this, function(a) {
        if (a.getState() === "SUCCESS") {
            component.set("v.record", a.getReturnValue());
        }
    });

    $A.enqueueAction(action);
	}
})



Apex:
public class DisplayCaseController {

    @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];
        }        
    }
    
    
}



Bhanup83Bhanup83
The catch here is "Use unique names for client-side and server-side actions in a component.!" Here is the below working code

Component:
<aura:component controller="DisplayCaseController" >
    <aura:attribute name="record" type="Case" />
    <aura:attribute name="caseId" type="String" default="50090000003VVJp"/>
    <ui:button label="get Case Record"  press="{!c.getCaseRecord}" />
    <p>
        Subject : {!v.record.Subject}<br/>
        Description : {!v.record.Description}<br/> 
        Status : {!v.record.Status} 
    </p>
</aura:component>

JS Controller:
({
    getCaseRecord : function(component, event, helper) {
        var action = component.get("c.getCaseFromId");
        action.setParams({ 
            caseID : component.get("v.caseId") 
        });
        action.setCallback(this, function(response){
            component.set("v.record", response.getReturnValue());
        });
     $A.enqueueAction(action);
    }
})

Apex:
public class DisplayCaseController {
    @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];
        }        
    }
}



 
praveen sonepraveen sone
I faced the same kind of problem while solving the trailhead challenge. But, I got it working with the below code. you can use it if it helps.

Component: (DisplayCase)

<aura:component controller="DisplayCaseController">
    <aura:attribute name="record" type="case[]"/>
    
    <ui:button label="display case" press="{!c.getCaseFromId}"/>
    
    <aura:iteration items="{!v.record}" var="record">
        {!v.record.Subject},{!v.record.Description},{!v.record.status}
    </aura:iteration> 
    
</aura:component>
*************************************************************************************************
Apex Controller: (DisplayCaseController)

public class DisplayCaseController {

    @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];
        }        
    }
    
    
}
*************************************************************************************************
JsController: (DisplayCaseController.js)

({
    getCaseFromId: function(component,record,callback) {
        var action = component.get("c.getCaseFromId");
            
        action.setcallback(this,function(response){
                           var state = response.getState();
                           if(state === "SUCCESS"){                                
                               component.set("v.record",response.getReturnValue());
                           }        
        });
        
        $A.enqueueAction(action);
    }
})

Thanks!
Akhil MehraAkhil Mehra
but this code doesn't show values for case's Subject, Description, Status.

1. we can get the values by calling Iteration Variable as
<aura:iteration var="cse" items="{!v.record}"> 
{!cse.CaseNumber}&nbsp;:&nbsp;&nbsp;&nbsp;{!cse.Subject}&nbsp;:&nbsp;&nbsp;&nbsp;{!cse.Description}&nbsp;:&nbsp;&nbsp;&nbsp;{!cse.Status}

2. And If Are Use unique names for client-side and server-side actions in a component. then it will run recursively
Andrew EversleyAndrew Eversley
@Praveen Thank you for you input, your coding helped me to solve my issue. I appreciate your solution.